Copied to clipboard

Flag this post as spam?

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


  • Rob 43 posts 79 karma points
    Nov 29, 2012 @ 02:53
    Rob
    0

    Problem with Menu - only listing Decendants at this level and not decendants of a diffent node at the same level

    I am having a total brain block here.

    The goal is to build a subnavigation menu where all nodes of levels 2 to 4 are visible, but if the user is on a submenu that has subnodes at level 5, or 6 then those items should show. But not items of level 5 and 6 for other nodes of level 2,3,or 4.

    However what i seem to be stuck on is getting all items of a level or none below the level of 4. I have been trying to work with .IsDescendant, but think it is probably wrong for this.. or I am just not using it correctly..

    Any help is greatly appreciated.

    Here is the code (currently it is all of a the level of @Model.Level)

    @using umbraco.MacroEngines;
    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
    const int startLevel = 2;
    var finishLevel [email protected];
    var parent = @CurrentModel.AncestorOrSelf(startLevel);
    if (parent != null)
    {
    @traverse(parent,startLevel,finishLevel) ;
    }
    }
    @helper traverse(dynamic parent,int startLevel,int finishLevel)
    { <ul> @foreach (var node in parent.Children.Where("Visible"))
    {
    <li>
    <a href="@node.Url">@node.Name</a>
    @if (@node.Level <= finishLevel||(@node.DescendantsOrSelf))
    {
    @traverse(node,startLevel,finishLevel);
    }
    </li>
    }
    </ul>
    }

  • Fuji Kusaka 2203 posts 4220 karma points
    Nov 29, 2012 @ 05:22
    Fuji Kusaka
    0

    Hi Rob,

    What you could do is try to display all nodes from level 2 with a docTypeName. Something like 

    @foreach(var node in Content.AncestorOrSelf(2).Descendants("docTypeName"){
    //
    }

    //fuji

  • Nathan Woulfe 447 posts 1665 karma points MVP 5x hq c-trib
    Nov 29, 2012 @ 06:57
    Nathan Woulfe
    0

    Hi Rob, 

    I'd try something like this (I'm assuming you want to show all nodes at levels 2-4, not just relative to the current node) - 

    @using umbraco.MacroEngines;
    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @{ 
        const int startLevel = 2;
        const int finishLevel = 4; 
        var parent = CurrentModel.AncestorOrSelf(startLevel); 
    
        if (parent != null)
        {
            @traverse(parent,startLevel,finishLevel) ;
        }
    }
    
    @helper traverse(dynamic parent,int startLevel,int finishLevel)
    { 
    
        <ul> 
        @foreach (var node in parent.Children.Where("Visible")) 
        { 
            <li>
                <a href="@node.Url">@node.Name @node.Level</a> 
                @if ((parent.Descendants(5).Count() != 0 && node.IsDescendantOrSelf(Model) && node.Level <= 5) || node.Level <= 3)
                {
                    @traverse(node,startLevel,finishLevel);
                }
            </li>
    
        } 
        </ul>
    }

    Seems to work for me - if I'm on a branch of the tree that is four or less levels deep, the navigation lists all nodes at levels 2-4. If I'm on a branch with more than four levels, it lists levels 5 and 6, but only for that branch. I'm checking that a node has descendants at level 5, the node is a descendant of the current node, and then traversing children up to level 5 (to display level 6 nodes). If there are no descendants at level five, then we traverse as far as level 3 (to get the level four nodes)

    Should at least be a step in the right direction. Hope that helps.

    cheers
    Nathan

  • Rob 43 posts 79 karma points
    Nov 29, 2012 @ 23:24
    Rob
    0

    Nathan, thanks. I will try that tonight when i get home. It looks like you have probably hit on my soultion for me. Looking at your code also makes me think i may have had the test for descendant on the wrong side of the ||  - classic logic error !

    THANKS!!!

    Rob

Please Sign in or register to post replies

Write your reply to:

Draft