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 to change order of collection items.

default discord avatar
saaymeen2 years ago
6

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
    2 years ago

    Hey

    @345089134328807425

    ! 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
    saaymeen2 years ago

    Hey

    @1032341301619871785

    , 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

    @1032341301619871785

    .

  • discord user avatar
    jarrod_not_jared
    2 years ago

    Hey

    @345089134328807425

    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
    saaymeen2 years ago

    Thanks

    @281120856527077378

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

  • discord user avatar
    jarrod_not_jared
    2 years 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
    majohannsen2 years ago

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

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.