问题描述
我正在使用数据表和 jQuery 来制作漂亮的可排序表.我现在想对行进行排序 a 值.该值是一个数值.但它也可能不可用,所以到时候我会回显一个破折号.
I'm using datatables and jQuery for making nice sortable tables. I now want to sort the rows an a value. This value is a numeric value. But it can also be not available, so at that point I will echo a dash.
当我现在对此列进行排序时,所有带破折号的行都在顶部.然后显示值为 1、3、6、8、10 的行.如何更改此设置以使短划线 (-) 始终位于表格底部?
When I now sort this column, all rows with the dash are on top. And than the rows with value 1, 3, 6, 8, 10 are shown. How do I change this so that the dash (-) are always on bottom of the table?
目前我输入了最大数量,是什么将它们放在了底部.但是,我不希望向用户显示此值.所以我需要一个隐藏的排序列,或者其他排序方法.
At the moment I put in a maximum number, what puts them on the bottom. However I don't want this value to be shown to the user. So I need a hidden sort column, or a other sorting method.
提前致谢!
推荐答案
表格中的第一列像普通列一样工作;第二列按您的要求工作.
The first column in the table works like a normal column; the second column works like you ask.
尝试自定义排序,如下所示:
Try custom sorting, something like this:
$.fn.dataTableExt.oSort['nullable-asc'] = function(a,b) {
if (a == '-')
return 1;
else if (b == '-')
return -1;
else
{
var ia = parseInt(a);
var ib = parseInt(b);
return (ia<ib) ? -1 : ((ia > ib) ? 1 : 0);
}
}
$.fn.dataTableExt.oSort['nullable-desc'] = function(a,b) {
if (a == '-')
return 1;
else if (b == '-')
return -1;
else
{
var ia = parseInt(a);
var ib = parseInt(b);
return (ia>ib) ? -1 : ((ia < ib) ? 1 : 0);
}
}
$('#table').dataTable( {
"bPaginate": false,
"bFilter": false,
"aoColumns": [
null,
{"bSortable": true, "sType": "nullable"}
],
} );
这篇关于jquery数据表排序忽略空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!