Reputation: 79
I'm trying to get bootstrap datetimepicker to work in conjunction with Datatables so that it filters the datatable when a date is picked. Problem is, it just shows up as an input bar on my page, there is no calendar or calendar symbol. It sounds like a CDN/library reference issue but I've double checked and seem to have them all loaded:
. Jquery . moment.js . Bootstrap . bootstrap-datetimepicker.min.js . bootstrap-datetimepicker.min.css
I've made sure Moment is loaded before bootstrap and jquery. I believe that is all the references I need?Any help would be much appreciated. Thanks
<div class="container">
<div class='col-md-4'>
<div class="form-group">
<div class='input-group date' id='from'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
<div class='col-md-4'>
<div class="form-group">
<div class='input-group date' id='to'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
</div>
<script>
$(function () {
$('#from').datetimepicker({
format: 'DD MMMM YYYY',
extraFormats: ['DD MM YYYY', 'DD.MM.YYYY',
'DD/MM/YYYY', 'DD MMM YYYY', 'DD.MMM.YYYY', 'DD/MMM/YYYY', 'DD MMMM YYYY',
'DD.MMMM.YYYY', 'DD/MMMM/YYYY']
}).on('dp.change', function (e) {
UpdateDataCount();
});
$('#to').datetimepicker({
format: 'DD MMMM YYYY',
extraFormats: ['DD MM YYYY', 'DD.MM.YYYY',
'DD/MM/YYYY', 'DD MMM YYYY', 'DD.MMM.YYYY', 'DD/MMM/YYYY', 'DD MMMM YYYY',
'DD.MMMM.YYYY', 'DD/MMMM/YYYY']
}).on('dp.change', function (e) {
UpdateDataCount();
});
});
UpdateDataCount: function () {
$.fn.dataTable.ext.search.push(
function (settings, data, dataIndex) {
var from = $('#from').datetimepicker("getDate");
var to = $('#to').datetimepicker("getDate");
//data being searched
var startDate = new Date(data[2]);
//if true show row/ if not, don't
if (from == null && to == null) { return true; }
if (from == null && startDate <= to) { return true; }
if (to == null && startDate >= from) { return true; }
if (startDate <= to && startDate >= from) { return true; }
return false;
}
);
}
</script>
Upvotes: 0
Views: 820
Reputation: 79
To answer my own question, datetimepicker is not compatible with bootstrap 4
Upvotes: 1
Reputation: 5699
I'd be interested to see what your console is doing but I don't think you want to be repeatedly pushing a function, rather invoking it on change...
I got it working and there were issues with comparing moment Dates with vanilla Dates:
var table = $('#example').DataTable();
var from = null;
var to = null;
$('#from').datetimepicker({
format: 'DD MMMM YYYY',
extraFormats: ['DD MM YYYY', 'DD.MM.YYYY',
'DD/MM/YYYY', 'DD MMM YYYY', 'DD.MMM.YYYY', 'DD/MMM/YYYY', 'DD MMMM YYYY',
'DD.MMMM.YYYY', 'DD/MMMM/YYYY'
]
}).on('dp.change', function(e) {
from = e.date;
table.draw();
});
$('#to').datetimepicker({
format: 'DD MMMM YYYY',
extraFormats: ['DD MM YYYY', 'DD.MM.YYYY',
'DD/MM/YYYY', 'DD MMM YYYY', 'DD.MMM.YYYY', 'DD/MMM/YYYY', 'DD MMMM YYYY',
'DD.MMMM.YYYY', 'DD/MMMM/YYYY'
]
}).on('dp.change', function(e) {
to = e.date;
table.draw();
});
$.fn.dataTable.ext.search.push(
function(settings, data, dataIndex) {
//data being searched
var startDate = moment(data[2], "DD/MM/YYYY");
if (!from && !to) {
return true;
}
if (!from && startDate.isSameOrBefore(to, "day")) {
return true;
}
if (!to && startDate.isSameOrAfter(from, "day")) {
return true;
}
if (startDate.isSameOrBefore(to, "day") && startDate.isSameOrAfter(from, "day")) {
return true;
}
return false;
}
);
Working JSFiddle here.
Hope that helps.
Upvotes: 0