jQuery tablesorter - 不对具有格式化货币值的列进行排序

标签 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 允许您为此类内容定义“custom parsers ”。

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

您可能需要调整格式功能,我尚未测试过。

关于jQuery tablesorter - 不对具有格式化货币值的列进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9027438/

相关文章:

jquery - Bootstrap 日期选择器 XPages

jquery - 如何在 JQuery 中加载 AJAX 内容后触发事件处理程序

Javascript 将参数传递给函数内部变量

javascript - 我可以暂时覆盖事件监听器吗?

jquery - [quicksearch] 如何确定显示的行数?

带页码的tablesorter pager (v 2.10)

javascript - 表排序器和搜索

javascript - 追加后如何对动态表数据进行排序

javascript - 如何获取Z-index的数值

javascript - 在 Jquery 中获取 Ajax 返回的值以及 html 标记