views:

1404

answers:

6

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?

A: 

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

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

See the source

Jared
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
Scott
I can see that. Is tablesorter using a different parser for the "fancyNumber" columns? Would it be smart to move the parsers creation above the other ones so that it gets first crack at the column, in case one of the other parsers is being selected first?
Jared
Jared, How would I know if its using a different parser?I am using the mini version of the sorter so I can't exactly put this parser up front...My thought is that its looking at it like a number for some rows and a number with a comma for others... For some reason, its just not working correctly..
Scott
Here's the debug steps I would try:1) In Firebug Console (or whatever) make sure that the regex triggers a true on the first value in the column2) Make sure the replace is working correctly to strip the commas out3) Try the "unminified" version and put the parser as the second one (after text) and see if that works--if so, it'll be a little trickier to work around.
Jared
A: 

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})
duckyflip
Ducky, the second line is not working... It says its missing a ; at the location [where the header is declared.
Scott
@Scott try now, I forgot to add var headers = {}; line
duckyflip
@ducky, Nope, still not happening. One I used your idea, another column that was sorting incorrectly started working correctly. Maybe its just this column thats having trouble...Maybe its because there is a integer and a comma separated integer in the same column?
Scott
try slapping semicolons on the end of every line.
antony.trupe
+2  A: 

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']} });
Scott
Cool. Good job.
Jared
You don't have to do this. The headers option allows you to specify the data type. Look http://tablesorter.com/docs/example-parsers.html there and see. In your options you just specify `headers: { 5: 'mysorter' }` and hey presto.
Kezzer
A: 

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

Falcao
A: 

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

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

A: 

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'}"