我有一个jquery数据表,它有日期过滤器。我的代码看起来像下面提到的plunker链接中提到的那样。为DatePickers编写我的JS代码
$(function() {
var oTable=$('#datatable').DataTable({
"oLanguage": {
"sSearch": "Filter Data"
},
"iDisplayLength": -1,
"sPaginationType": "full_numbers",
});
$('#datepicker_from').click(function() {
$("#datepicker_from").datepicker("show");
});
$('#datepicker_to').click(function() {
$("#datepicker_to").datepicker("show");
});
$("#datepicker_from").datepicker({
"onSelect": function(date) {
minDateFilter = new Date(date).getTime();
oTable.fnDraw();
}
}).keyup(function() {
minDateFilter = new Date(this.value).getTime();
oTable.fnDraw();
});
$("#datepicker_to").datepicker({
"onSelect": function(date) {
maxDateFilter = new Date(date).getTime();
oTable.fnDraw();
}
}).keyup(function() {
maxDateFilter = new Date(this.value).getTime();
oTable.fnDraw();
});
});
// Date range filter
minDateFilter = "";
maxDateFilter = "";
$.fn.dataTableExt.afnFiltering.push(
function(oSettings, aData, iDataIndex) {
if (typeof aData._date == 'undefined') {
aData._date = new Date(aData[0]).getTime();
}
if (minDateFilter && !isNaN(minDateFilter)) {
if (aData._date < minDateFilter) {
return false;
}
}
if (maxDateFilter && !isNaN(maxDateFilter)) {
if (aData._date > maxDateFilter) {
return false;
}
}
return true;
}
);
http://plnkr.co/edit/yTAmQRJHN1zVdzYJBKop?p=preview
From和To Date Range Filter似乎工作正常。但是这些 我可能有一个单选按钮(即:本周和本月) 本周单选按钮将在今天日期和上周之间给出结果,并且月份单选按钮也是如此。 我该怎么解决这个问题。?
答案 0 :(得分:1)
在date_filter之后添加单选按钮:
<p id="radio_date_filter">
<input type="radio" name="filterTable" value="week" />This Week
<input type="radio" name="filterTable" value="month"/>This Month
</p>
根据选择编写过滤数据表的脚本:
$("#radio_date_filter input:radio").click(function(){
var currentDate = new Date();
if($(this).val() == 'week'){
var weekDate = new Date();
var first = weekDate.getDate() - 7;
var firstDayofWeek = new Date(weekDate.setDate(first));
minDateFilter = firstDayofWeek.getTime();
}else{
var monthDate = new Date();
var firstDayOfMonth = new Date(monthDate.setMonth(monthDate.getMonth() - 1));
minDateFilter = firstDayOfMonth.getTime();
}
maxDateFilter = currentDate.getTime();
oTable.fnDraw();
});