Skip to content

Instantly share code, notes, and snippets.

@bUxEE
Created May 5, 2018 22:50
Show Gist options
  • Save bUxEE/3e40d40c4a490db3e0520db309d02163 to your computer and use it in GitHub Desktop.
Save bUxEE/3e40d40c4a490db3e0520db309d02163 to your computer and use it in GitHub Desktop.
var pttable = $( '.dataTable' ).dataTable().api();
$('.posts-table-controls').first().append(
'<div class="datepicker-datatables" id="date_filter">' +
'<span id="date-label-from" class="date-label">Dal: </span><input class="date_range_filter date" type="text" id="datepicker_from" />' +
'<span id="date-label-to" class="date-label">Al: <input class="date_range_filter date" type="text" id="datepicker_to" />' +
'</div>'
);
$.fn.dataTable.ext.search.push(
function (settings, data, dataIndex) {
var min = $('#datepicker_from').datepicker("getDate");
var max = $('#datepicker_to').datepicker("getDate");
var date = moment(data[1], 'DD/MM/YYYY').format('YYYY-MM-DD');
var startDate = new Date(date);
if (min == null && max == null) { return true; }
if (min == null && startDate <= max) { return true;}
if(max == null && startDate >= min) {return true;}
if (startDate <= max && startDate >= min) { return true; }
return false;
}
);
$("#datepicker_from").datepicker({ onSelect: function () { pttable.draw(); }, changeMonth: true, changeYear: true , dateFormat:"dd-mm-yy"});
$("#datepicker_to").datepicker({ onSelect: function () { pttable.draw(); }, changeMonth: true, changeYear: true, dateFormat:"dd-mm-yy" });
// Event listener to the two range filtering inputs to redraw on input
$('#min, #max').change(function () {
pttable.draw();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment