从这里开始:http: //datatables.net/development/sorting#type_based
要向 DataTables 添加新的排序函数,您需要将函数附加到对象 $.fn.dataTableExt.oSort。例如,以下添加了“string-case”类型的区分大小写的排序函数:
jQuery.fn.dataTableExt.oSort['date-range-asc'] = function(x,y) {
//x = "MM/dd/yyyy - MM/dd/yyyy";
var regex = /(\d{2}\/\d{2}\/\d{4})/,
matchesX = regex.exec(x),
matchesY = regex.exec(y);
//for example, only sort on first date..
x = Date.parse(matchesX[0]);
y = Date.parse(matchesY[0]);
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
上面的代码可能被破坏了......但它应该让你走上正轨
mtmurdock 的解决方案
将此代码添加到 site.js(或在数据表之后导入的任何位置):
jQuery.extend(jQuery.fn.dataTableExt.oSort, {
"my-date-range-pre": function (a) {
var ukDatea = a.split('/');
var year = $.trim(ukDatea[2].split('-')[0]);
var month = $.trim(ukDatea[0]);
var day = $.trim(ukDatea[1]);
if (month.length == 1)
month = "0" + month;
if (day.length == 1)
day = "0" + day;
return (year + month + day) * 1;
},
"my-date-range-asc": function (a, b) {
var result = ((a < b) ? -1 : ((a > b) ? 1 : 0));
return result;
},
"my-date-range-desc": function (a, b) {
var result = ((a < b) ? 1 : ((a > b) ? -1 : 0));
return result;
}
});
然后在你的表定义上设置 aoColumnDefs 像这样:
{ "sType": "my-date-range", "aTargets": [0] }