我正在使用jQuery YADCF插件过滤出一列格式化为mm的日期。插件的工作原理非常出色,只是它只显示了日期上的精确匹配。如果确切的排在桌子上的话,它会显示给我2016年03年/06/2016,但我真的希望它能显示2016年06月03/06和之后的所有日期。
我一直在尝试用DataTables手动完成这个任务,而且没有一个真正的工作或文档良好的jQuery DataTables版本,可以进行“大于等于”的日期搜索。
是否有人用YADCF (或DataTables单人)成功地做到了这一点?
发布于 2016-04-04 20:34:50
这里我的用例是YADCF的一个边缘案例,所以我重新使用了来自http://codepen.io/markmichon/pen/tmeGD的一些代码来使我的过滤器工作。这次我没有用YADCF,但很高兴知道它在外面。大吼大吼丹尼尔,插件作者,对我的回应!!
// converts date strings to a Date object, then normalized into a YYYYMMMDD format (ex: 20131220). Makes comparing dates easier. ex: 20131220 > 20121220
var normalizeDate = function(dateString) {
var date = new Date(dateString);
var normalized = date.getFullYear() + '' + (("0" + (date.getMonth() + 1)).slice(-2)) + '' + ("0" + date.getDate()).slice(-2);
return normalized;
}
var filterByDate = function(column, vesselDate) {
// Custom filter syntax requires pushing the new filter to the global filter array
$.fn.dataTableExt.afnFiltering.push(
function( oSettings, aData, iDataIndex ) {
var rowDate = (aData[column]).replace(/<(?:.|\n)*?>/gm, '').match(/[0-9][0-9]\/[0-3][0-9]\/[0-9][0-9][0-9][0-9]/), // take my mm-dd-yyyy – timestamp format wrapped in hTML and strip it down to the mm-dd-yyy only
start = normalizeDate(vesselDate); // use a normalized date (see code below)
rowDate = normalizeDate(rowDate);
// If our date from the row is between the start and end
if (start <= rowDate) {
return true;
} else if (rowDate >= start && start !== ''){
return true;
} else {
return false;
}
}
);
};
$('#dateStart').on('change keyup', function(){
dateStart = $(this).val();
filterByDate(3, dateStart); //passing the column value to the function
$vesselSchedule.draw(); //updating the table
});
$('#dateEnd').on('change keyup', function(){
dateEnd = $(this).val();
filterByDate(4, dateEnd);
$vesselSchedule.draw();
});https://www.nwseaportalliance.com/operations/vessels#/是一个活的例子。
https://stackoverflow.com/questions/36272076
复制相似问题