Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » sort children by Date

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

Page: 1
Go to End
Author Topic: 4034 Views
  • Andre
    Avatar
    Community Member
    125 Posts

    sort children by Date Link to this post

    Hi there,

    I want to control over a Pages children on which one every got a field Date. Instead of using the order which is given by drag and drop in the backend, I want to sort the children by the datefield. How do I implement this?

  • banal
    Avatar
    Community Member
    901 Posts

    Re: sort children by Date Link to this post

    You could add a method to your class, that returns the child pages, sorted by date:

    // this method belongs to your Page Class
    // or the class that holds the sortable children
    public function SortedChildren(){
       // $children will be a DataObjectSet
       $children = $this->Children();

       if( !$children )
          return null; // no children, nothing to work with

       // sort the DataObjectSet
       // see http://doc.silverstripe.com/doku.php?id=dataobjectset#sorting
       $children->sort('Date');

       // return sorted set
       return $children;
    }

  • ConGra
    Avatar
    Community Member
    1 Post

    Re: sort children by Date Link to this post

    Hello

    We're trying to get this to work, but are not sucseeding ;-(

    Is this function supposed to be posed in the class or in the controller class?

    Thank you.

    Conny

  • Mungo
    Avatar
    Community Member
    1 Post

    Re: sort children by Date Link to this post

    The function has to be in the Controller Class.

    then you'll be able to acess your Custom-Sorted Children in the Template via:

    <% control SortedChildren %>

    $Date
          
    <% end_control %>

    cheers.

  • zenmonkey
    Avatar
    Community Member
    524 Posts

    Re: sort children by Date Link to this post

    I'm trying to get this to work on DataObject within a Children.GroupedBy control and I'm not getting any results. Anyone tried this?

    4034 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.