Hi all,
I'd like to set up 1->1 relationship in between a doctor and their specialization, where specialization is defined with a single name (text) field.
const Doctors: CollectionConfig = {
slug: 'doctors',
fields: [
{
name: 'specialization',
required: true,
label: 'Spec',
type: 'relationship',
relationTo: 'specializations',
hasMany: false
},
]
}
Available values in
specializationscollection are queried properly, but in the admin UI, options in the dropdown are shown as ids of specializations (attachment) Is there a way to opt in for an arbitrary field in the related collection to be used as a value displayed in dropdown? This is very important from the UX perspective.
do I need to build a custom Field for this or is there a predefined config I could use? seems like a pretty standard use case to me 🙂
Yep! In your
specializationscollection config you just need to set the
useAsTitlefield to any of the fields of the collection. Whatever you select here will be used as the labels in those dropdowns.
Here's a link to that piece of documentation
https://payloadcms.com/docs/configuration/collections#admin-optionsthanks! exactly what I needed
Is there a way to set useAsTitle to two different values? We want to show a tax percentage but also if this is inclusive or exclusive (else it would be displayed as: 25%, 25% oder inclusive, inclusive without knowing wich entry to choose)
like this:
const Taxes: CollectionConfig = {
slug: 'taxes',
admin:{
useAsTitle: "percentage"+"taxType"
},
fields: [
{
name: 'percentage',
type: 'number',
required: true,
},
{
name: 'taxType',
type: 'select',
options: [
{
label: 'inclusive',
value: 'inclusive',
},
{
label: 'exclusive',
value: 'exclusive',
},
],
},
],
}
useAsTitletakes the name of any one field, so no your example is not currently possible — BUT I can see the value here. If we support a callback function for that property then you could potentially format it to exactly your specifications.
this would be great!
Star
Discord
online
Get dedicated engineering support directly from the Payload team.