SyA*_*yAu 22 jquery tablesorter
jQuery 1.7.1&tablesorter插件 - 我有一个货币列,有千位分隔符和值,如$ 52.00 $ 26.70 $ 100.00 $ 50.00 $ 1,002.00 $ 1,102.00.当我尝试按以下方式排序时,
$1,002.00
$1,102.00
$26.70
$50.00
$52.00
$100.00
Run Code Online (Sandbox Code Playgroud)
需要像,
$26.70
$50.00
$52.00
$100.00
$1,002.00
$1,102.00
Run Code Online (Sandbox Code Playgroud)
试过这里提到的很多解决方案,但没有成功.
Bee*_*oot 31
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'
}
}
});
});
Run Code Online (Sandbox Code Playgroud)
您可能需要调整格式函数,我没有测试过.
Owe*_*wen 24
如果您只想修复货币数量(最快):
<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>
Run Code Online (Sandbox Code Playgroud)
我不喜欢StackOverflow上的其他3个其他提议的解决方案:
Owe*_*wen 15
如果要修复所有数据类型(最灵活):
<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>
Run Code Online (Sandbox Code Playgroud)
这具有将显示数据与排序数据分离的优点,更可重复使用.