Dec 9, 2022
0
Level 1
Data range search filter with two variables in one column
Like in title. I use data range search code from datatables documentation like:
<script>
var minDate, maxDate;
// Custom filtering function which will search data in column four between two values
$.fn.dataTable.ext.search.push(
function( settings, data, dataIndex ) {
if ($('#min').val() != '') {
var min = new Date($('#min').val());
console.log(min);
} else {
var min = null;
}
if ($('#max').val() != '') {
var max = new Date($('#max').val());
console.log(min);
} else {
var max = null;
}
var date = new Date( data[6] );
console.log(date);
if (
( min === null && max === null ) ||
( min === null && date <= max ) ||
( min <= date && max === null ) ||
( min <= date && date <= max )
) {
return true;
}
return false;
}
);
$(document).ready(function() {
$('#example').DataTable();
minDate = new DateTime($('#min'), {
format: 'YYYY-MM-DD'
});
maxDate = new DateTime($('#max'), {
format: 'YYYY-MM-DD'
});
var table = $('#example').DataTable();
$('#filtr_nazw').on( 'change', function () {
table.columns(0).search( this.value ).draw();
} );
$('#filtr_zadan').on('change', function () {
table.columns(1).search( this.value ).draw();
} );
$('#filtr_zlecen').on('change', function () {
table.columns(2).search( this.value ).draw();
} );
$('#filtr_firm').on('change', function () {
table.columns(3).search( this.value ).draw();
} );
$('#filtr_pracownikow').on('change', function () {
table.columns(4).search( this.value ).draw();
} );
$('#filtr_rns').on( 'change', function () {
table.columns(8).search( this.value ).draw();
} );
$('#filtr_opisu').on( 'change', function () {
table.columns(9).search( this.value ).draw();
} );
$('#min, #max').on('change', function () {
table.draw();
});
});
</script>
but when i try to search dates there is nothing. Inspect show invalid date error. There are two variables in one column in the table like :
<td>
{{ $job->start_date ?? '' }}
{{ Carbon\Carbon::parse($job->start)->format('h:i') }}
</td>
when i delete second variable everythink is ok, search filter works. Is there any way i can filter by these two values?
Please or to participate in this conversation.