Search code examples
sortingjquery-pluginstablesorter

JQuery TableSorter Comma-Digit Parser not working


Here's my problem,

I am currently using the JQuery Table Sorter and I found a Comma-Digit parser on the web. The problem I am having is it doesn't seem to be working.

So here is what the column is sorted as:

  1. 4,666
  2. 141,666
  3. 293
  4. 341,666
  5. 346
  6. 461,676

This should be sorted as

  1. 293
  2. 346
  3. 4,666
  4. 141,666
  5. 341,666
  6. 461,676

The parser I am using is this:

$( function() { 

    $.tablesorter.addParser({
        id: "fancyNumber",
        is: function(s) {
            return /^[0-9]?[0-9,\.]*$/.test(s);
        },
        format: function(s) {
            return $.tablesorter.formatFloat(s.replace(/,/g, ''));
        },
        type: "numeric"
    });
}); 

I just don't know I am doing wrong. Am I loading it wrong? Is the parser wrong? I need real help here and have been struggling with this problem for a while now.

Edit: Because of how I generate my columns and the columns allowed to be chosen by the user, I would never know which header is in and not. I have tried using the class="{sorter: 'fancyNumber'}" command as stated here: http://tablesorter.com/docs/example-meta-parsers.html

**Edit:**It looks like one of the columns is working correctly, but this column is still having problems. maybe because it has digits and comma seperated digits?


Solution

  • For anyone that comes across this question. I had to add the class to my header row. So for any header that I wanted to fancy sort, I added this class:

    <th class=\"{sorter: 'fancyNumber'}\">
    

    This turned on the sorter by default which made it work nice.

    What made me realize my error in my ways was turning on the debugger like so.

    $("#tblInfo").tablesorter({debug:true, widgets: ['zebra'], widgetZebra: { css: ['d0', 'd1']} });