简体   繁体   English

jQuery数据表:带逗号的排序编号不起作用

[英]Jquery datatables: sort number with comma doesn't work

I am trying to sort a column with numerical numbers with comma (,). 我正在尝试用逗号(,)对带有数字的列进行排序。

I am getting a wrong result using the num-fmt option: 我使用num-fmt选项得到了错误的结果:

在此处输入图片说明

Here is my code : 这是我的代码:

$('#test').DataTable({
    columnDefs: [
         { targets: 4, type: 'num-fmt' }
    ]
});

Use numeric-comma plugin to sort numbers correctly which use a comma as the decimal place. 使用数字逗号插件正确地对数字进行排序,这些数字使用逗号作为小数位。

Either include //cdn.datatables.net/plug-ins/1.10.11/sorting/numeric-comma.js or use it inline as shown below: 包括//cdn.datatables.net/plug-ins/1.10.11/sorting/numeric-comma.js或内联使用,如下所示:

$.extend( $.fn.dataTableExt.oSort, {
    "numeric-comma-pre": function ( a ) {
        var x = (a == "-") ? 0 : a.replace( /,/, "." );
        return parseFloat( x );
    },

    "numeric-comma-asc": function ( a, b ) {
        return ((a < b) ? -1 : ((a > b) ? 1 : 0));
    },

    "numeric-comma-desc": function ( a, b ) {
        return ((a < b) ? 1 : ((a > b) ? -1 : 0));
    }
} );

$('#test').DataTable({
    columnDefs: [
         { targets: 4, type: 'numeric-comma' }
    ]
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM