Jump to:

23377 Posts in 18296 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » Load children via AJAX

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

Page: 1
Go to End
Author Topic: 765 Views
  • web2works
    Avatar
    Community Member
    50 Posts

    Load children via AJAX Link to this post

    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

  • Jaymonkey
    Avatar
    Community Member
    10 Posts

    Re: Load children via AJAX Link to this post

    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

  • Jaymonkey
    Avatar
    Community Member
    10 Posts

    Re: Load children via AJAX Link to this post

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

  • Jaymonkey
    Avatar
    Community Member
    10 Posts

    Re: Load children via AJAX Link to this post

    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');
    }

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: Load children via AJAX Link to this post

    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>

  • Jaymonkey
    Avatar
    Community Member
    10 Posts

    Re: Load children via AJAX Link to this post

    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.

    765 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.