Sorting data with presenter?


#1

Hi.

I have a long list of stuff from my db and I want to let the user sort the list based on when it was created, when it was updated, etc.

but im struggling with finding out exactly how to do this with adonis without rerendering the whole page. is it possible to do this with a presenter and then re-render only the component?

I also need to use paginate when fetching data from the database.


#2

If you need to paginate then you need to sort on the database using ORDER BY query (or equivalent in the Lucid ORM).

Pagination and sorting is a rather complex issue and there is no one straightforward way of doing it.

But look into cursor-based pagination.


#3

Ok. If I were to not need pagination, would it be easier? And how could I solve it? Woudl it be possible to call a function within the presenter? or a controller?


#4

I normally do all of the sorting using the database, unless there is some obscure reason when it is not possible, then I’d use the .sort method on array.

The other option is to sort client side, if there is no pagination, in which case you can use one of the dynamic table libraries or a table sorting library.

Why do you insist on using the presenter? Why can’t you just sort in the database using the query builder?

https://adonisjs.com/docs/4.0/query-builder#_orderby_column_direction_asc


#5

using the .sort array method is what I want to do, and i would prefer doiung it client-side with JS, not by querying the database all the time, but it seems like database querying is the easiest way (and the only way if I need to paginate)


#6

If you need to paginate, you need to sort on the database side and use cursors, ideally.


#7

Here’s a good recent article on pagination by Slack Engineering team:


#8

thanks. ill look into it.

but i think it would be nice to be able to manipulate the data in some way client side with a presenter or something similar.


#9

I believe sorting is as simple as adding ORDER BY clause in the query and it can be independently of pagination


#10

Yeah, i know. But i was wondering if there was some kind of way to do it with js client side.


#11

There are way too many ways to do it on the client side. But this is an AdonisJS forum. If you want client side, you probably want to ask on that stack specific forum.