3

(看了一堆关于这个话题的答案,没有一个适用于这个问题。)

DataTables 具有让用户单击每列的向上/向下三角形图标以按升序或降序排序的功能。我已按如下方式加载数据

        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"]);

在第 2 列中,当我单击向上/向下三角形时,数值按字母顺序处理。如何调整它以便第二列向上/向下箭头将触发正确的方式,将字符视为数字。我尝试使用以下初始化:

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

所有这一切都是为了锁定列,并且向上/向下图标在该列的标题中不起作用。

4

4 回答 4

4

您的问题是数字没有被识别,即使它们是,数字中的逗号也可能会关闭排序功能(因为它可能无法正确删除它们)。

您拥有的一种选择是实现您自己的排序功能,以正确处理您的数字。这是一个满足您需要的示例:

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

PS——以下是相关文档:http ://datatables.net/development/sorting

于 2013-03-06T21:57:47.220 回答
2

从技术上讲,第 2 列包含字符串 (1,000) - 带逗号的数字,第 3 列也是如此 - 带百分比的数字)。最好的办法是将数据作为整数(不带逗号和 %)传递给数据表,并编写自定义格式化程序以使用mRender选项添加逗号和百分比(在http://www.datatables.net /用法/列)。

如果您向数据添加自定义格式,请不要忘记将选项设置为使用基础数据作为排序源而不是显示的数据。

于 2013-03-06T21:55:22.320 回答
0

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

于 2013-03-06T21:31:18.670 回答
0

您好,我使用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));};

如果您有 10 列并且想要使用sort 7,8,9类似的数字7,081 1,925.49

"aoColumns":[null,null,null,null,null,null,{ "sType": "string-nbr" },{ "sType": "string-nbr" },{ "sType": "string-nbr" },null]
于 2014-01-03T02:47:40.850 回答