【发布时间】:2014-11-14 16:39:23
【问题描述】:
所以这个问题已经被问到here,但是这个解决方案对我不起作用(我可能做错了什么)。我想按字母顺序(“type”:“natural”)对表格进行排序,但我希望空单元格位于底部(对于 desc 和 asc)。
我尝试了 fbas 之前给出的解决方案:
jQuery.fn.dataTableExt.oSort['mystring-asc'] = function(x,y) {
var retVal;
x = $.trim(x);
y = $.trim(y);
if (x==y) retVal= 0;
else if (x == "" || x == " ") retVal= 1;
else if (y == "" || y == " ") retVal= -1;
else if (x > y) retVal= 1;
else retVal = -1; // <- this was missing in version 1
return retVal;
}
jQuery.fn.dataTableExt.oSort['mystring-desc'] = function(y,x) {
var retVal;
x = $.trim(x);
y = $.trim(y);
if (x==y) retVal= 0;
else if (x == "" || x == " ") retVal= -1;
else if (y == "" || y == " ") retVal= 1;
else if (x > y) retVal= 1;
else retVal = -1; // <- this was missing in version 1
return retVal;
}
与:
$(document).ready(function() {
$('#classement').dataTable({
"aoColumns": [
null,
null,
{ "type" : "mystring" },
{ "type" : "mystring" },
null
]
} );
} );
像| N° | Edit | Song | Singer | Url | 这样的表
仅按歌曲和歌手排序。
空单元格位于底部(如预期的那样),但现在排序没有逻辑(没有字母顺序,我应该在 dataTable 中使用另一个属性吗?)。
有人有解决办法吗?
编辑:如果我们动态添加一行,如何刷新排序?
$("#example").find('tbody')
.append($('<tr>')
.append($('<td>')
.text('Boro')
)
);
【问题讨论】:
-
把代码放在jsfiddle上就好了
标签: jquery sorting datatables emptydatatext