使用 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/