How to filter by a period
How to filter by a period
brunofds_90
Posts: 1Questions: 1Answers: 0
Hi.
I have four columns and and the second colum is the dates of data. I'm trying to filter choosing a min date and a max date, but every time i did It, the table don´t appear anymore.
Code without filter
$(document).ready(function()
{
$('#loading_image').hide();
$('#datatable').show();
$('#datatable').DataTable();
});
Code with filter
$(function() {
var oTable = $('#datatable').DataTable({
"oLanguage": {
"sSearch": "Filter Data"
},
"iDisplayLength": -1,
"sPaginationType": "full_numbers",
});
$("#datepicker_from").datepicker({
showOn: "button",
buttonImage: "images/calendar.gif",
buttonImageOnly: false,
"onSelect": function(date) {
minDateFilter = new Date(date).getTime();
oTable.fnDraw();
}
}).keyup(function() {
minDateFilter = new Date(this.value).getTime();
oTable.fnDraw();
});
$("#datepicker_to").datepicker({
showOn: "button",
buttonImage: "images/calendar.gif",
buttonImageOnly: false,
"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;
}
);
This discussion has been closed.
Answers
Hi @brunofds_90 ,
This example here should help, it's searching between a range (though numbers in this case).
If that doesn't get you going, we're happy to take a look if you could link to a running test case. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.
Cheers,
Colin