tablesorter.js多个textExtraction

时间:2018-01-28 19:20:26

标签: javascript jquery tablesorter

使用tablesorter.js对表列进行排序。

我有一列图片,所以我使用textExtraction来获取图片alt值来进行排序。

现在我想使用textExtraction用逗号和货币符号对其他列进行排序。

独立地,两者都可以工作,但我无法让他们两个都工作。

Tablesorter有example,我无法适应。

textExtraction: {
  0: function(node, table, cellIndex){ return $(node).find("strong").text(); },
  1: function(node, table, cellIndex){ return $(node).find("div").text(); },
  2: function(node, table, cellIndex){ return $(node).find("span").text(); },
  3: function(node, table, cellIndex){ return $(node).find("em").text(); },
  4: function(node, table, cellIndex){ return $(node).find("a").text(); },
  '.date' : function(node, table, cellIndex){ return $(node).find("u").text(); }
}

我按如下方式修改了我的代码:

$(document).ready(function($){ 
    $("#cardSort").tablesorter(
        {
            initWidgets: true,

            textExtraction: {
                0: function(s){
                    if($(s).find('img').length == 0) return $(s).text();
                    return $(s).find('img').attr('alt');
                },
                1: function(s){
                    return $(s).text().replace(/[,$£€]/g,'');
                }                    
            }
        }
    );
}
); 

这似乎与tablesorter示例相同,例如它使用编号的函数方法。但是这种格式都不起作用。

关于如何同时拥有textExtraction函数的任何想法?

编辑添加:这是TableSorter版本2.0.5b

1 个答案:

答案 0 :(得分:1)

听起来您可能仍在使用tablesorter的原始版本(v2.0.5)...将textExtraction设置为对象在该版本上不起作用,您需要使用我的{ {3}}

更新:如果必须使用v2.0.5,请尝试以下操作:

textExtraction: function(node) {
  var $cell = $(node),
    text = $cell.text();
  switch(node.cellIndex) {
    case 0:
      return $cell.find('img').length ? $cell.find('img').attr('alt') : text;
    case 1:
      return text.replace(/[,$£€]/g,'');
    default:
      return text;
  }
}

注意:当colspan中有tbody时,此方法无法正常工作。