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?
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 :)
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>
}
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???
Rendering tree structure menu recursively
Hi all,
I've stared myself blind on this one :-)
I have the following structure in the content section:
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:
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
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 :)
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):
Hope that helps anyone with the same problem :-)
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
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:
In the top of my .cshtml file :-) Hope that helps.
All the best,
Bo
is working on a reply...
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.