Richie
Richie

Reputation: 5197

jquery tablesorter how to ignore a row from the sort

I've got a bit of a problem with my table sorter sort at the moment. The issue is that I am sorting my time but when the column contains n/a I would like it to appear at the end of the sort. Can anyone help me achieve this please?

enter image description here

Here is my sort code...

$("#myStoreStatus").tablesorter({
    sortList: [[2,1],[8,0],[0,0]],
    stripingRowClass: ['even','odd'],
    stripeRowsOnStartUp: true,
    widthFixed: false, 
    widgets: ['zebra'],
    dateFormat: "uk",
    headers: {
        0: { sorter: 'digit' } // column number, type
    }
});

Upvotes: 3

Views: 1773

Answers (2)

Jadran Josimovic
Jadran Josimovic

Reputation: 86

As far as I could understand the problem is that you want the n/a rows to appear always at the end, even if the sorting order for the time column is changed.

Here is one possible option.

$("#myStoreStatus").tablesorter({
    sortList: [[2,1],[8,0],[0,0]],
    stripingRowClass: ['even','odd'],
    stripeRowsOnStartUp: true,
    widthFixed: false, 
    widgets: ['zebra'],
    dateFormat: "uk",
    headers: {
        0: { sorter: 'digit' } // column number, type
    },

    textExtraction: function (node) {
        if (($(node).index()==8) && ($(node).text().toLowerCase()=='n/a'))
            $(node).parent().addClass('jsnamark');
        return $(node).text();
    }

}).bind('sortEnd' function () {
    $(this).append($(this).find('.jsnamark'));
});

Now, what this peace is doing is quite simple. It uses textExtraction function to check if the 8th (time) cell has the 'n/a' value. If it does then it adds a jsnamark class to its row. (you can also do the same thing in a several different ways with jQuery or from the server side if you are serving page with a server script).

When the sorting is done it reappends the rows that have the jsnamark class to the end of the table.

Upvotes: 1

Mottie
Mottie

Reputation: 86453

You might want to also check out my fork of tablesorter.

There is a built-in option where you can set how string values (e.g. "N/A") sort within numerical columns - see this demo.

If you are using the original tablesorter, you can use the textExtraction option to replace N/A with an empty string; or use @Jadran's code which would also work.

Upvotes: 1

Related Questions