I am trying to sort on a column called Priority. There are 5 possible values for this column: High, Medium, Low, Unprioritized, and N/A. I'd like to be able to sort them in that order (as ascending) or the reverse (as descending). My approach was to create a custom sorting function based on what I found here. This is what I did:
option["columnDefs"] = [
{
"render":function(data,type,row) {
var $select;
switch(data) {
case: "High":
$select = $('<div class="my-class-high" priority="high">High</div');
break;
case: "Medium":
$select = $('<div class="my-class-medium" priority="medium">Medium</div');
break;
// etc. for other values.
}
return $select.prop("outerHTML");
},
"targets" : 7,
"sType" : "priority"
}
];
function getRank(cellHtml) {
switch ($(cellHtml).attr("priority")) {
case "high":
return 0;
case "medium":
return 1;
case "low":
return 2;
case "unprioritized":
return 3;
case "notapplicable":
return 4;
default:
throw "Unrecognized priority.";
}
}
jQuery.fn.dataTableExt.oSort["priority-desc"] = function (x, y) {
return getRank(x) < getRank(y);
};
jQuery.fn.dataTableExt.oSort["priority-asc"] = function (x, y) {
return getRank(x) > getRank(y);
};
//further down...
$(#mytable).DataTable(option);
All of the code, including the sort function, is being hit as expected. When I click on the Priority column, the glyph flips. My problem is that the sort order as displayed never changes: It remains at ascending, which is correctly displayed with High priorities at the top of the table.
I created a temporary event handler function to check on things:
$(tableLookup).on('order.dt',function() {
var order = table.order();
});
With this I can verify further that the sort order is changing (internally, at least) each time I click the column header, even though the displayed order isn't updating.
I'm running jquery.dataTables.js version 1.10.4.
I've also tried to use the data-sort attribute, as suggested in Anjani's answer, here. This has no effect on the sort order at all--the table still sorts the column alphabetically using the displayed text.
Any ideas what could be causing these issues I'm seeing and how to get sort working?