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 

需要像,

  $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' } } }); }); 

您可能需要调整格式函数,我没有测试过。

如果您只想修复货币编号(最快):

  £80,000.00 

我不喜欢StackOverflow上的其他3个其他提议的解决方案:

  1. ‘使用自定义解析器并在表中应用sort init’ – 对于大量表不可重用
  2. ‘使用自定义解析器并应用表格单元格的类’ – 脏标记
  3. ‘修复源中的TableSorters货币排序’ – 为将来的升级而烦恼

如果要修复所有数据类型(最灵活):

  01 Apr 2008 Alice £80.00 

这具有将显示数据与排序数据分离的优点,更可重复使用。

遵循@Ownen提出的相同想法,自TableSorter v2.16.0起,您可以直接使用data-text属性,而无需声明自己的textExtraction函数( 此处有更多信息):

 01 Apr 2008 Alice £80.00 

此属性也适用于其他小部件,如数学 。

注意:为了使其与输出窗口小部件一起使用 ,您需要声明output_dataAttrib选项:

 $('#table').tablesorter({ widgets: ["output"], widgetOptions : { output_dataAttrib: 'data-text' } });