Default Sort functionalty not working correctly when on-demand loading of data is implemented.
Default Sort functionalty not working correctly when on-demand loading of data is implemented.
After implementing on-demand loading of data the default column-sort functionality is working inconsistently.
In some cases the data-table gets sorted in wrong order and in some cases the sort-image(arrows) is displayed incorrectly. Although in most cases the sorting is done correctly. Hence there's consistency issues.
I have set the descending order as default for the 'ID' column at the code-level. Here's the code for it: (This code works correctly. Whenever the page loads for first time the ID column gets sorted in descending order)
var bIsLoad = false;
var sorter = function () {
var search = {
SortColumn: bIsLoad ? settings.aoColumns[settings.aLastSort[0].col].data : "ID",
SortDirection: bIsLoad ? settings.aLastSort[0].dir : "desc"
};
bIsLoad = true;
return JSON.stringify(search);
}
But sometimes when we click the header to sort it sorts incorrectly .
Kindly see the attachment.
Here's my sample datatable JS code :
"dom": '<"top"lB>tr<"bottom"ip>',
"lengthMenu": [[10, 25, 50, 100, 500], [10, 25, 50, 100, 500]],
"pageLength": 25,
"pagingType": "full_numbers",
"ordering": true,
"order": [[0, "desc"]],
"scrollX": true, "filter": true,
"responsive": true,
"serverSide": true,
"info": true, "processing": true,
"stateSave": true,
"ajax":
{
"url": "Home/Load",
"type": "POST",
"datatype": "json",
"data": function (d) {
d.searchParams = sorter;
},
},
Here the debug link : http://debug.datatables.net/idoquj
Can you tell me why the data get's sorted incorrectly? How can I solve this issue?
All I want is to make any column to sort accordingly consistently. Currently i
Answers
You are using server-side processing (
"serverSide": true,
) so if sorting isn't working, it means yourHome/Load
script isn't doing the sorting. See the server-side processing manual if you want more details on server-side sorting.Also consider, do you actually need server-side processing? If not, disable it and let DataTables sort the data.
Allan
Hi,
Came to know where I'm doing wrong.
As per the code I've provided above I'm using this statement : (copying the code again)
So for the first when the page will load, the variable bIsLoad **will always be **false.
Hence the data will be displayed in descending ** order and sort coloumn will be **ID.
Now let's assume I click the column header of any column for sorting (this will be the first time I'll sort by clicking after page load) let's say column named 'Name'.
Now due to this line of code :
it won't take ** 'Name' ** column for sorting but instead it'll take the last column sorted which in our case is 'ID' because I've written :
What code should I use to replace :
and make the datatable sort the current column clicked(which in our case was Name)
Thanks in advance.
Is there settings.aCurrentColumn[0] ??
The parameters sent by DataTables are documented here. You do indeed need to look up the column index data from the sorting information.
Allan