custom sorting column on the basis of priority for datatable
custom sorting column on the basis of priority for datatable
I want to custom sort data Table on the basis of priority. I have written code as follows. can anyone help me with please. I saw some solution here in this forum. My problem is: its not sorting properly. I want to sort forth column on the basis of priority but it is not working as expected.
here is my code
$(document).ready(function() {
$('#dashboard').DataTable({
"lengthMenu" : [ [ 25, 50, 75, -1 ], [ 25, 50, 75, "All" ] ],
"pageLength" : 200,
"aoColumns" : [ null, null, null, {
"sType" : "priority",
"bSortable" : true
} ]
});
});
function getPriority(name) {
var rankNumber;
if (name == "High - 1st") {
rankNumber = 1;
} else if (name == "High - 2nd") {
rankNumber = 2;
} else if (name == "High - 3rd") {
rankNumber = 3;
} else if (name == "High - 4th") {
rankNumber = 4;
} else if (name == "High - 5th") {
rankNumber = 5;
} else if (name == "High") {
rankNumber = 6;
} else if (name == "Medium") {
rankNumber = 7;
} else if (name == "Low") {
rankNumber = 8;
} else if(name == "") {
rankNumber = 9;
}
return rankNumber;
}
jQuery.fn.dataTableExt.oSort["priority-desc"] = function(x, y) {
return getPriority(x) < getPriority(y);
};
jQuery.fn.dataTableExt.oSort["priority-asc"] = function(x, y) {
return getPriority(x) > getPriority(y);
}
I have attached my screen shot of table. Forth column(i.e Requested Priority) is sorting randomly(expected to sort on the basis of rankNumber but not working) Can anyone help me with this.
Thank you in advance!
This question has an accepted answers - jump to answer
Answers
looks like you are using the older syntax of datatables. Not sure whether this is going to work with what you are using but it should work with the latest version of datatables.
This code auto-detects the column as being a priority column and then does the sorting. The code needs be above the datatable.
Thank you so much @rf1234 . It worked