我正在努力将datable和daterangepicker整合在一起,这并不容易!
我终于到了这一点,我对自己的所作所为感到高兴!
不幸的是,我还有一个问题:(
我有两列我可以搜索日期。 在第一个(第1列)上,我搜索过去的日期 在第二个(第6列),我搜索未来的日期。
但第二个不起作用!它无法搜索未来的日期!
这是我的JS CODE:
$(document).ready(function() {
//DATATABLE
//To display datatable without search and page length select, and to still have pagination work, instantiate like so
var oTable=$('#table_id').dataTable({
"sDom":"tp",
"pageLength": 10,
"pagination":true,
// Date Sorting
columnDefs: [
{ type: 'date-eu', targets: ([1,6])}
],
//// order table onload
"order": [[ 1, 'desc' ]],
});
//DATE RANGE
//set global vars that are set by daterange picker, to be used by datatable
var startdate;
var enddate;
//instantiate datepicker and choose your format of the dates
$('#reportrange').daterangepicker({
ranges: {
"Today": [moment(), moment()],
'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
'7 last days': [moment().subtract(6, 'days'), moment()],
'30 last 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')]
}
,
"opens": "right",
format: 'DD/MM/YYYY'
},
function(start, end,label) {
// Parse it to a moment
var s = moment(start.toISOString());
var e = moment(end.toISOString());
startdate = s.format("YYYY-MM-DD");
enddate = e.format("YYYY-MM-DD");
});
//Filter the datatable on the datepicker apply event with reportage 1
$('#reportrange').on('apply.daterangepicker', function(ev, picker) {
startdate=picker.startDate.format('YYYY-MM-DD');
enddate=picker.endDate.format('YYYY-MM-DD');
$.fn.dataTableExt.afnFiltering.push(
function( oSettings, aData, iDataIndex ) {
if(startdate!=undefined){
// 1 here is the column where my dates are.
//Convert to YYYY-MM-DD format from DD/MM/YYYY
var coldate = aData[1].split("/");
var d = new Date(coldate[2], coldate[1]-1 , coldate[0]);
var date = moment(d.toISOString());
date = date.format("YYYY-MM-DD");
//Remove hyphens from dates
dateMin=startdate.replace(/-/g, "");
dateMax=enddate.replace(/-/g, "");
date=date.replace(/-/g, "");
//console.log(dateMin, dateMax, date);
// run through cases to filter results
if ( dateMin == "" && date <= dateMax){
return true;
}
else if ( dateMin =="" && date <= dateMax ){
return true;
}
else if ( dateMin <= date && "" == dateMax ){
return true;
}
else if ( dateMin <= date && date <= dateMax ){
return true;
}
// all failed
return false;
}
}
);
oTable.fnDraw();
});
$('#reportrange2').daterangepicker({
ranges: {
"Today": [moment(), moment()],
'Tomorrow': [moment().add(1, 'days'), moment().subtract(1, 'days')],
'7 next days': [moment().add(6, 'days'), moment()],
'Next 30 days': [moment().add(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')]
}
,
"opens": "right",
format: 'DD/MM/YYYY'
},
function(start, end,label) {
// Parse it to a moment
var s = moment(start.toISOString());
var e = moment(end.toISOString());
startdate = s.format("YYYY-MM-DD");
enddate = e.format("YYYY-MM-DD");
});
//Filter the datatable on the datepicker apply event with reportage 1
$('#reportrange2').on('apply.daterangepicker', function(ev, picker) {
startdate=picker.startDate.format('YYYY-MM-DD');
enddate=picker.endDate.format('YYYY-MM-DD');
$.fn.dataTableExt.afnFiltering.push(
function( oSettings, aData, iDataIndex ) {
if(startdate!=undefined){
// 1 here is the column where my dates are.
//Convert to YYYY-MM-DD format from DD/MM/YYYY
var coldate = aData[6].split("/");
var d = new Date(coldate[2], coldate[1]-1 , coldate[0]);
var date = moment(d.toISOString());
date = date.format("YYYY-MM-DD");
//Remove hyphens from dates
dateMin=startdate.replace(/-/g, "");
dateMax=enddate.replace(/-/g, "");
date=date.replace(/-/g, "");
//console.log(dateMin, dateMax, date);
// run through cases to filter results
if ( dateMin == "" && date <= dateMax){
return true;
}
else if ( dateMin =="" && date <= dateMax ){
return true;
}
else if ( dateMin <= date && "" == dateMax ){
return true;
}
else if ( dateMin <= date && date <= dateMax ){
return true;
}
// all failed
return false;
}
}
);
oTable.fnDraw();
});
// Setup - add a text input to Consultant/Client/candidate Header cell
$('#table_id .filters .FilterinputSearch').each( function () {
var title = $('#table_id thead .FilterinputSearch').eq( $(this).index() ).text();
$(this).html( '<input type="text" placeholder="recherche '+title+'" />' );
} );
// DataTable
var table = $('#table_id').DataTable();
// Apply the search
table.columns([3,4,5]).eq( 0 ).each( function ( colIdx ) {
$( 'input', $('.filters th')[colIdx] ).on( 'keyup change', function () {
table
.column( colIdx )
.search( this.value )
.draw();
} );
} );
} );
这是我的JSFIDDLE:JSFIDDLE
也许这是我无法看到的东西,因为我是一个真正的JS Noob! :d
答案 0 :(得分:0)
我刚玩过你的小提琴,我的错误就在这里,所以我改变了这个:
'Tomorrow': [moment().add(1, 'days'), moment().add(1, 'days')],
当然将一条记录的日期改为明天:
<tr>
<td>Approved</td>
<td>09/12/2015</td>
<td title="Prolongation simple">prlg. nv. ctr.</td>
<td>Donnée 2</td>
<td>Donnée 2</td>
<td>Donnée 2</td>
<td>09/12/2015</td>
</tr>
这对我有用。顺便说一句,你把所有这些放在一起做得很好。 DataTable实现起来并不容易。