Jump to:

3434 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » Count parent menus and return number

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

Page: 1
Go to End
Author Topic: 1459 Views
  • joelg
    Avatar
    Community Member
    129 Posts

    Count parent menus and return number Link to this post

    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

  • joelg
    Avatar
    Community Member
    129 Posts

    Re: Count parent menus and return number Link to this post

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

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

  • joelg
    Avatar
    Community Member
    129 Posts

    Re: Count parent menus and return number Link to this post

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

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