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'));
}
function loadMoreData(page) {
var filters = searchFilters();
$.ajax({
url: '/restaurants/search?page=' + page,
data: filters, // <-- this was missing
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...");
});
}
UPDATE
$(document).ready(function(){
storeSearchAjax();
});
UPDATE 2
single scroll causing 10s of request !
Actually the scroll event is firing too many times even for a "light touch" creating many request for the same filter criteria you need to use some deboucing using library like lodash. So that the event fire less often and won't jam the browser or the server
Add lodash lib.
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.19/lodash.min.js"></script>
Remove this code
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() >= $(document).height()) {
page++;
loadMoreData(page);
}
});
Add this instead
window.addEventListener('scroll', _.throttle(function(){
if($(window).scrollTop() + $(window).height() >= $(document).height()) {
page++;
loadMoreData(page);
}
}, 500));
UPDATE 3
For the second issue, don't increment page on scroll rather do on the ajax success callback. And also one more thing : currently its doing ajax even if we scroll up which is wrong instead it should do ajax only when we scroll down MORE than the previous deepest point for that you need to store the deepest scroll value in some variable and use that to compare whether use has scrolled MORE deep than the previously done
//create on global var say
var deepestPoint = 0;
window.addEventListener('scroll', _.throttle(function(){
if(
( $(window).scrollTop() + $(window).height() >= $(document).height() )
&& ($(window).scrollTop() > deepestPoint )
) {
page++; //<--Remove this from here
loadMoreData(page);
deepestPoint = $(window).scrollTop();
}
}, 500));
//And then
}).done(function(data) {
if(data.html=="") {
$('.ajax-load').html("");
return;
}
$('.ajax-load').hide();
$(".loading_restaurants").append(data.html);
//<--paste here
}).fail(function(jqXHR, ajaxOptions, thrownError) {
$('.ajax-load').html("server not responding...");
});
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
JQuery is arguably the most popular JavaScript library with so many features for modern development. JQuery is a fast and concise JavaScript library created by John Resig in 2006. It is a cross-platform JavaScript library designed to simplify client-side HTML scripting. Over 19 million websites are currently using jQuery! Companies like WordPress, Facebook, Google, IBM and many more rely on jQuery to provide a kind of web browsing experience.
https://jquery.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.