Copied to clipboard

Flag this post as spam?

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


  • Dominic Kelly 114 posts 133 karma points
    Feb 16, 2012 @ 12:15
    Dominic Kelly
    0

    Razor sub-nav

    I'm trying to build a very complex menu and really need some help.

    I've put together some examples of how the sub nav needs to work in the following document:

    http://dl.dropbox.com/u/528350/kws-sub-nav.docx

    I've also got the nav kind of working here:

    http://31.222.141.203/level-2.aspx

    Notice that all the branches open as you step down the navigation tree. What does not happen is essentially:

    I only want to display the chiildren and parents of the currently selected node. Not all nodes.

    The code I have so far is:

    @inherits umbraco.MacroEngines.DynamicNodeContext          
    @functions{
        int level_3 = 1;
        string open_level_3 = "";
        string open_level_4 = "";
    }
    @{
        int level_2_node_id = 0;
        foreach (var level in @Model.Ancestors().Where("Visible").Where("Level == 2"))
        {
            <h2><a href="@level.Url">@level.Name</a></h2>
            level_2_node_id = @level.Id;
        }
        if (@Model.Level == 2)
        {
            <h2><a href="@Model.Url">@Model.Name</a></h2>
            level_2_node_id = @Model.Id;
        }
      
    }
    @helper traverse(dynamic node)
    {
        var items = node.Children.Where("Visible").Where("Level <= 99").Where("NodeTypeAlias != \"contact-us\"").Where("NodeTypeAlias != \"HomepageSlide\"").Where("NodeTypeAlias != \"search\"").Where("NodeTypeAlias != \"GalleryMediaItem\"").Where("NodeTypeAlias != \"InteriorSlide\"").Where("NodeTypeAlias != \"FocusedLandingSlide\"");
        
        foreach (var item in items)
        {
        
            if (@Model.Name == @node.Name) { open_level_3 = "level3"; }

        }
        if (items.Count() > 0 && @Model.Level >= @node.Level)
        {   
            
            <ul class="nav clearfix" id="level_@(node.Level + 1)">
                @foreach (var item in items)
                {
                    
                    <li  @if (@Model.Url == @item.Url){@Html.Raw("class=\"selected\"");}><a href="@item.Url">@item.Name</a>
                        
                        @traverse(item)
                        
                    </li>
                        
                }
            </ul>
           
        }
            
    }

    <div class="results">
        @traverse(@Model.NodeById(@level_2_node_id))
    </div>

     

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Feb 16, 2012 @ 14:49
    Tom Fulton
    0

    Hi Dominic,

    You could try using the IsAncestorOrSelf helper to determine if you should render the subpages or not - so basically if the current page in the loop is an ancestor of the current page, then render the submenu.

    Maybe this might do the trick:

    @if (item.IsAncestorOrSelf(Model)) {
               @traverse(item)
    }

    Check the Razor Cheat Sheet for other helpers :)

    -Tom

  • Dominic Kelly 114 posts 133 karma points
    Feb 16, 2012 @ 18:04
    Dominic Kelly
    1

    You have made me a very happy man! I knew I was close, I just couldn't get my head around the last part.

Please Sign in or register to post replies

Write your reply to:

Draft