I have added a custom sort functionality in datatable. It works fine when the table header is clicked. However, On pageLoad I want the table to be sorted by that column using the same logic as provided in the custom sort function. It does not seem to do so. Here is my code snippet
$(document).ready(function(){
$("#stripedTableAcp").DataTable({
"columnDefs": [
{
"type": "acp_name",
"targets": 3
}
],
"order": [[ 3, "asc" ]],
"pageLength": 100,
"lengthMenu": [100, 500, 1000, 2000, 5000]
});
$.fn.dataTableExt.oSort["acp_name-desc"] = function (x, y)
{
console.log("1");
x = jQuery(x).text();
y = jQuery(y).text();
temp = x.split("-");
xPart1 = temp[0];
xPart2 = parseInt(temp[1]);
temp = y.split("-");
yPart1 = temp[0];
yPart2 = parseInt(temp[1]);
if(xPart1 > yPart1)
{
return -1;
}
if(xPart2 > yPart2)
{
return -1;
}
return 1;
};
$.fn.dataTableExt.oSort["acp_name-asc"] = function (x, y)
{
console.log("2");
x = jQuery(x).text();
y = jQuery(y).text();
temp = x.split("-");
xPart1 = temp[0];
xPart2 = parseInt(temp[1]);
temp = y.split("-");
yPart1 = temp[0];
yPart2 = parseInt(temp[1]);
if(xPart1 > yPart1)
{
return 1;
}
if(xPart2 > yPart2)
{
return 1;
}
return -1;
}
});
Please note that the console.log added to the function is fired when the table header is clicked but not upon page load
datatable version is 1.10.19 as provided by the CDN
Any help is greatly appreciated.