In JQuery tablesorter - How do I change parser according to the sort order

StackOverflow https://stackoverflow.com/questions/21872401

  •  13-10-2022
  •  | 
  •  

سؤال

I am using jQuery tablesorter on a table that has columns with comma separated number content. I added a Custom Parser and that solves my problem of sorting the comma formatted numbers. I want a special case, the value 'NA' to always be at the end of the sorted column, irrespective of the sort order. One way of doing that I figured is to "tweak" the Custom Parser I'm using in some way, like in the comment of below code.

 $.tablesorter.addParser({
    id: 'number-sorter',
    is: function (s) {
        return false;
    },
    format: function (s) {
        return s.replace(/,/g, '');

        /* what I want here is -
        if(sortOrder == "asc")
            return s.replace(/,/g, '').replace('NA', Number.POSITIVE_INFINITY);
        else
            return s.replace(/,/g, '').replace('NA', Number.NEGATIVE_INFINITY);
        */

    },
    type: 'numeric'
});

I'm having problem with determining the sort order (the variable that I'm assuming 'sortOrder'). Or is there any other way of doing this?

هل كانت مفيدة؟

المحلول

Actually, from what it appears you are trying to do is sort non-numbers at the bottom of the table. I have a fork of tablesorter that has an option named stringTo which allows you to set where you want strings (non-numeric values) to sort: 'max', 'min', 'top', 'bottom', 'none' (treats a string as a number with the value of zero); so in your case, set the value to 'max'.

Check out the demo

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top