使用逗号分隔符的表排序不起作用


Tablesorting with comma seperator not working

我正在尝试使用表排序插件对表中的数据进行排序,但数据以逗号(,(作为分隔符,因此无法正确排序。我认为它将数字视为字符串。在谷歌的帮助下,我找到了一些代码,但这些代码对我不起作用。这是我到目前为止尝试过的。

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

请告诉我我做错了什么。

我也给专栏提供了类<th width="62" class="{sorter: 'fancyNumber'}">column</th>

如果在类名中设置排序器,如下所示:

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

确保您也在元数据插件中加载,因为这是处理该格式所必需的。

或者,如果您不想使用该插件,可以使用headers选项设置解析器:

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