How can I generate an ID based on Title field?

default discord avatar
Taun
3 weeks ago
6

Hi. When I create Categories, the display name is the ID string, I'd like it to be the same as it's title. How do I go about this?



Repo:

https://github.com/taunhealy/Melex_CMS

Categories.ts


import { CollectionConfig } from 'payload/types';

const Categories: CollectionConfig = {
  slug: 'categories',
  admin: {
    useAsTitle: 'name',
  },
  access: {
    read: () => true,
  },
  fields: [
    {
      name: 'name',
      type: 'text',
    },
  ],
  timestamps: false,
}

export default Categories;


I've inputted 'Road-Legal'. I'd like that to display as the ID name when I view the differing categories.



I'm using the PayloadCMS E-commerce template

  • default discord avatar
    tylandavis
    3 weeks ago

    Hi @Taun, you can change the columns that appear in the List view by adding an array of field names to your collection config.


    admin: {
      useAsTitle: 'name',
      defaultColumns: [
        'name',
        // any other columns you want to show
      ],
    },
  • default discord avatar
    Taun
    3 weeks ago

    Will it be considered good practice to have a unique string rather than the name of the category? Or for simplicity sake can I auto-name the ID?

  • default discord avatar
    tylandavis
    3 weeks ago

    Personally, I would always leave a ID, just in case I want to query by that specific document, and there is less of a chance of accidentally duplicating a custom-set ID.



    But, if you really wanted to, you can easily set a custom ID in Payload by passing in an ID field:


    {
      fields: [
        {
          name: 'id',
          type: 'number',
          // can also be of type: 'text'
        },
      ],
    }
  • default discord avatar
    Taun
    3 weeks ago

    Thanks @tylandavis . Do you know how I can move the 'name' column to appear as the first column? How do I re-order these in general?

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.