I have applied the infinite-ajax-scroll to my project. It is a PHP Laravel project that displays a long list of divs. Instead of using pagination, I wanted to make the user see all results on the same page by scrolling down. I also have a filter for the results and it works well, but the strange thing is that after the results appear through filtering, scrolling down will lead to the appearance of all results without taking into account the current filter.
Can anyone advise on my best approach to this? I want to use the scrolling and it is something maybe realted to the url but I don't know how to fix this
Below is what I have so far.
// Filters
// Search functions
function storeSearchAjax() {
var filters = searchFilters();
$.ajax({
method: 'get',
data: filters,
url: '/restaurants/search-ajax',
success: function(data) {
$('#result').html(data);
}
});
}
function searchFilters() {
offerFilter = $(".offerCheckbox:checked").map(function () {
return $(this).val();
}).get();
cuisineFilter = $(".cuisineCheckbox:checked").map(function () {
return $(this).val();
}).get();
freedeliveryFilter = $(".freedeliveryCheckbox:checked").map(function () {
return $(this).val();
}).get();
var filters = {
"offers" : JSON.stringify(offerFilter),
"cuisines" : JSON.stringify(cuisineFilter),
"freedelivery" : JSON.stringify(freedeliveryFilter)
}
return filters;
}
// Paginate links
$('#result .pagination li a').click(function(e) {
e.preventDefault();
var url = $(this).attr('href');
var params = $.param(searchFilters());
window.location = url+'&'+params;
});
$('input[name="offers[]"], input[name="cuisines[]"], input[name="freedelivery[]"]').change(function(e) {
e.preventDefault();
storeSearchAjax();
});
var page = 1;
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() >= $(document).height()) {
page++;
loadMoreData(page);
}
});
function loadMoreData(page) {
$.ajax({
url: '/restaurants/search?page=' + page,
type: "get",
beforeSend: function() {
$('.ajax-load').show();
}
}).done(function(data) {
if(data.html=="") {
$('.ajax-load').html("");
return;
}
$('.ajax-load').hide();
$(".loading_restaurants").append(data.html);
}).fail(function(jqXHR, ajaxOptions, thrownError) {
$('.ajax-load').html("server not responding...");
});
}
Controller :
public function ajaxSearch(Request $request)
{
$stores = $this->getStores($request);
Helper::usePaginate();
$stores = $stores->paginate(15)->setPath('/restaurants/search');
$cuisines = Storecuisine::getStoreCuisines();
$storedays = Storeday::getStoreDays();
$storewhours = Storeday::all();
$isapp_open=Helper::isAppOpen();
return response()->view('store-search.stores_listing', compact('stores','storedays','isapp_open','storewhours','cuisines'));
}
