I have a databable which uses ajax to load data from server. The data is not sorted at the server. I want to get datatable sorted on one column by default when it’s displayed.
Say I have the “siteId” property in my data model, and want the datatable to be sorted on the property when it’s displayed.
So I add the onAfterLoad function and try the following in the function:
// this.sort(“siteId”);
this.sort("#siteId#");
this.markSorting(“siteId”, “asc”);
Yeah, I get the sorted mark on the column, but the data is not actually sorted.
What else do I need to add to get this to work?
After seeing your example, I tried it on my own app again. It works fine on columns with default sorting type, like “string”.
But I have an IPV4 address column with a custom “ipv4” sorting type. It’s sorted as expected if I click on the column title. But it does not sort by the sort() call in the onAfterLoad event.
It’s my own fault, I guess.
I assume if I do not specify the sorting type in the sort call, the sorting type specified in the column definition will be used as the default. But no, the default sorting type is always “string”, irregardless of the column definition.
After I specify the sorting type in the sort call, it works as expected.
But, hey, I’m calling this.sort() on the datatable instance, and I have sorting type defined on the column, why does not the sort() method get sorting type from the column definition as the default?
this why i’m here, looking for a solution. i expected that my code works wrongly, cause by it complexity is not even close with any examples i ve seen here. was surprised i’ve reproduced.
This result is expected, since the initial sorting is done by a predefined sorting function, whereas table columns use a custom sorting function (sorting by length). Therefore, the sorting logic differs between both cases, leading to the observed result. If you want a consistent outcome, the sorting logic must be identical in both cases.
In actuality, it should look like this if you want to use a custom sorting function: