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

sort children by Date


Go to End


5 Posts   6985 Views

Avatar
Andre

Community Member, 146 Posts

26 August 2009 at 2:55am

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?

Avatar
bummzack

Community Member, 904 Posts

26 August 2009 at 8:50am

Edited: 26/08/2009 8:51am

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

Avatar
ConGra

Community Member, 1 Post

19 November 2009 at 11:50pm

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

Avatar
Mungo

Community Member, 1 Post

23 February 2010 at 7:04am

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.

Avatar
zenmonkey

Community Member, 545 Posts

24 November 2010 at 6:05am

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?