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.

Sorting relationship items

default discord avatar
rrums2 years ago
5

Hey guys, I have problem when I want to swap field component of relationship, and use

useField

hook to update the field. To add the items it works, but somehow I can't change the order of items.



I use setValue with an array, i.e.


initialValue ->

['a', 'b', 'c']

then

setValue(['c', 'b', 'a'])

then refresh page still

['a', 'b', 'c']

it is works when i add item, i.e

['a', 'b', 'c', 'd']

Is there another way to set item's order?

  • default discord avatar
    jessrynkar2 years ago

    hi

    @946373741175267329

    - where are you getting the array of data from? as you can sort on requests

    https://payloadcms.com/docs/queries/overview#sort
  • default discord avatar
    rrums2 years ago

    not to querying, but to store data (array of IDs)


    because i swapped the field component, but i can't change order of the IDs



    like this -> i log the

    value

    , it has correct arrays, the order is set as I drag the item. But it wasn't stored with correct order.



    FYI, the field on collection


    {
      name: 'items',
      type: 'relationship',
      relationTo: 'menuItems',
      hasMany: true,
      required: true,
      admin: {
        components: {
          Field: MenuSelect // custom component
        },
        isSortable: true,
      }
    }
  • discord user avatar
    jarrod_not_jared
    2 years ago

    Curious, what does the network panel look like for this request?

  • default discord avatar
    rrums2 years ago

    ouch found the problem, because I fetch not from initialValue. My bad.

  • default discord avatar
    whysocketlast year

    Hey

    @946373741175267329

    I store an order field.


    Check this, but the update doesn't seem to work too.


    https://discord.com/channels/967097582721572934/967097582721572937/1237583115715874836

    How did you handle that?

    @281120856527077378

    @946373741175267329
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.