jQuery EasyUI 数据网格 - 自定义排序
如果默认的排序行为不满足您的需求,您可以自定义数据网格(datagrid)的排序行为。
最基础的,用户可以在列上定义一个排序函数,函数名是 sorter。这个函数将接受两个值,返回值将如下:
valueA > valueB => 返回 1
valueA < valueB => 返回 -1
自定义排序代码
<table id="tt"></table>
$(&qpos;#tt&qpos;).datagrid({
title:&qpos;Custom Sort&qpos;,
iconCls:&qpos;icon-ok&qpos;,
width:520,
height:250,
singleSelect:true,
remoteSort:false,
columns:[[
{field:&qpos;itemid&qpos;,title:&qpos;Item ID&qpos;,width:60,sortable:true},
{field:&qpos;listprice&qpos;,title:&qpos;List Price&qpos;,width:70,align:&qpos;right&qpos;,sortable:true},
{field:&qpos;unitcost&qpos;,title:&qpos;Unit Cost&qpos;,width:70,align:&qpos;right&qpos;,sortable:true},
{field:&qpos;attr1&qpos;,title:&qpos;Attribute&qpos;,width:120,sortable:true},
{field:&qpos;date&qpos;,title:&qpos;Date&qpos;,width:80,sortable:true,align:&qpos;center&qpos;,
sorter:function(a,b){
a = a.split(&qpos;/&qpos;);
b = b.split(&qpos;/&qpos;);
if (a[2] == b[2]){
if (a[0] == b[0]){
return (a[1]>b[1]?1:-1);
} else {
return (a[0]>b[0]?1:-1);
}
} else {
return (a[2]>b[2]?1:-1);
}
}
},
{field:&qpos;status&qpos;,title:&qpos;Status&qpos;,width:40,align:&qpos;center&qpos;}
]]
}).datagrid(&qpos;loadData&qpos;, data);
您可以从这段代码中看到,我们为 date 列创建了自定义的 sorter。日期的格式是 &qpos;dd/mm/yyyy&qpos;,可以轻松的按年月日排序。