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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Load children via AJAX


Reply


6 Posts   888 Views

Avatar
web2works

Community Member, 50 Posts

26 February 2011 at 11:31pm

Hi, I have quite a heavy homepage with a lot of children dataset that are also displayed with data and images.

Would it be possible to extend Silverstripe to fetch the children only via jQuery AJAX?

Thanks ben

Avatar
Jaymonkey

Community Member, 10 Posts

17 January 2014 at 11:54pm

I am trying to do something similar to this myself, but finding it difficult to find resources online for this topic!

I have set up a News Holder Page and a News Page. But I want to populate the holder via Ajax rather than loading everything at once, probably triggered when the browser is scrolled down down beyond currently loaded news.

I have a function in my controller which can return a specified amount of news teasers.

But I guess what I really need is for some way to tell my function that I already have X amount of data displayed on the page and I need you to continue loading from this point!

Does that make sense?

Has anybody done anything similar to this? Any assistance would be very appreciated.

Thanks Jay

Avatar
Jaymonkey

Community Member, 10 Posts

18 January 2014 at 12:40am

On further investigation I have discovered the PaginatedList class, this may help me I think!

Avatar
Jaymonkey

Community Member, 10 Posts

18 January 2014 at 5:48am

I think I am getting closer! But for some reason the $results will not render to my template! Can anyone help?

public function LatestNews(SS_HTTPRequest $request) {

if ((!isset($request)) || (!$request->requestVars())) {
$length = 2;
$current = 1;
} else {
$length = $request->getVar('length');
$current = $request->getVar('current');
}

$holder = ArticleHolder::get()->First();

$results = new PaginatedList(ArticlePage::get()->filter('ParentID', $holder->ID)->sort('Date DESC'));

$results->setPageLength($length);

$results->setCurrentPage($current);

return $results->renderWith('ArticleHolder');
}

Avatar
Willr

Forum Moderator, 5513 Posts

18 January 2014 at 11:13am

You're calling renderWith on the Paginated list directly which works but you would need to use loop $Me to refer to the current object in scope. You also won't get any other variables from the current controller. A potential 'better' way is calling renderWith on the controller.

return $this->customise(new ArrayData(array(
'Results' => $results
))->renderWith('ArticleHolder');

Then you can use

<ul>
<% loop $Results %>
<li><a href="$Link">$Title</a></li>
<% end_loop %>
</ul>

Avatar
Jaymonkey

Community Member, 10 Posts

18 January 2014 at 11:24am

Thanks for the reply, after much reading today I think I finally got there earlier.

I did this in the end:

public function LatestNews(SS_HTTPRequest $request) {

if (!isset($request->requestVars)) {
$length = 1;
$current = 1;
} else {
$length = $request->getVar('length');
$current = $request->getVar('current');
}

$holder = ArticleHolder::get()->First();

$results = new PaginatedList(ArticlePage::get()->filter('ParentID', $holder->ID)->sort('Date DESC'));

$results->setPageLength($length);

$results->setCurrentPage($current);

return $results->customise(array('LatestNews' => $results))->renderWith('newsteaser');
}

It seems to be working at the moment!

Thanks again.