Sorting relationship items

default discord avatar
rrums
6 months ago
7

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?

  • discord user avatar
    jesschow
    Payload Team
    6 months ago

    hi @rrums - 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
    rrums
    6 months 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,
      }
    }
  • default discord avatar
    Jarrod
    6 months ago

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

  • default discord avatar
    rrums
    6 months ago

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

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.