JQuery tablesorter自定义解析器到所有标头?

我目前正在使用tablesorter表,我需要为每个标头使用自定义解析器。 我想知道是否有一种简单的方法可以做到这一点,例如:

table.tablesorter({ headers: { 0-20: { sorter:'CareerLast' }, } }); 

我知道上面的代码不起作用,但我只是想知道是否有一种更易读的应用自定义解析器的方法,而不是通过索引手动将它放在每一列上。

好吧,我认为你有三个选择:

  1. 在初始化选项中定义每个标头0到20。

     header : { 0 : { sorter : 'CareerLast' }, 1 : { sorter : 'CareerLast' }, 2 : { sorter : 'CareerLast' }, // etc 20 : { sorter : 'CareerLast' } } 
  2. 使用元数据插件并在头类中添加分类器定义:

     // untested, but I think this will work $('table').find('thead th').addClass("{sorter:'CareerLast'}"); $('table').tablesorter(); 
  3. 试试我的分叉版本的tablesorter ,只需将分拣机添加为类名

     $('table').find('thead th').addClass('sorter-CareerLast'); $('table').tablesorter(); 

从addParser中的’is’返回true

例如:此解析器将’N / A’赋值为-1 $.tablesorter.addParser({ id: 'num-with-na', is: function(s) { //always use this return true; }, format: function(n){ return n === 'N/A' ? -1 : n; }, type: 'numeric' });