简体   繁体   English

数据表...排序不适用于数字逗号...为什么?

[英]datatables... Sort don't work with numeric-comma... Why?

I use the jquery datatables with server_processing.我使用带有 server_processing 的 jquery 数据表。 This reminds me server_processing in datatables numbers (or words?) with the ',' separator for decimal.这让我想起了 datatables 数字(或单词?)中的 server_processing,使用 ',' 分隔小数。

$ inter = trim ($ aRow ['carat']);
$ row [] = number_format ($ inter, 2, ',', '');

When I click on the arrows ascending and descending, sorting does not occur in the numbers method, but the method texts.当我单击升序和降序箭头时,数字方法中不会发生排序,而是方法文本中发生排序。 ex.前任。 10.01> 9.99 (in numbers) 10.01> 9.99(数字)

How make it work with my numbers?如何让它与我的号码一起使用?

In JavaScript only dots are valid as decimal separator.在 JavaScript 中,只有点作为小数分隔符有效。 As DataTables uses JavaScript to sort columns values, it will consider number with comma separator as strings.由于 DataTables 使用 JavaScript 对列值进行排序,它会将带有逗号分隔符的数字视为字符串。 However, this problem can be easily overtaken with a sort plug-in.但是,使用排序插件可以轻松解决这个问题。

jQuery.extend(jQuery.fn.dataTableExt.oSort, {
    "numeric-comma-pre": function (a) {
        // prepare number
        a = +(a.replace(",", "."));
        a = (isNaN(a)) ? Number.MAX_VALUE : a;
        return a;
    },
    "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));
    }
});

The previous code block defines a new data type, to be used by DataTables to sort columns.前面的代码块定义了一种新的数据类型,DataTables 使用它来对列进行排序。 This code is an adaptation of an example that can be found in DataTables official site .此代码改编自DataTables 官方网站中的示例。

The first function in the plug-in object, is the one that converts the cell value to number, so that it can be sorted by the following functions.插件对象中的第一个函数,是将单元格值转换为数字的函数,以便可以通过以下函数对其进行排序。 In this case I followed a suggestion from this post , and used the unary operator to convert the value to number.在这种情况下,我遵循了这篇文章的建议,并使用一元运算符将值转换为数字。 If the value is not a number then NaN is returned, and I change it to Number.MAX_VALUE.如果值不是数字,则返回 NaN,我将其更改为 Number.MAX_VALUE。 I do it this way because I choose to move invalid numbers last when sorting ascending.我这样做是因为我选择在升序排序时最后移动无效数字。

After that, only remains create the datatable, and define the new data type for the columns we want.之后,只剩下创建数据表,并为我们想要的列定义新的数据类型。

jQuery("#myTable").dataTable( {
    "aoColumnDefs": [
        { "sType": "numeric-comma", "aTargets": [2,3] }
    ]
});

Supposing that third and fourth columns have comma separated numbers, they now must be sorted correctly.假设第三和第四列有逗号分隔的数字,它们现在必须正确排序。

I tried the example of datatables page.我尝试了数据表页面的示例。 It works fine to me.它对我来说很好。

The data should be decimal format数据应为十进制格式

https://datatables.net/examples/plug-ins/sorting_sType.html https://datatables.net/examples/plug-ins/sorting_sType.html

$(document).ready(function() {

    jQuery.fn.dataTableExt.oSort['numeric-comma-asc'] = function(a, b) {
        var x = (a == "-") ? 0 : a.replace(/,/, ".");
        var y = (b == "-") ? 0 : b.replace(/,/, ".");
        x = parseFloat(x);
        y = parseFloat(y);
        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    };

    jQuery.fn.dataTableExt.oSort['numeric-comma-desc'] = function(a, b) {
        var x = (a == "-") ? 0 : a.replace(/,/, ".");
        var y = (b == "-") ? 0 : b.replace(/,/, ".");
        x = parseFloat(x);
        y = parseFloat(y);
        return ((x < y) ? 1 : ((x > y) ? -1 : 0));
    };

    oTable = $('#grid').dataTable({
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "aoColumns": [
            null,
            null,
            null, {
                "sType": "numeric-comma"
            },
            null,
            null
        ]
    });
});

Try inserting the following code on your head.尝试在您的头上插入以下代码。 Remeber to change the "example" for your datatable name.请记住更改数据表名称的“示例”。

$(document).ready(function() {
    $('#example').DataTable( {
        "language": {
            "decimal": ",",
            "thousands": "."
        }
    } );
} );

if you are doing server-side processing implementation of Datatables, you cannot use any sort plugins or sType variables, because they all reside client side如果您正在执行数据表的服务器端处理实现,则不能使用任何排序插件或 sType 变量,因为它们都驻留在客户端

You would need to update your database query to properly sort a column as a number/string, then use mRender or javascript to update the column format, so that the sort really has noting todo with the column format您需要更新数据库查询以将列正确排序为数字/字符串,然后使用 mRender 或 javascript 更新列格式,以便排序确实与列格式有关

example : returning money as a number from the database, then using mRender to add commas/currency types示例:从数据库中将钱作为数字返回,然后使用 mRender 添加逗号/货币类型

The latest dataTables version (1.10.4) handles formatted numeric sorting without any extension, that is, puts $1,000.00 before $900.00 when sorting in descending order and puts $900.00 before $1,000.00 when sorting in ascending order.最新的 dataTables 版本 (1.10.4) 处理格式化数字排序,没有任何扩展名,即按降序排序时将$1,000.00 $900.00放在$900.00之前,按升序排序时将 900.00 $1,000.00放在 1,000.00 美元之前。

See num-fmt in http://datatables.net/reference/option/columns.type .请参阅http://datatables.net/reference/option/columns.typenum-fmt

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

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