覆盖表排序器顺序

Override table sorter order

本文关键字:顺序 排序 覆盖      更新时间:2023-09-26

我想重写表排序器上的排序。现在,我的表中有以下动态添加的条目:

1 Active,34 Active, 453 Active, 3235 Active, inactive, inactive

问题是,有另一列,我希望所有的活动项排序,而不是通过它前面的数字。

是否可以用表排序器做到这一点?如果有,那是怎么回事?

好了,我算出来了。我所要做的是使用一个表分析器。然后,我使用正则表达式从Active字符串中剥离数字和空格。之后我将inactive替换为1,将active替换为2。然后,我可以根据活动和非活动类别对表进行排序,并根据另一列进行进一步排序。下面是代码:

$.tablesorter.addParser({
  //give an id to the parser
  id: 'campaigns',
  is: function(s) {
    return false;
  },
  format: function(s) {
    //remove any numbers and spaces in the string s
    var state = s.replace(/[0-9]/g, '').replace(/'s/g, '');
    //change inactive to a 1 and active to a 2
    return state.toLowerCase().replace(/inactive/,1).replace(/active/,2);
  },
  //sort on a numeric type
  type: 'numeric'
});
$(function() {
  $("table#game-data-table").tablesorter({
    //sort based off of active inactive, then off column 4,and finally off column 1
    sortList: [[1,1], [3,1], [0,0]],
    headers: {
    1: {
      sorter: 'campaigns'
    }
  }
 });
});

你可以这样做:

$(document).ready(function() { 
// call the tablesorter plugin 
$("table").tablesorter({ 
    // sort on the first column and third column, order asc 
    sortList: [[0,0],[2,0]] 
}); 

});