Using tables with a comma disabled

I am trying to sort data in a table using the tablesorting plugin, but the data has commas (,) as a delimiter, so they are not sorted correctly. I think it treats the number as a string. With google, I found some codes, but they do not work for me. Here is what I have tried so far.

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

Please tell me what I'm doing wrong.

I also gave a class <th width="62" class="{sorter: 'fancyNumber'}">column</th>in a column.

+4
source share
1 answer

If you set the sorter to the class name as follows:

<th width="62" class="{sorter: 'fancyNumber'}">column</th>

, , .

, , headers:

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

Source: https://habr.com/ru/post/1628803/


All Articles