简体   繁体   English

如何对 jQuery DataTables 中的数字列进行排序

[英]How to sort on a numeric column in jQuery DataTables

(Looked at a bunch of answers on this topic, none of which applies to this question.) (看了一堆关于这个话题的答案,没有一个适用于这个问题。)

The DataTables have the feature of letting the user click on each column's Up/Down triangle icons to sort in Ascending or Descending order.数据表具有让用户单击每列的向上/向下三角形图标以按升序或降序排序的功能。 I have loaded the data as follows我已经加载了如下数据

        oTable.fnAddData( ["Bogus data","1,541,512","12.5%","0","0","0"]);
        oTable.fnAddData( ["Bogus data","3,541,512","2.5%","0","0","0"]);
        oTable.fnAddData( ["Bogus data","541,512","1.5%","0","0","0"]);
        oTable.fnAddData( ["Bogus data","2,541,512","32.5%","0","0","0"]);
        oTable.fnAddData( ["Bogus data","741,512","3.5%","0","0","0"]);
        oTable.fnAddData( ["A Bogus data","41,512","1.5%","0","0","0"]);
        oTable.fnAddData( ["Bogus data","2,541,512","12.5%","0","0","0"]);
        oTable.fnAddData( ["Z Bogus data","1,541,512","12.5%","0","0","0"]);
        oTable.fnAddData( ["Bogus data","3,541,512","2.5%","0","0","0"]);
        oTable.fnAddData( ["La Bogus data","541,512","1.5%","0","0","0"]);
        oTable.fnAddData( ["The Bogus data","2,541,512","32.5%","0","0","0"]);
        oTable.fnAddData( ["Bogus data","741,512","3.5%","0","0","0"]);
        oTable.fnAddData( ["Bogus data","41,512","1.5%","0","0","0"]);
        oTable.fnAddData( ["Bogus data","2,541,512","12.5%","0","0","0"]);
        oTable.fnAddData( ["Bogus data","1,541,512","12.5%","0","0","0"]);
        oTable.fnAddData( ["Bogus data","3,541,512","2.5%","0","0","0"]);
        oTable.fnAddData( ["Bogus data","541,512","1.5%","0","0","0"]);
        oTable.fnAddData( ["Bogus data","2,541,512","32.5%","0","0","0"]);
        oTable.fnAddData( ["Bogus data","741,512","3.5%","0","0","0"]);
        oTable.fnAddData( ["Bogus data","41,512","1.5%","0","0","0"]);

In column number 2 the numeric values are processed alphabetically when I click on the Up/Down triangles.在第 2 列中,当我单击向上/向下三角形时,将按字母顺序处理数值。 How can I adjust it so that the second column Up/Down arrows will trigger the proper way, treating the characters as numbers.如何调整它以便第二列向上/向下箭头将触发正确的方式,将字符视为数字。 I tried using the following initialization:我尝试使用以下初始化:

oTable = $('.utable').dataTable( {
"aoColumns": [{ sWidth: '60%' },{sWidth: '30%', "sType": "numeric"},{ sWidth: '10%' }],
"sDom": 'rt',
"sScrollY":"200px",
"bPaginate":false,
"bFilter":false,
"bInfo": false});  

All this does is to lock the colum and the Up/Down icons will not work in the header of that column.所有这一切都是为了锁定列,并且向上/向下图标在该列的标题中不起作用。

Your problem is that the numbers are not being recognized as such and even if they were, the commas in your numbers would probably the sorting function off (because it will probably not properly remove them).您的问题是数字没有被识别,即使它们被识别,数字中的逗号也可能会关闭排序功能(因为它可能无法正确删除它们)。

One option you have is to implement your own sorting function which deals with your numbers properly.您拥有的一种选择是实现您自己的排序功能,该功能可以正确处理您的数字。 Here's an example that does what you need:这是一个可以满足您需要的示例:

http://live.datatables.net/oborug/2/edit http://live.datatables.net/oborug/2/edit

PS -- Here's the relevant documentation: http://datatables.net/development/sorting PS - 这是相关文档: http : //datatables.net/development/sorting

Technically column number two contains strings (1,000) - numbers with a comma and so does column 3 - numbers with percentages).从技术上讲,第二列包含字符串 (1,000) - 带逗号的数字,第 3 列也是如此 - 带百分比的数字)。 Best thing for you to do is to pass the data to data tables as an integer (without commas and %) and write a custom formatter to add commas and percentages using mRender option (read about it at http://www.datatables.net/usage/columns ).您最好做的事情是将数据作为整数(不带逗号和 %)传递到数据表,并编写自定义格式化程序以使用mRender选项添加逗号和百分比(在http://www.datatables.net 上阅读) /用法/列)。

If you add custom formatting to your data also do not forget to set the option to use underlying data as your sort source rather than the displayed data.如果您向数据添加自定义格式,也不要忘记设置使用基础数据作为排序源而不是显示数据的选项。

您必须定义排序函数(尝试使用按选择排序或按插入排序)尝试将数据变量存储在数组中并进行计算,否则您可以直接对数据进行排序

Hello I made this using parseFloat and replace methods and using this example http://datatables.net/release-datatables/examples/basic_init/multi_col_sort.html您好,我使用parseFloat和替换方法并使用此示例进行了此操作http://datatables.net/release-datatables/examples/basic_init/multi_col_sort.html

jQuery.fn.dataTableExt.oSort["string-nbr-asc"]  = function(x,y) {return ((parseFloat(x.replace(",","")) < parseFloat(y.replace(",",""))) ? -1 : ((parseFloat(x.replace(",","")) > parseFloat(y.replace(",",""))) ?  1 : 0));};

jQuery.fn.dataTableExt.oSort["string-nbr-desc"] = function(x,y) {return ((parseFloat(x.replace(",","")) < parseFloat(y.replace(",",""))) ?  1 : ((parseFloat(x.replace(",","")) > parseFloat(y.replace(",",""))) ? -1 : 0));};

If you have 10 columns and want to sort 7,8,9 wich are numbers like 7,081 1,925.49 use如果您有 10 列并且想要sort 7,8,9进行sort例如7,081 1,925.49类的7,081 1,925.49使用

"aoColumns":[null,null,null,null,null,null,{ "sType": "string-nbr" },{ "sType": "string-nbr" },{ "sType": "string-nbr" },null]

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

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