Question

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?

Was it helpful?

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']} });

OTHER TIPS

This can also happen if you forget to include the metadata plugin

** Posted here since this was the first search result on Google.

here what I've done:

$(document).ready(function() {
      $.tablesorter.addParser({
        id: 'fancyNumber',
        is:function(s){return false;},
        format: function(s) {return s.replace(/[\,\.]/g,'');},
        type: 'numeric'
    });
    $("table").tablesorter({headers: {0: {sorter: 'fancyNumber'}}});
});

worked with comma and dot separator.

test it http://jsbin.com/equci5

I found a solution that worked for me. In the tablesorter.js, modify the formatFloat() function as follows:

this.formatFloat = function (s) {
            var i = parseFloat(s.replace(/[,]/g, ''));
            return (isNaN(i)) ? 0 : i;
        };

This will replace the commas which were interfering with the sorting. Found the answer here. Hope this helps...!

Try explicitly assigning the parser in the .tablesorter() declaration.

.tablesorter( { headers: { 0: { sorter:'fancyNumber' } }); 

See the source

As Jared has mentioned, you need to specify which column uses which Parser, if you don't know the index of the column then you can find it our using this:

var fancyIndex = $('th.fancyColumn').prevAll().length
var headers = {};
headers[fancyIndex] = {sorter:'fancyNumber'}

$("table").tablesorter({headers:headers})

I, try this regular expression: /(\d{1,3})?(\,\d{3})*/

The parsers only look at the first tbody row to detect which parser to use. I'm guessing your first row doesn't have any commas in it. I ran into the same problem, and finally just forced the parser I wanted, using class="{sorter: 'fancyNumber'}"

You are completely correct that the parser should be working, the reason it is not is due to a bug in the plugin. In short the plugin thinks it can sort numbers with commas in them correctly, and so uses its built in sorter, but then fails to sort them correctly.

There are a few ways to fix it.

Firstly you can (as you suggested above) force the use of your sorting function (either in the or in the javascript on initialisation of the plugin).

Secondly you can edit the plugin to use your functions in preference to your own, this can be achieved by reversing the direction of the for loop on line 220 of the plugin.

Thirdly you can fix the broken default sorting behaviour by modifying either the digit detection function to not accept commas (line 861 of the plugin) or by modifying the default number sorter to handle commas (line 852 of the plugin).

I have raised this issue on the tablesorter google code page: http://code.google.com/p/tablesorter/issues/detail?id=6

I found a solution that worked for me. In the tablesorter.js, modify the formatFloat() function as follows:

this.formatFloat = function (s) {
            s = s.toString();
            var i = parseFloat(s.replace(/[,]/g, ''));
            return (isNaN(i)) ? 0 : i;
        };

This will replace the commas which were interfering with the sorting. Found the answer here. Hope this helps...!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top