dsm1998 2016-02-15 09:09
浏览 41
已采纳

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

I am trying to sort the data in the table using tablesorting plugin but the data has commas(,) as the separator so it is not sorting properly. I think it is considering the number as a string. With the help of google, I have found some codes but those are not working 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 am doing wrong.

I have given class <th width="62" class="{sorter: 'fancyNumber'}">column</th> to the column also.

  • 写回答

1条回答 默认 最新

  • dowbwrr3590709 2016-02-15 13:36
    关注

    If you set the sorter in the class name like this:

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

    Make sure you are also loading in the metadata addon because that is needed to process that format.

    Or, if you don't want to use that plugin, you can set the parser using the headers option:

    $(function(){
      $('table').tablesorter({
        headers : {
          0 : { sorter: 'fancyNumber' }
        }
      });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?