tablesorter.js 多个文本提取

tablesorter.js multiple textExtraction

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

我有一个包含图像的列,所以我使用 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

听起来您可能仍在使用原始版本的 tablesorter (v2.0.5)...设置 textExtraction as an object doesn't work on that version, you'll need to use my fork of tablesorter.


更新:如果您必须使用 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;
  }
}

注意:当 tbody.

中有 colspan 时,此方法将无法正常工作