我正在使用jQuery DataTables 1.10.4渲染一个表,每个表的行都有关于文件的信息。
该表中的一列显示文件大小。呈现后,我希望该列读取NNN KB
(带有“ KB”后缀)。我还希望用户能够对文件大小列进行数字排序。
但是,虽然我的files
数据数组只有数字指示文件的大小(以字节为单位),但排序功能似乎使用的是字符串的渲染值,我得到的是字符串排序,而不是数字。
有没有一种简单的方法来声明列的类型,使排序为数字?即进行排序,我希望jQuery DataTables使用files数组中的值。
如果不能仅使用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();
}
});
解决方法很简单,当显示数据(type === 'display'
)时,返回格式化的字符串,否则返回要排序的数据。从手册:
请求的呼叫类型的数据-这将是
'filter'
,'display'
,'type'
或'sort'
。
var sortable_size = function(data, type, full, meta) {
if(type === 'display'){
return Math.floor(full[2]/1024) + " KB";
} esle {
return data;
}
};
有关更多信息,请参见columns.render。
如果您没有以字节为单位存储文件大小,则解决方案是使用“文件大小排序”插件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句