【问题标题】:jquery datatable sort plugins not working at alljquery数据表排序插件根本不起作用
【发布时间】:2014-05-05 12:48:04
【问题描述】:

我正在使用jquery数据表排序插件对货币进行排序。但是没有一个插件对我有用。

我在列中的数据类型为:

$5,871
$385.58
$430
$1,308.60

当列中没有 ','"$" 符号时,排序就可以了。其他明智的排序不符合预期。

示例:对我得到的输出进行排序(排序时)

$890.54
$5.49
$5,871
$2,548.50

输出应该在哪里:

$5,871
$2,548.50
$890.54
$5.49

我尝试过:numeric-comma、currency、title-numeric sort 来对这些值进行排序。

如果我从这些值中删除 $ 和 , 则它们的排序正确。

试过了:

  1. http://datatables.net/plug-ins/sorting/title-numeric
  2. http://datatables.net/plug-ins/sorting/formatted-numbers
  3. http://datatables.net/plug-ins/sorting/numeric-comma

排序后的输出总是和上面一样:

对这个问题还有一个调整:

对于我们没有任何货币的列,它表示为“---”

例子

$5,871
$2,548.50
$890.54
---
---
$5.49

【问题讨论】:

  • “对于我们没有任何货币的列,它表示为“---””是什么意思?是否有带有“---”的整列?

标签: javascript jquery datatable datatables


【解决方案1】:

使用以下代码。它也应该与“$”符号一起使用。您不需要删除 $ 符号。

 jQuery.extend(jQuery.fn.dataTableExt.oSort, {
            "currency-pre": function (a) {
                a = (a === "-") ? 0 : a.replace(/[^\d\-\.]/g, "");
                return parseFloat(a);
            },
            "currency-asc": function (a, b) {

                return a - b;
            },
            "currency-desc": function (a, b) {

                return b - a;
            }
    });

// Initialize datatable

$('#datatableID').dataTable({
            'aoColumns': [
                            { 'sType': 'currency' }],

【讨论】:

    【解决方案2】:

    为什么不构建自己的扩展,它真的很简单

    jQuery.extend(jQuery.fn.dataTableExt.oSort, {
                "currency-pre": function(a) {
                //EDIT: To Accommodate for the "---" columns, use this
                    return a.indexOf('--') > -1 ? -1 : a.replace(/\$|,/g, '').trim();
                },
                "currency-asc": function(a, b) {
                    return ((a < b) ? -1 : ((a > b) ? 1 : 0));
                },
                "currency-desc": function(a, b) {
                    return ((a < b) ? 1 : ((a > b) ? -1 : 0));
                });
    

    然后确保将 'sType' 设置为 'currency'

    【讨论】:

    • 这也行不通。我试图将正则表达式编辑为 /[\$\-,]/ 但没有运气。
    猜你喜欢
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    相关资源
    最近更新 更多