custom sorting column on the basis of priority for datatable

custom sorting column on the basis of priority for datatable

itsmeseema00itsmeseema00 Posts: 4Questions: 2Answers: 0

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

  • rf1234rf1234 Posts: 2,985Questions: 87Answers: 421
    edited December 2017 Answer ✓

    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.

    //auto detection only works if ALL cells of a column comply with the criterion !!!
    
    var rank = [  'High - 1st', 'High - 2nd', 'High - 3rd', 'High - 4th',
                  'High - 5th', 'High', 'Medium', 'Low', ''               ];
    
    $.fn.dataTable.ext.type.detect.unshift( function ( data ) {
        if (typeof data !== 'undefined') {
            if ( data != null )  {
                var i=0;
                while ( rank[i] ) {
                    if ( isNaN(data) ) {  //search may only be performed on non-numeric fields
                        if ( data.search( rank[i] ) > -1 )   {
                            return 'priority';
                        }
                    }
                    i++;
                }
            }
        }
        return null;
    } );
    
    $.extend( $.fn.dataTable.ext.type.order, { 
        "priority-pre": function ( 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;
        },
        "priority-asc": function ( a, b ) {
                return a - b;
        },
        "priority-desc": function ( a, b ) {
                return b - a;
        }
    } );
    
  • itsmeseema00itsmeseema00 Posts: 4Questions: 2Answers: 0

    Thank you so much @rf1234 . It worked :smile:

This discussion has been closed.