custom sorting, group on secondary column

custom sorting, group on secondary column

saustinsaustin Posts: 7Questions: 2Answers: 0

I need to know how group a custom sort based on a secondary column.

I want to be able to group my sort on the value in the second column, then sort like normal based on that grouping.

Here are my sorting functions:

jQuery.fn.dataTableExt.oSort['sortFed-asc'] = function (x, y) {
    if (x === "Federal") return -1; // keep this row at top
    if (y === "Federal") return 1; // keep this row at top

    var stateName1 = $('label[id*=_label_]', '<div>' + x + '</div>').text();
    var stateName2 = $('label[id*=_label_]', '<div>' + y + '</div>').text();

    return ((stateName1 < stateName2) ? -1 : ((stateName1 > stateName2) ? 1 : 0));
};

jQuery.fn.dataTableExt.oSort['sortFed-desc'] = function (x, y) {
    if (x === "Federal") return -1; // keep this row at top
    if (y === "Federal") return 1; // keep this row at top

    var stateName1 = $('label[id*=_label_]', '<div>' + x + '</div>').text();
    var stateName2 = $('label[id*=_label_]', '<div>' + y + '</div>').text();

    return ((stateName1 < stateName2) ? 1 : ((stateName1 > stateName2) ? -1 : 0));
};

$("#myTable").dataTable({
    "bAutoWidth": false,
    "bFilter": false,
    "paging": false,
    "info": false,
    "scrollY": "300px",
    "scrollCollapse": false,
    "bSortCellsTop": true,
    "aoColumns": [
        { "sWidth": "60px", "sType": "sortFed", "sClass": "agency_col" },
        { "sWidth": "30px", "sType": "sortFed", "sClass": "status_col" }
    ]
});

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 62,858Questions: 1Answers: 10,344 Site admin
    Answer ✓

    You cannot use a sorting function to perform a secondary sort - use the multi-column sorting abilities of DataTables instead.

    • To have a specific column always do a multi-column sort use columns.orderData.
    • To have one or more columns always prefixed or postfixed to the user defined sort use orderFixed.

    Allan

  • saustinsaustin Posts: 7Questions: 2Answers: 0

    How would I use the answers you gave me to keep the first column ordered based on the second column? I want to keep a status of not ready at the top of my table when the first column is sorted?

    That is if I have a table with two columns, the first being a state name, the second column being a status. That status can be ready or not ready. I need the states with a not ready status to always be at the top when the first column is sorted,

  • saustinsaustin Posts: 7Questions: 2Answers: 0

    nevermind. I got it working. I accidentally added orderFixed to the wrong table. Thanks!

This discussion has been closed.