# Infinite scroll - controller

**URL:** <https://forum.getkirby.com/t/infinite-scroll-controller/6205>\
**Category:** Questions\
**Tags:** v2\
**Created:** [January 5, 2017, 12:15pm UTC](https://forum.getkirby.com/t/infinite-scroll-controller/6205 "2017-01-05T12:15:54Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![wildthing](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/wildthing/32/3457_2.png) [@wildthing](https://forum.getkirby.com/u/wildthing)\
**Post date:** [January 5, 2017, 12:15pm UTC](https://forum.getkirby.com/t/infinite-scroll-controller/6205/1 "2017-01-05T12:15:54Z")

</div>

Hi to all

I have made infinite scroll for page where is placed filters.  
and when i’m filter something its filter good but if I scroll page down it start loads not filtered items to loop

```
return function($site, $pages, $page) {

  // get all articles and add pagination
  $bookmakers = page('review')->children()->visible();
  $count = $bookmakers->count();
	
  // check if the request is an Ajax request and if the limit and offset keys are set
  if(r::ajax() && get('offset') && get('limit')) {

// convert limit and offset values to integer
$offset = intval(get('offset'));
$limit = intval(get('limit'));

// limit projects using offset and limit values
$bookmakers = $bookmakers->offset($offset)->limit($limit);

// check if there are more projects left
$more = $count > $offset + 1;

  // otherwise set the number of projects initially displayed
  } else {

  
  if($query_rating = get('rating')) {
	  $bookmakers = $bookmakers->filter(function($bookmaker) use ($query_rating) {
		    $collection = page('user-reviews')->children()->visible()->filterBy('bookmaker_name', '==', $bookmaker->slug());
		    $rating = $collection->pluck('rating_10',',', false);
		    if(array_intersect($query_rating, array(round(a::average($rating))))) {
			   return $bookmaker;
			}
	  });
	  $bookmakers = $bookmakers->sortBy($query_rating, 'asc');	  
	}
  if($query_bookmaker_name = get('bookmaker_name')) {
  		$bookmakers = $bookmakers->search($query_bookmaker_name, 'title');
	}	
  if($query_market = get('market')) {
  		$bookmakers = $bookmakers->search($query_market, 'market');
	}
  if($query_country = get('country')) {
  		$bookmakers = $bookmakers->search($query_country, 'excluded_countries');
	}
  if($query_payment = get('payment')) {
  		$bookmakers = $bookmakers->search($query_payment, 'deposit|withdrawal');
	}
  if($query_language = get('language')) {
  		$bookmakers = $bookmakers->search($query_language, 'website_language');
	}
  if($query_currency = get('currency')) {
  		$bookmakers = $bookmakers->search($query_currency, 'currency');
	}

$offset = 0;
$limit = 20;
$bookmakers = $bookmakers->limit($limit);

  }

  // pass $articles and $pagination to the template
  return compact('offset', 'limit', 'bookmakers', 'more', 'bookmaker', 'rating', 'bookmaker_name', 'query_bookmaker_name', 'query_market', 'query_rating', 'query_country', 'query_payment', 'query_language', 'query_currency');

};

```

where is my mistake?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [January 5, 2017, 12:20pm UTC](https://forum.getkirby.com/t/infinite-scroll-controller/6205/2 "2017-01-05T12:20:56Z")

</div>

You use an if statement, where you check if you have an Ajax call, and if not, you do all sorts of filtering. But those filters are not respected in the Ajax call.

---

<div class="post-metadata">

**Author:** ![wildthing](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/wildthing/32/3457_2.png) [@wildthing](https://forum.getkirby.com/u/wildthing)\
**Post date:** [January 5, 2017, 12:30pm UTC](https://forum.getkirby.com/t/infinite-scroll-controller/6205/3 "2017-01-05T12:30:30Z")

</div>

You mean that I need to separate controllers? and filter trough other controller (on other page)?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [January 5, 2017, 12:48pm UTC](https://forum.getkirby.com/t/infinite-scroll-controller/6205/4 "2017-01-05T12:48:28Z")

</div>

No, that’s not what I meant. I mean, you need to apply the filters also within the Ajax call. But you could in fact use [content representations](https://getkirby.com/docs/developer-guide/advanced/representations) instead of checking for the Ajax call.

---

<div class="post-metadata">

**Author:** ![wildthing](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/wildthing/32/3457_2.png) [@wildthing](https://forum.getkirby.com/u/wildthing)\
**Post date:** [January 5, 2017, 12:55pm UTC](https://forum.getkirby.com/t/infinite-scroll-controller/6205/5 "2017-01-05T12:55:17Z")

</div>

Don’t understand about “content representations” how it will helps me?

> [@texnixe](#):
>
> I mean, you need to apply the filters also within the Ajax call

Can you show where I need to make the changes (just on one filter for example)

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [January 5, 2017, 1:00pm UTC](https://forum.getkirby.com/t/infinite-scroll-controller/6205/6 "2017-01-05T13:00:09Z")

</div>

All you need to do is use the same filtering code also within the if-statement

```auto
if(r::ajax() && get('offset') && get('limit')) {

// convert limit and offset values to integer
$offset = intval(get('offset'));
$limit = intval(get('limit'));

// use all filters here as well
if($query_bookmaker_name = get('bookmaker_name')) {
  		$bookmakers = $bookmakers->search($query_bookmaker_name, 'title');
	}

// limit projects using offset and limit values
$bookmakers = $bookmakers->offset($offset)->limit($limit);

// check if there are more projects left
$more = $count > $offset + 1;

  // otherwise set the number of projects initially displayed
  } else {

```

---

<div class="post-metadata">

**Author:** ![wildthing](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/wildthing/32/3457_2.png) [@wildthing](https://forum.getkirby.com/u/wildthing)\
**Post date:** [January 5, 2017, 1:19pm UTC](https://forum.getkirby.com/t/infinite-scroll-controller/6205/7 "2017-01-05T13:19:53Z")

</div>

Ive made this changes

```
return function($site, $pages, $page) {

// get all articles and add pagination
$bookmakers = page('review')->children()->visible();
$count = $bookmakers->count();

// check if the request is an Ajax request and if the limit and offset keys are set
if(r::ajax() && get('offset') && get('limit')) {

// convert limit and offset values to integer
$offset = intval(get('offset'));
$limit = intval(get('limit'));

	if($query_rating = get('rating')) {
	  $bookmakers = $bookmakers->filter(function($bookmaker) use ($query_rating) {
		    $collection = page('user-reviews')->children()->visible()->filterBy('bookmaker_name', '==', $bookmaker->slug());
		    $rating = $collection->pluck('rating_10',',', false);
		    if(array_intersect($query_rating, array(round(a::average($rating))))) {
			   return $bookmaker;
			}
	  });
	  $bookmakers = $bookmakers->sortBy($query_rating, 'asc');	  
	}
	if($query_bookmaker_name = get('bookmaker_name')) {
			$bookmakers = $bookmakers->search($query_bookmaker_name, 'title');
	}	
	if($query_market = get('market')) {
			$bookmakers = $bookmakers->search($query_market, 'market');
	}
	if($query_country = get('country')) {
			$bookmakers = $bookmakers->search($query_country, 'excluded_countries');
	}
	if($query_payment = get('payment')) {
			$bookmakers = $bookmakers->search($query_payment, 'deposit|withdrawal');
	}
	if($query_language = get('language')) {
			$bookmakers = $bookmakers->search($query_language, 'website_language');
	}
	if($query_currency = get('currency')) {
			$bookmakers = $bookmakers->search($query_currency, 'currency');
	}

// limit projects using offset and limit values
$bookmakers = $bookmakers->offset($offset)->limit($limit);

// check if there are more projects left
$more = $count > $offset + 1;

  // otherwise set the number of projects initially displayed
} else {

	if($query_rating = get('rating')) {
	  $bookmakers = $bookmakers->filter(function($bookmaker) use ($query_rating) {
		    $collection = page('user-reviews')->children()->visible()->filterBy('bookmaker_name', '==', $bookmaker->slug());
		    $rating = $collection->pluck('rating_10',',', false);
		    if(array_intersect($query_rating, array(round(a::average($rating))))) {
			   return $bookmaker;
			}
	  });
	  $bookmakers = $bookmakers->sortBy($query_rating, 'asc');	  
	}
	if($query_bookmaker_name = get('bookmaker_name')) {
			$bookmakers = $bookmakers->search($query_bookmaker_name, 'title');
	}	
	if($query_market = get('market')) {
			$bookmakers = $bookmakers->search($query_market, 'market');
	}
	if($query_country = get('country')) {
			$bookmakers = $bookmakers->search($query_country, 'excluded_countries');
	}
	if($query_payment = get('payment')) {
			$bookmakers = $bookmakers->search($query_payment, 'deposit|withdrawal');
	}
	if($query_language = get('language')) {
			$bookmakers = $bookmakers->search($query_language, 'website_language');
	}
	if($query_currency = get('currency')) {
			$bookmakers = $bookmakers->search($query_currency, 'currency');
	}

$offset = 0;
$limit = 20;
$bookmakers = $bookmakers->limit($limit);

}

  // pass $articles and $pagination to the template
return compact('offset', 'limit', 'bookmakers', 'more', 'bookmaker', 'rating', 'bookmaker_name', 'query_bookmaker_name', 'query_market', 'query_rating', 'query_country', 'query_payment', 'query_language', 'query_currency');

};

```

But this isn’t help. It is still loads all items after scroll (ajax event).

---

<div class="post-metadata">

**Author:** ![wildthing](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/wildthing/32/3457_2.png) [@wildthing](https://forum.getkirby.com/u/wildthing)\
**Post date:** [January 5, 2017, 1:25pm UTC](https://forum.getkirby.com/t/infinite-scroll-controller/6205/8 "2017-01-05T13:25:22Z")

</div>

I think that the problem is that inside ajax get info from filter is losing (because im sending filter with POST method)

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [January 5, 2017, 1:26pm UTC](https://forum.getkirby.com/t/infinite-scroll-controller/6205/9 "2017-01-05T13:26:08Z")

</div>

Yes, I just thought you have to send the filters with the Ajax call as well.

---

<div class="post-metadata">

**Author:** ![wildthing](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/wildthing/32/3457_2.png) [@wildthing](https://forum.getkirby.com/u/wildthing)\
**Post date:** [January 5, 2017, 1:41pm UTC](https://forum.getkirby.com/t/infinite-scroll-controller/6205/10 "2017-01-05T13:41:15Z")

</div>

> [@texnixe](#):
>
> the filters with the Ajax call as well.

I create JS ajax call for form

```
// Filter form
$("#bookmaker-form").submit(function(e) {

var url = "/review"; // the script where you handle the form input.

$.ajax({
       type: "GET",
       url: url,
       data: $("#bookmaker-form").serialize(), // serializes the form's elements.
       success: function(data)
       {
           alert(data); // show response from the php script.
       }
     });

e.preventDefault(); // avoid to execute the actual submit of the form.
});

```

And still no luck with it

[https://99bookmakers.com/review](https://99bookmakers.com/review)  
here is how filter works…

---

<div class="post-metadata">

**Author:** ![wildthing](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/wildthing/32/3457_2.png) [@wildthing](https://forum.getkirby.com/u/wildthing)\
**Post date:** [January 5, 2017, 3:22pm UTC](https://forum.getkirby.com/t/infinite-scroll-controller/6205/11 "2017-01-05T15:22:55Z")

</div>

All I can create is ugly way to change limit for filtered content. If it is just listing it limits 20 items, if it filtered content it limits by 200 items…

```
return function($site, $pages, $page) {

// get all articles and add pagination
$bookmakers = page('review')->children()->visible();
$count = $bookmakers->count();
$offset = 0;
$limit = 20;

if($query_rating = get('rating')) {
  $bookmakers = $bookmakers->filter(function($bookmaker) use ($query_rating) {
	    $collection = page('user-reviews')->children()->visible()->filterBy('bookmaker_name', '==', $bookmaker->slug());
	    $rating = $collection->pluck('rating_10',',', false);
	    if(array_intersect($query_rating, array(round(a::average($rating))))) {
		   return $bookmaker;
		}
  });
  $bookmakers = $bookmakers->sortBy($query_rating, 'asc');
  $offset = 0;
  $limit = 200;
}
if($query_bookmaker_name = get('bookmaker_name')) {
		$bookmakers = $bookmakers->filter(function($bookmaker) use ($query_bookmaker_name) {
		return in_array($bookmaker->slug(), $query_bookmaker_name);	
  });
  
  $offset = 0;
  $limit = 200;		
}	
if($query_market = get('market')) {
		$bookmakers = $bookmakers->search($query_market, 'market');
  $offset = 0;
  $limit = 200;
}
if($query_country = get('country')) {
		$bookmakers = $bookmakers->search($query_country, 'excluded_countries');
  $offset = 0;
  $limit = 200;
}
if($query_payment = get('payment')) {
		$bookmakers = $bookmakers->search($query_payment, 'deposit|withdrawal');
  $offset = 0;
  $limit = 200;
}
if($query_language = get('language')) {
		$bookmakers = $bookmakers->search($query_language, 'website_language');
  $offset = 0;
  $limit = 200;
}
if($query_currency = get('currency')) {
		$bookmakers = $bookmakers->search($query_currency, 'currency');
  $offset = 0;
  $limit = 200;
}

// check if the request is an Ajax request and if the limit and offset keys are set
if(r::ajax() && get('offset') && get('limit')) {

// convert limit and offset values to integer
$offset = intval(get('offset'));
$limit = intval(get('limit'));

// limit projects using offset and limit values
$bookmakers = $bookmakers->offset($offset)->limit($limit);

// check if there are more projects left
$more = $count > $offset + 1;

// otherwise set the number of projects initially displayed
} 

$bookmakers = $bookmakers->limit($limit);

  // pass $articles and $pagination to the template
return compact('offset', 'limit', 'bookmakers', 'more', 'bookmaker', 'rating', 'bookmaker_name', 'query_bookmaker_name', 'query_market', 'query_rating', 'query_country', 'query_payment', 'query_language', 'query_currency');

};
```

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 4:53pm UTC](https://forum.getkirby.com/t/infinite-scroll-controller/6205/12 "2025-02-06T16:53:06Z")

</div>

Archived on February 6, 2025, 4:53pm UTC


