Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Phillip Turner 98 posts 413 karma points
    Dec 02, 2014 @ 18:03
    Phillip Turner
    0

    Navigation with drop downs

    Hello Umbracians!

    I have trying to create a navigation with submenus (drop downs) based of the the node tree.

    This is the code I have been using for the basic navigation:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        @*Get the root of the website *@
        var homeNode = CurrentPage.AncestorOrSelf(1);
    }
    <ul id="main-menu">
        <li class="@(homeNode.Name == CurrentPage.Name ? "current-selected" : null)">
        <a href="@homeNode.Url">@(homeNode.alternativeName == "" ? homeNode.Name : homeNode.alternativeName)</a>
    </li>
        @foreach (var page in homeNode.Children.Where("Visible"))
        {
            <li class="@(page.IsAncestorOrSelf(CurrentPage) ? "current-selected" : null)">
                <a href="@page.Url">@(page.alternativeName == "" ? page.Name : page.alternativeName)</a>
            </li>
        }
    </ul>
    

    but now I need check each node for the existence on children and then loop through them.

    Any assistance would be great.

    Phillip

  • Dan Lister 416 posts 1974 karma points c-trib
    Dec 02, 2014 @ 18:12
    Dan Lister
    100

    Hi Phillip,

    Could you not introduce another for each loop inside your existing one?

    @{
        var homeNode = CurrentPage.AncestorOrSelf(1);
    
        foreach (var page in homeNode.Children.Where("Visible"))
        {
            <li>
                <a href="@page.Url">page.Name</a>
    
                @if (page.Children.Where("Visible").Any())
                {
                    <ul>
                        @foreach (var child in page.Children.Where("Visible"))
                        {
                            <li><a href="@child.Url">@child.Name</a></li>
                        }
                    </ul>
                }
            </li>
        }
    }
    

    Hope this helps.

    Thanks, Dan.

  • Phillip Turner 98 posts 413 karma points
    Dec 02, 2014 @ 18:36
    Phillip Turner
    0

    Worked indeed.

    Many thanks!

    Phillip

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies