使用法语编号的Tablesorter特定排序

时间:2016-06-28 08:32:21

标签: javascript jquery regex tablesorter

我使用tablesorter排序不同类型的数字和字符串,但在这个数字中我的数字如下:(逗号和空格)

200,08€
1 201,56€
1 521 120,00€

我必须像

一样
1 521 120,00€
1 201,56€
200,08€

和反向。

我尝试解析器:



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




但是它没有用,你知道为什么吗? 非常感谢!

1 个答案:

答案 0 :(得分:1)

解析器有两个问题:(1)逗号应替换为小数点,(2)需要删除空格。

尝试此更新(demo

$(function() {

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

  $('table').tablesorter({
    sortList: [[0,1]],
    headers: {
      0: { sorter: 'colcpl' }
    }
  });

});
相关问题