如何使用不同的数据进行排序
How to use different data for sorting
我正在使用 jQuery DataTables 1.10.4 来呈现 table 每个行都有关于文件的一些信息。
此 table 中的一列显示了文件大小。呈现时,我希望该列显示为 NNN KB
(带有 "KB" 后缀)。我还希望用户能够对文件大小列进行数字排序。
然而,虽然我的 files
数据数组只有以字节为单位指示文件大小的数字,但排序功能似乎使用了字符串的呈现值,我得到的是字符串排序,而不是数字一。
有没有一种简单的方法来声明列的类型,以便按数字排序? IE。对于排序,我希望 jQuery DataTables 使用文件数组中的值。
如果这不能只用 columnDefs
规范声明,最容易使用的排序插件或函数是什么?
这是我目前的情况。
var files = {['name','dir',10240], .... }
var sortable_size = function(data, type, full, meta) {
return Math.floor(full[2]/1024) + " KB";
};
$('#files').dataTable({
data: files,
pagingType: 'simple',
columnDefs: [
{ targets:0, render:clickable_message },
{ targets:3, render:clickable_attachment },
{ targets:2, render:sortable_size, width:'100px', type:'num' }
],
// no width for col 0 here because it causes linewrap in data
// and size fields (attachment name can be fairly wide as well)
order:[[1, 'asc']], // col 1 (date), ascending
fnInitComplete: function() {
$('#attachments').fadeIn();
}
});
用我自己的排序类型解决了它:
$.fn.dataTableExt.oSort['sort-kb-asc'] = function(x,y) {
console.log('x =' + x + ' y = ' + y);
x = parseInt(x.substring(0, x.indexOf(' KB')));
y = parseInt(y.substring(0, y.indexOf(' KB')));
console.log('x =' + x + ' y = ' + y);
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
$.fn.dataTableExt.oSort['sort-kb-desc'] = function(x,y) { return -1 * $.fn.dataTableExt.oSort['sort-kb-asc'](x,y); }
在我的专栏 Defs 调用中:
{targets:2,render:sortable_size,width:'100px',type:'sort-kb',className: "dt-right"},
解决方法很简单,当显示数据时(type === 'display'
),return格式化字符串,否则return数据进行排序。来自 manual:
The type call data requested - this will be 'filter'
, 'display'
, 'type'
or 'sort'
.
var sortable_size = function(data, type, full, meta) {
if(type === 'display'){
return Math.floor(full[2]/1024) + " KB";
} esle {
return data;
}
};
有关详细信息,请参阅 columns.render。
如果您不想以字节为单位存储文件大小,解决方案是使用 File size sorting plug-in.
我正在使用 jQuery DataTables 1.10.4 来呈现 table 每个行都有关于文件的一些信息。
此 table 中的一列显示了文件大小。呈现时,我希望该列显示为 NNN KB
(带有 "KB" 后缀)。我还希望用户能够对文件大小列进行数字排序。
然而,虽然我的 files
数据数组只有以字节为单位指示文件大小的数字,但排序功能似乎使用了字符串的呈现值,我得到的是字符串排序,而不是数字一。
有没有一种简单的方法来声明列的类型,以便按数字排序? IE。对于排序,我希望 jQuery DataTables 使用文件数组中的值。
如果这不能只用 columnDefs
规范声明,最容易使用的排序插件或函数是什么?
这是我目前的情况。
var files = {['name','dir',10240], .... }
var sortable_size = function(data, type, full, meta) {
return Math.floor(full[2]/1024) + " KB";
};
$('#files').dataTable({
data: files,
pagingType: 'simple',
columnDefs: [
{ targets:0, render:clickable_message },
{ targets:3, render:clickable_attachment },
{ targets:2, render:sortable_size, width:'100px', type:'num' }
],
// no width for col 0 here because it causes linewrap in data
// and size fields (attachment name can be fairly wide as well)
order:[[1, 'asc']], // col 1 (date), ascending
fnInitComplete: function() {
$('#attachments').fadeIn();
}
});
用我自己的排序类型解决了它:
$.fn.dataTableExt.oSort['sort-kb-asc'] = function(x,y) {
console.log('x =' + x + ' y = ' + y);
x = parseInt(x.substring(0, x.indexOf(' KB')));
y = parseInt(y.substring(0, y.indexOf(' KB')));
console.log('x =' + x + ' y = ' + y);
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
$.fn.dataTableExt.oSort['sort-kb-desc'] = function(x,y) { return -1 * $.fn.dataTableExt.oSort['sort-kb-asc'](x,y); }
在我的专栏 Defs 调用中:
{targets:2,render:sortable_size,width:'100px',type:'sort-kb',className: "dt-right"},
解决方法很简单,当显示数据时(type === 'display'
),return格式化字符串,否则return数据进行排序。来自 manual:
The type call data requested - this will be
'filter'
,'display'
,'type'
or'sort'
.
var sortable_size = function(data, type, full, meta) {
if(type === 'display'){
return Math.floor(full[2]/1024) + " KB";
} esle {
return data;
}
};
有关详细信息,请参阅 columns.render。
如果您不想以字节为单位存储文件大小,解决方案是使用 File size sorting plug-in.