Sort columns when server side pagination is enabled

Hello,

I want to sort data based on the creation date, but nothing happens when I click on the header.
I have enabled server-side pagination.
And we have an ordering option available on the API. Just not sure how to trigger the API when header is clicked

@Karan

Hey there :wave: Thanks for reaching out! Just to confirm, toggling server-side pagination on should disable column sorting. Here is a link to the section in our docs on Server side pagination with dynamic column sorting :slightly_smiling_face:

Ok, Thanks @lauren.gus

hello, it seems the section you describe does not longer exists on the page you linked.
do you have any documentation about best practices to handle sorting in server-side pagination?

Hey @duc-gp!

Here's an updated link to our server-side pagination docs. Typically, you'll want to use the sortedColumn and sortedDesc properties of the table as inputs for the query itself and have it trigger off of changes in those properties. Is there any particular issue you're running into when setting it up?

1 Like

It would be great if an example is provided in that docs since it's quite common. I can't even add the following to my SQL query:

{{table1.sortedColumn !== null ? `order by ${table1.sortedColumn} desc` : ''}}

Also, how can I put the desc / asc dynamically?

Appreciate your help.

Hey @cubeadinda

It looks like there's currently a bug with server-side paginated tables that's interfering with setting up dynamic sorting but I can report back here when it has been fixed!

To give an example of how this should work:

Dynamically setting column sort order in SQL mode depends on whether or not you have converting queries to prepared statements enabled for your resource (it is by default). Meaning your query is converted to a prepared statement and every transformer is passed as a parameter to prevent SQL injection attacks. Typically, SQL prepared statements don't let you set ORDER BY clauses using query parameters, see this PostgreSQL post for an example.

If you turn prepared statements off you can dynamically create any SQL string and pass it through. However, with it turned on, you might need to do something like the following for each of your sort columns:

CASE WHEN {{ table.sortedColumn == ‘name’ && tblResults.sortedDesc }} THEN name END DESC,
CASE WHEN {{ table.sortedColumn == ‘name’ && !tblResults.sortedDesc }} THEN name END ASC
2 Likes

when calling the table you dont have property of sortedColumn or sortedDesc. i am not sure if your referencing the old table or the new one but the new doesnt have that capability

Unfortunately, most of the documentation on server-side pagination appears to be focused on the old table. So far I have been able to find that the property you are looking for is sortArray on the new table.

2 Likes