javascript - tablesorter.js 多次文本提取

标签 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 Version 2.0.5b

最佳答案

听起来您可能仍在使用原始版本的 tablesorter (v2.0.5)...设置 textExtraction由于对象在该版本上不起作用,因此您需要使用我的 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 时,此方法将无法正常工作。

关于javascript - tablesorter.js 多次文本提取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48490371/

相关文章:

显示当前和+1 日期的 Javascript 时间

javascript - jQuery 检测动态添加元素的事件

javascript - 2个for循环写在一个循环中的时间复杂度是多少

jquery - 添加 css 类验证规则时如何分配错误消息

javascript - Modernizr 减慢了表排序器的速度

javascript - 大型动态表的性能极慢

javascript - CORS,防止带有授权 header 的请求预检

jquery - jquery 1.9.1 的选择器更新

javascript - 创建一个接受对象列表的函数和一个通过它们的匿名函数

javascript - TableSorter StickyHeaders 小部件不适用于多个表