2
我想使用tablesorter来排序一列表格单元格中有许多美元符号。美元符号的金额将从一美元到五美元不等,并且应该以1为最低,5为最高。注意:我需要这些字符串完全匹配,因为会有很多美元符号被抛出,所以$$$需要与$$和$区分。Tablesorter排序多个美元符号
这是我目前使用的代码,当我有美元符号时它不工作,但是当我用“b”和“a”代替它们时,它完美地起作用。我有2个美元符号代表1,因为我读到这是代表正则表达式中的美元符号所必需的。我也试过\$
。这可能是由于美元符号在HTML表格中的输入方式造成的吗?我已经使用标准“$”和“$
”,都无法正常工作。
// add parser through the tablesorter addParser method
$.tablesorter.addParser({
// set a unique id
id: 'pricerange',
is: function(s) {
// return false so this parser is not auto detected
return false;
},
format: function(s) {
// format your data for normalization
return s.toLowerCase().replace(/\bd\b/g,3).replace(/\bc\b/g,2).replace(/\b$$$$\b/g,1).replace(/\b$$\b/g,0);
},
// set type, either numeric or text
type: 'numeric'
});
$(function() {
$("myTable").tablesorter({
headers: {
3: {
sorter:'pricerange'
}
}
});
});
我想这会工作,但遗憾的是它没有。我也试过:'s.replace(/ \ $/g,0)'和's.replace(/ \ b \ $ \ b/g,0)'和's.replace(/ $$/g,0 )'以防万一。没有任何工作,我为什么很困惑。 – Tasoli 2013-03-10 10:26:41
它们是真正的“$”符号还是“$”字符实体?他们是连续的(两者之间没有空白)?你是否将解析器应用于正确的列? – 2013-03-10 10:42:30
您还需要调整'$(“myTable”)'中的选择器,例如'$(“#myTable”)'。 – 2013-03-10 10:47:11