[英]Filter datatable with date range
I want to integrate datarange picker plugin into datatables and I write: 我想将数据范围选择器插件集成到数据表中,并编写:
$(document).ready(function() {
$(function() {
var start = moment("2017-01-01 12:34:16");
var end = moment("2018-03-03 10:08:07");
function cb(start, end) {
$('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
}
$('#reportrange').daterangepicker({
startDate: start,
endDate: end,
ranges: {
'Today': [moment(), moment()],
'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
'Last 7 Days': [moment().subtract(6, 'days'), moment()],
'Last 30 Days': [moment().subtract(29, 'days'), moment()],
'This Month': [moment().startOf('month'), moment().endOf('month')],
'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
}
}, cb);
cb(start, end);
});
$('#reportrange').on('apply.daterangepicker', function(ev, picker) {
var start = picker.startDate.format('YYYY-MM-DD');
var end = picker.endDate.format('YYYY-MM-DD');
$.fn.dataTable.ext.search.push(
function(settings, data, dataIndex) {
var min = start;
var max = end;
var startDate = new Date(data[1]);
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;
}
);
table.draw();
});
var dataSet = [
['2093',
'Feb 23, 2018',
'asd asd ',
'asd@hotmail.com',
'£ 50',
'£0.00',
"Feb 23, 2019",
],
['2092',
'Feb 23, 2018',
'asddd asddd',
'dddd@hotmail.com',
'£ 50',
'£0.00',
"Feb 23, 2019",
],
['2050',
'Feb 20, 2018',
'Angus Fret',
'angus@fgf.co.uk',
'£ 50',
'£0.00',
"Feb 20, 2019",
],
['2048',
'Feb 19, 2018',
'John Smith',
'john@smith.com',
'£ 50',
'£0.00',
"Feb 19, 2019",
],
['2046',
'Feb 19, 2018',
'Ana Ana',
'ana@talktalk.net',
'£ 50',
'£0.00',
"Feb 19, 2019",
],
['2045',
'Feb 19, 2018',
'Ray N',
'rayn@nn.com',
'£ 50',
'£0.00',
"Feb 19, 2019",
],
['2044',
'Feb 16, 2018',
'Paul N',
'paul@gmail.com',
'£ 200',
'£0.00',
"Feb 16, 2019",
],
['2042',
'Feb 13, 2018',
'Bradley New',
'new-marden@hotmail.com',
'£ 100',
'£0.00',
"Feb 13, 2019",
],
['2012',
'Jan 27, 2018',
'Mark Zuckenberg',
'markzeg@me.com',
'£ 150',
'£0.00',
"Jan 27, 2019",
],
];
var table = $('#example').DataTable({
"order": [
[0, "desc"]
],
lengthChange: false,
data: dataSet,
columns: [{
title: "ORDER ID"
},
{
title: "ORDER DATE"
},
{
title: "PURCHASED BY"
},
{
title: "RECIPIENT"
},
{
title: "ORDER TOTAL"
},
{
title: "VAT"
},
{
title: "EXPIRY"
}
]
});
});
and as you can see I use a search datatable API function to compare dates and to get data between dates (min, max) 如您所见,我使用搜索数据表API函数比较日期并获取日期之间的数据(最小值,最大值)
What is a problem? 怎么了
When I try to draw the table with new filtered data only with dates between min and max date I get 0 rows from datatable plugin. 当我尝试使用仅具有最小和最大日期之间日期的新过滤数据绘制表时,我从datatable插件中获得了0行。 WHy?
为什么?
Here is the fiddle: https://jsfiddle.net/ankepv5v/16/ 这是小提琴: https : //jsfiddle.net/ankepv5v/16/
What is wrong? 怎么了? I also don't see any error in my code... please help
我的代码中也没有看到任何错误...请帮助
How I can solve my problem? 我该如何解决我的问题? WHy table retunr me 0 results ?
为什么表可以让我0个结果?
The main issue is your date comparisons 主要问题是您的日期比较
var start = picker.startDate.format('YYYY-MM-DD');
var end = picker.endDate.format('YYYY-MM-DD');
Should be: 应该:
var start = picker.startDate;
var end = picker.endDate;
Then you also end up with a problem that your data is getting removed completely out of the search when you filter so you need this after you draw your table 然后,您还遇到一个问题,即在进行过滤时,数据将完全从搜索中删除,因此在绘制表格后需要
$.fn.dataTable.ext.search.pop();
https://jsfiddle.net/ankepv5v/79/ https://jsfiddle.net/ankepv5v/79/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.