Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Relationship dropdown display values

default discord avatar
mainzhum3 years ago
5

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

specializations

collection 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 🙂

  • discord user avatar
    jacobsfletch
    3 years ago

    Yep! In your

    specializations

    collection config you just need to set the

    useAsTitle

    field 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-options
  • default discord avatar
    mainzhum3 years ago

    thanks! exactly what I needed

  • default discord avatar
    neonpinkvisu3 years ago

    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',


    },


    ],


    },


    ],


    }

  • discord user avatar
    jacobsfletch
    3 years ago
    useAsTitle

    takes 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.

  • default discord avatar
    neonpinkvisu3 years ago

    this would be great!

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.