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.
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!
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.
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/1413Thanks @jarrod_not_jared, that's exactly what I'm looking for and seems to have a PR coming. Cheers!
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
youare 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.
Any updates on this so far? Would be a very convenient feature 🙂
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.