How to change order of collection items.

default discord avatar
saaymeen
8 months ago
9

How can I control the order of the items in my collection? When applying no filters or sorts, I want the REST API to deliver the collection in a predefined order.

  • discord user avatar
    patrikkozak
    Payload Team
    8 months ago

    Hey @saaymeen! One option you could do is add a middleware to the collection that sets a default sort order.



    So on your collection config, you would do something like this:


     endpoints: [
         {
          path: '/',
          method: 'get',
          handler: (req: PayloadRequest, res: Response, next) => {
            if (!req.query.sort) {
              req.query.sort = '-updatedAt';
            }
            next();
          },
        },
      ],


    Let me know if this is what you were looking for or if you have any questions!

  • default discord avatar
    saaymeen
    8 months ago

    Hey @patrikkozak, thanks for getting back to me. The issue here is that we want the items to appear in an arbitrary custom order by default. Let's say we have a client that manages a collection of recipes. The client now wants to control in what order they appear by default - this order is completely arbitrary to them. Sorting by most viewed, most liked or popular is out of the question here. The only two solutions I found so far require the client to upload the recipes in the one specific order they want (meaning they cannot rearrange it) - or me manually sorting it in middleware having to adjust the code every time a new recipe is added or the order changes.



    Hey, I would like to bump this. Can you please change the tag back to unanswered @patrikkozak.

  • discord user avatar
    jarrod_not_jared
    Payload Team
    8 months ago

    Hey @saaymeen I believe what you are asking for is marked as planned on our current roadmap, read more here:

    https://github.com/payloadcms/payload/discussions/1413
  • default discord avatar
    saaymeen
    8 months ago

    Thanks @jarrod_not_jared, that's exactly what I'm looking for and seems to have a PR coming. Cheers!

  • discord user avatar
    jarrod_not_jared
    Payload Team
    8 months ago

    I think the tagged PR on that roadmap item might be a bit misleading - and I think should be discussed there (I can do that). The linked PR allows for dragging and dropping filter headings (which is great!) but im not sure that is what

    you

    are looking for.



    If I am understanding you correctly, you would like to be able to drag and drop rows in an unfiltered list, right?



    Which is what the discussion is aimed at, but I think the PR may be misleading and unrelated to the actual roadmap item now that I am reading it back again.

  • default discord avatar
    majohannsen
    3 months ago

    Any updates on this so far? Would be a very convenient feature 🙂

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.