jQuery tablesorter-不使用格式化货币值对列进行排序
jQuery1.7.1和tablesorter插件——我有一个货币列,其中有1000个分隔符,值为$52.00$26.70$100.00$50.00$1002.00$1102.00。当我尝试按以下方式排序时jQuery tablesorter-不使用格式化货币值对列进行排序,jquery,tablesorter,Jquery,Tablesorter,jQuery1.7.1和tablesorter插件——我有一个货币列,其中有1000个分隔符,值为$52.00$26.70$100.00$50.00$1002.00$1102.00。当我尝试按以下方式排序时 $1,002.00 $1,102.00 $26.70 $50.00 $52.00 $100.00 需要这样的价值观 $26.70 $50.00 $52.00 $100.00 $1,002.00 $1,102.0
$1,002.00
$1,102.00
$26.70
$50.00
$52.00
$100.00
需要这样的价值观
$26.70
$50.00
$52.00
$100.00
$1,002.00
$1,102.00
尝试了这里提到的许多解决方案,但没有成功。Tablesorter允许您为类似的内容定义“”
// add parser through the tablesorter addParser method
$.tablesorter.addParser({
// set a unique id
id: 'thousands',
is: function(s) {
// return false so this parser is not auto detected
return false;
},
format: function(s) {
// format your data for normalization
return s.replace('$','').replace(/,/g,'');
},
// set type, either numeric or text
type: 'numeric'
});
$(function() {
$("table").tablesorter({
headers: {
6: {//zero-based column index
sorter:'thousands'
}
}
});
});
您可能需要调整format函数,我还没有测试过它。如果您只想修复货币编号(最快):
$(“表”).tablesorter({
textExtraction:函数(节点){
//对于格式为1.000欧元的数字,50欧元,例如意大利语
//返回$(node).text().replace(/[.$欧元]/g',).replace(/,/g',);
//对于格式为1000.50美元的数字,例如英语
返回$(node).text().replace(/[,$英镑欧元]/g',);
}
})
£80,000.00
我不喜欢关于StackOverflow的其他3个建议解决方案:
如果要修复所有数据类型(最灵活):
$(函数(){
$(“表”).tablesorter({
textExtraction:函数(节点){
var cell_value=$(节点).text();
var sort_value=$(节点).data('value');
返回(排序值!=未定义)?排序值:单元格值;
}
})
})
2008年4月1日
爱丽丝
£80.00
这具有将显示数据与排序数据分离的优点,更易于重用。遵循@Ownen提出的相同想法,自TableSorter v2.16.0以来,您可以直接使用
数据文本
属性,而无需声明自己的文本提取
函数(更多信息):
你为什么不去掉逗号,然后再加回去?@JosephSilber不知道怎么做。谢谢,看看这个。这是EU模式,但您可以找到它:@SyAu-要删除逗号,只需使用
str.replace(/,/g',)
。若要将它们添加回,请参阅。@BrentAnderson我遵循了该链接并找到了修复方法,请参阅下面接受答案下的我的评论。我测试了您的解决方案,它是有效的。谢谢我已选择接受你的答案。我有很多像上面那样带有货币值的页面,所以我在jquery.tablesorter.min.js的ts.addParser({id:“currency”…with s=s.replace(',','');正如建议的那样,感谢Brent Anderson的建议。也感谢Joseph Silber的建议。这在多个''s或','s ie 9.2(92)>2.2.9(229)的情况下失败@Blowsie,这并不奇怪,因为数据不再符合“数千”格式规则。你需要开发一个解析器来专门处理你的数据。这正是我要找的。谢谢!!如果你想在这个文本中添加“Kg”,当你尝试按字母“g”过滤时,提取会在其余列中创建一个bug。该列将无法筛选此“g”。因此我建议使用上面的“addParser”解决方案。
<script type="text/javascript">
$("table").tablesorter({
textExtraction: function(node){
// for numbers formattted like €1.000,50 e.g. Italian
// return $(node).text().replace(/[.$£€]/g,'').replace(/,/g,'.');
// for numbers formattted like $1,000.50 e.g. English
return $(node).text().replace(/[,$£€]/g,'');
}
})
</script>
<td><span>£80,000.00</span></td>
<script type="text/javascript">
$(function() {
$("table").tablesorter({
textExtraction: function(node){
var cell_value = $(node).text();
var sort_value = $(node).data('value');
return (sort_value != undefined) ? sort_value : cell_value;
}
})
})
</script>
<td data-value="2008-04-01">01 Apr 2008</td>
<td>Alice</td>
<td data-value="80.00"><span>£80.00</span></td>
<td data-text="2008-04-01">01 Apr 2008</td>
<td>Alice</td>
<td data-text="80.00"><span>£80.00</span></td>
$('#table').tablesorter({
widgets: ["output"],
widgetOptions : {
output_dataAttrib: 'data-text'
}
});