I have a javascript date range picker with this structure
$(document).ready(function() {
var start = moment().subtract(29, 'days');
var end = moment();
function calender(start, end) {
$('#reportrange span').html(start.format('D MMM YYYY') + ' - ' + end.format('D MMM YYYY'));
fetchRecords(start, end);
}
$('#reportrange').daterangepicker({
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')]
},
"startDate": start,
"endDate": end,
"opens": "left"
}, calender);
calender(start, end);
$('#prevMonth').on('click', function() {
start.subtract(1, 'month');
end.subtract(1, 'month');
$('#reportrange').data('daterangepicker').setStartDate(start);
$('#reportrange').data('daterangepicker').setEndDate(end);
calender(start, end);
});
$('#nextMonth').on('click', function() {
start.add(1, 'month');
end.add(1, 'month');
$('#reportrange').data('daterangepicker').setStartDate(start);
$('#reportrange').data('daterangepicker').setEndDate(end);
calender(start, end);
});
function fetchRecords(startDate, endDate) {
$.ajax({
type: 'GET',
url: '/record/',
data: {
startDate: startDate.format('YYYY-MM-DD'),
endDate: endDate.format('YYYY-MM-DD')
},
success: function (response) {
console.log('Records fetched successfully:', response);
updateRecordsOnPage(response);
},
error: function (error) {
console.error('Error fetching records:', error);
}
});
}
function updateRecordsOnPage(recordsHtml) {
$('#records-container').html(recordsHtml);
}
});
Once I picked the dates it will past the start and end date to the index controller
public function index(Request $request) {
$user = Auth::user();
$startDate = $request->input('startDate');
$endDate = $request->input('endDate');
$startDate = $startDate ? Carbon::parse($startDate)->startOfDay() : now()->startOfMonth();
$endDate = $endDate ? Carbon::parse($endDate)->endOfDay() : now()->endOfMonth();
$records = Record::where('user_id', $user->id)
->whereBetween('datetime', [$startDate, $endDate])
->paginate(14);
return view('record.index', compact('records'));
}
How can update the record list after picking the dates into the homepage below without reload the page
<div class="flex items-center datepicker">
<i class="fa fa-caret-left ml-2 cursor-pointer" id="prevMonth"></i>
<div id="reportrange" class="flex mx-auto rounded-md border-0 items-center">
<span></span>
</div>
<i class="fa fa-caret-right mr-2 cursor-pointer" id="nextMonth"></i>
</div>
<div class="records" id="records-container">
@foreach ($records as $record)
<!-- record list -->
@endforeach
</div>
I have tried updating the record list through Ajax in the date range picker js above by using updateRecordsOnPage(response);, but it keeps looping and duplicating my current view until the page crashes. Any solution to make the data lists update after picking the date without reloading the page?