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

Count parent menus and return number


Go to End


3 Posts   2519 Views

Avatar
joelg

Community Member, 134 Posts

5 February 2009 at 11:50pm

Edited: 05/02/2009 11:51pm

Hi everyone

I'm trying to do a pretty basic thing I guess, but can't seem to figure out how.

I have an accordion javascript/css menu in two levels. It works. When the menu open and I click on a second level menu link I want to open the same first level menu when the site has loaded the new page. Right now the accordion closes again. It's the javascript that handles the opening and my codes look like this.

<script type="text/javascript">
var slider1=new accordion.slider("slider1");
slider1.init("slider", 0, "open");
</script>

It's the second parameter in the slider1.init function that tells which menu to open in the accordion. So basically what I would do is to create a function, that finds the parent, count through the menus and return the correct number.

Any help is really appreciated.

Joel

Avatar
joelg

Community Member, 134 Posts

6 February 2009 at 12:39am

If I could do something like this, that would be nice, but it doesn't work:

<% if Parent %>
	$Parent.Pos
<% end_if %>

Avatar
joelg

Community Member, 134 Posts

6 February 2009 at 2:37am

Ok, found a solution. Perhaps not best practise, but works:


function menuItemSelected() {
$record = DataObject::get("SiteTree", "ParentID = 0");
$count = 0; 
		   
   if($record->exists()) 
   {	
   		foreach($record as $item)
   		{
   			if($item->ID == $this->ParentID)
   			{
   				break;	
   			}
   			$count ++;
   		}
   }
	   
return ($count);
}