Copied to clipboard

Flag this post as spam?

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


  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Mar 20, 2013 @ 15:23
    Bo Damgaard Mortensen
    0

    Rendering tree structure menu recursively

    Hi all,

    I've stared myself blind on this one :-)

    I have the following structure in the content section:

    Page 1
        - Page2
        - Page 3
              - Page 4
              - Page 5
                    - Page 55
    
    Page 6
        - Page 7
        - Page 8
    
    Page 9

    Now, on the page I need to render the same tree structure, but only display the levels for the current node. So, if I am on Page 5 on the site, my menu would look like this:

     

    Page 1
        - Page2
        - Page 3
              - Page 4
              - Page 5 <-- The page I am visiting right now
                    - Page 55
    
    Page 6
    Page 9

     

    I've got a feeling that this is quite easy to achieve and I am completely overthinking it ;-)

    Has anyone got a code-snippet to get me kicked back in?

    Thanks in advance.

    All the best,

    Bo

  • Andreas Iseli 150 posts 427 karma points
    Mar 20, 2013 @ 16:42
    Andreas Iseli
    100

    There is a "IsDescendant" method on the dynamic content node that should help you figuring out whether the current page is a child of the first level node. If not the skip, else display the path. Something like that :)

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Mar 22, 2013 @ 10:11
    Bo Damgaard Mortensen
    1

    Hey Andreas,

    Genious! :-) Why didn't I think of that.

    Got it to work last night with the following code (sorry about the danish comments):

    @{
                DynamicNode current = Model;
    
                // Hent nodes på 2. niveau
                var nodes = Model.AncestorOrSelf(2).Children.Where("Visible");        
    
                // Check om nogle nodes på 2. niveau eksisterer
                if(nodes.Any())
                {
                    <ul class="side-nav-list">
                        @* Loop igennem det øverste (2.) niveau af nodes *@
                        @foreach(var node in nodes)
                        {                        
                            // Start den rekursive metode til rendering af træstruktur
                            @RenderSubMenuRecursive(node)                                           
                        }
                    </ul>
                }
            }

    @* Helper metode til at rendere venstremenu som træstruktur *@ @helper RenderSubMenuRecursive(DynamicNode node) { // Reference til alle childnodes af den givne node, som er sat til at blive vist på sitet var childNodes = node.Children.Where(x => x.GetPropertyValue("umbracoNaviHide").Equals("0")); // CSS class afhængig af om den pågældende node i rekusionstræet er forfædre til den nuværende node string css = node.IsAncestor(Model) ? "open" : ""; // Ændre CSS class hvis den pågældende node i rekusionstræet er den samme som den nuværende node if(node.Id == Model.Id) { css = "active"; } // Output markup <li class="@css"> <a href="@node.Url">@node.Name</a> <ul> @* Loop igennem childnodes af den pågældende node i rekursionstræet hvor nuværende node er descendent til den pågældende node i rekursionstræet *@ @foreach (var childNode in childNodes.Where(childNode => Model.IsDescendantOrSelf(node))) { // Kør metode så længe der eksisterer descendente nodes til nuværende node // eller childnodes til nodes på 2. niveau @RenderSubMenuRecursive(childNode) } </ul> </li> } 

    Hope that helps anyone with the same problem :-)

  • Allan James 20 posts 40 karma points
    Mar 28, 2013 @ 07:01
    Allan James
    0

    Hello Bo... I'm hoping you can help me... I have copied the exact code you show here but I'm gett the following error when I try and save the macro:

    d:\Websites\www.assetexecutive.com\macroScripts\635000249915072889_RootMenu.cshtml(22): error CS0246: The type or namespace name 'DynamicNode' could not be found (are you missing a using directive or an assembly reference?)

    I was using version 6.02 but just updated to 6.03 as there seemed to be something Dynamic Node in the fixes list so I upgraded

    I noticed in your sample that you didn't list any using or include statements.... is there something more I need to include that you haven't shown here???

    I tried adding these bt it didn't work:

    @using umbraco.NodeFactory  
    @inherits umbraco.MacroEngines.DynamicNodeContext

    Allan (midnight and stuck on something that should be SO easy)
  • Bo Damgaard Mortensen 719 posts 1207 karma points
    May 07, 2013 @ 11:40
    Bo Damgaard Mortensen
    0

    Hi Allan,

    Sorry about the very late reply, I didn't realise someone actually replied to my thread (our.umbraco seriously needs notifications!)

    I have the following:

    @using umbraco.MacroEngines
    @inherits umbraco.MacroEngines.DynamicNodeContext

    In the top of my .cshtml file :-) Hope that helps.

    All the best,

    Bo

Please Sign in or register to post replies

Write your reply to:

Draft