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.

How can I generate an ID based on Title field?

default discord avatar
taun2160last year
4

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

  • discord user avatar
    tylandavis
    last year

    Hi

    @479030528084017165

    , 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
    taun2160last year

    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?

  • discord user avatar
    tylandavis
    last year

    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
    taun2160last year

    Thanks

    @783701636165402624

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

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.