Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

[solved] Pagination and Ajax Calls


Go to End


5 Posts   2507 Views

Avatar
zenmonkey

Community Member, 545 Posts

18 March 2015 at 3:28am

I'm trying to craete an infitie scroll using pagiantion. The JavaScript code work fine i render the whole page on pagination, but it's slow. So i tried converting the template into an ajax page. The paginated pages (after the first) only seem return a to a string that says "DataList".

Here is my controller

class ArtistsPage_Controller extends Page_Controller {
	
	private static $allowed_actions = array (
		"Artists", "RepresentedArtists", "OtherArtists", "PaginationCache", "RepCache"
	);

	public function init() {
		parent::init();
	}
	
	public function index() {
		if (Director::is_ajax()) {
		    return $this->renderWith(array("AjaxArtists"));
		} else {
			return Array();
		}
	}
	
	public function OtherArtists() {
		
		$artists = $this->Children()->filter(array("IsRepresented" => 0));
		
		foreach ($artists as $artist) {
			if($artist->Children()->count() == 0) {
				$artists->remove($artist);
			}
		}
		
		return new PaginatedList($artists, $this->request);
	}
	

}

Avatar
wmk

Community Member, 87 Posts

18 March 2015 at 4:52am

Could it be possible that $artists->remove($artist) deleted the DataObject in DB? Cause $artists is still an DataList, and DataList::remove() deletes in DB... ArrayList::remove() on the other hand just removes from the current list.

Either filter in your query or create a new ArrayList and put all DataObjects to the new list...

Avatar
zenmonkey

Community Member, 545 Posts

18 March 2015 at 7:41am

@wmk, nothing is being removed from the DB. I don't hink $this->Children() returns a DataLIst. As a test I modified the function to create a new ArrayList and build the Paginated list from that and it's the same result.

As I mentioned when it's not an ajax call the function works properly.

Avatar
wmk

Community Member, 87 Posts

18 March 2015 at 9:01pm

You might be partially right, as page -> children is a has_many it returns a HasManyList, which also does Database operations. See docs...

Please double check if your database is still ok and all relations are set properly and ->Children() returns anything...

What does

$this->Children()->count()
return?

Avatar
zenmonkey

Community Member, 545 Posts

19 March 2015 at 3:48am

Edited: 19/03/2015 3:49am

I figured it out. The problem is the standard code to get the next page link $Source.NextLink. Since the URLs are relative the AJAX call was looking for /currentpage/currentpage/?starrt=10. I replaced the standard $Source.NextLink with {$BaseHref}$Source.NextLink to fore the URL to be absolute.

Though I'm still not sure why it matters. both tempaltes (Ajax and non) shoud work the same