Like what we’re doing? Star us on GitHub!

Sorting relationship items

rrums
2 weeks 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?

  • jesschow
    Payload Team
    2 weeks 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
  • rrums
    2 weeks 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,
      }
    }
  • Jarrod
    Payload Team
    last week

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

  • rrums
    last week

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

Open the post
Continue the discussion in Discord
Can't find what you're looking for?
Get help straight from the Payload team with an Enterprise License.Learn More