Copied to clipboard

Flag this post as spam?

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


  • Sander van de Pas 74 posts 147 karma points
    Sep 12, 2011 @ 13:29
    Sander van de Pas
    0

    How can I get all the child nodes from the nodes on the same level??

    I've something like this:

    - Node 1
    - Node 2
    - Subnode 1
    - Subndoe 2
    - Node 3
    - Subnode 1
    - Subnode 2

    Now I want alle these subnodes when I'm at the Node 1 page. But something like Model.Parent.Children.Children doesn't work!

    Please help!

    Thanx!

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Sep 12, 2011 @ 13:57
    Sebastiaan Janssen
    0

    When you do Parent.Children.Children on for example Subnode 1, it will look at the parent (Node 2) and find the Children (Subnode 1 and 2) but then there's a problem: .Children expects to get the children from a single node.. and you have 2 of them here! 

    So what you can do is a little recursion (I am assuming that this is your exact tree, so no nodes higher than Node 1 - 3. Also assuming you only want to go to level 2, so if Subnodes have children, then you don't want to show them. Adjust "Level <= 2" if you DO want to go deeper):

    <div id="menu"> 
        @traverse(@Model.AncestorOrSelf())
    </div>

    @helper traverse(dynamic node){
       var items = node.Children.Where("Visible && Level <= 2");
    <ul>
    @foreach (var item in items) {
    <li><a href="@item.Url">@item.Name</a>
    @traverse(item)
    </li>
    }
    </ul>
    }
  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Sep 12, 2011 @ 13:59
    Sebastiaan Janssen
    0

    Sorry, the indentation is a bit weird.. 

    One addition: if your tree does have a parent node above Node 1 - 3, then change Model.AncestorOfSelf() to Model.AncestorOrSelf(1) (meaning: go to level 1). Also change the level to 3 then, as everything will be one level lower.. 

  • Sander van de Pas 74 posts 147 karma points
    Sep 12, 2011 @ 14:39
    Sander van de Pas
    0

    My node tree looks like this:

    - Topnode (level 1)
    - Node 1
    - Node 2
    - Subnode 2.1
    - Subnode 2.2
    - Subnode 2.2.1
    - Subnode 2.2.2
    - Subnode 2.2.3
    - Subnode 2.3 
    - Subnode 2.3.1
    - Subnode 2.3.2
    - Subnode 2.4
    - Subnode 2.4.1
    - Subnode 2.4.2
    - Subnode 2.4.3 
    - Node 3
    - Node 4

    The Subnode 2.1 page must be an overview of the children of nodes Subnode 2.2, Subnode 2.3, Subnode 2.4.

    When I do it like you told (with AncestorOrSelf(2) and "Visible && Level

    What's wrong?

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Sep 12, 2011 @ 15:18
    Sebastiaan Janssen
    0

    I think half of your text has been eaten. Can you show me what the expected output should be and on which pages should that appear? Not sure I understand.

  • Sander van de Pas 74 posts 147 karma points
    Sep 12, 2011 @ 15:23
    Sander van de Pas
    0

    There's something wrong with the white spaces...but from "Subnode 2.1" I would have the following output:

    • Subnode 2.2.1
    • Subnode 2.2.2
    • Subnode 2.2.3
    • Subnode 2.3.1
    • Subnode 2.3.2
    • Subnode 2.4.1
    • Subnode 2.4.2
    • Subnode 2.4.3
  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Sep 12, 2011 @ 15:25
    Sebastiaan Janssen
    0

    And this should appear on ALL pages or just from Node 2 and for all subnodes?

  • Sander van de Pas 74 posts 147 karma points
    Sep 12, 2011 @ 15:33
    Sander van de Pas
    0

    No, this should only appear when I'm at the page "Subnode 2.1".

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Sep 12, 2011 @ 15:39
    Sebastiaan Janssen
    0

    Aaah, okay, that should be easy! :-)

    So what you really want is go from 2.1 to 2.2 and show all of 2.2's children. There's a nifty little method called .Next() that can move you easily from 2.1 to 2.2 (as it's the Next node):

    <ul>
    @foreach (var item in Model.Next().Children) {
    <li><a href="@item.Url">@item.Name</a></li>

    </ul> 
  • Sander van de Pas 74 posts 147 karma points
    Sep 12, 2011 @ 15:56
    Sander van de Pas
    0

    Yes, but also the childs of 2.3 and 2.4...and if there came more, also the childs from 2.5, 2.6 and so on...

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Sep 12, 2011 @ 16:02
    Sebastiaan Janssen
    0

    Hmm, I don't really get it, so ONLY on 2.1 you want the subpages of 2.2, 2.3, etc?

    So basically you want the subnodes of every 2.x item, except for 2.1. Then you can just Skip the first item:


      @foreach (dynamic node in Model.Parent.Children.Skip(1)) {
        foreach (var item in node.Children) {
         

        }
      }
  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Sep 12, 2011 @ 16:06
    Sebastiaan Janssen
    0

    Or, if you want to also show the title of the 2.x items (for example): 

    <ul>
      @foreach (dynamic node in Model.Parent.Children.Skip(1)) {
       
    <li>
          @node.Name

         
    <ul>
          @foreach (var item in node.Children) {
           
    <li><a href="@item.Url">@item.Name</a></li>
          }
         
    </ul>
       
    </li>
      }
    </ul>

    edit: added missing .Children

  • Sander van de Pas 74 posts 147 karma points
    Sep 12, 2011 @ 16:55
    Sander van de Pas
    0

    Yes indeed! I already got an error :-)

    I needed a for each inside a for each...But instead of the .Children.Skip(1) I used Parent.[nodeTypeAlias] because the nodetype of 2.1 is different from 2.2 etc.

    Most important: it works!!

    Thanx!!

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Sep 12, 2011 @ 17:11
    Sebastiaan Janssen
    0

    Yay, glad to hear, could you show an example of Parent.[nodeTypeAlias]? Not sure I am familiar with that technique.. Might benefit others in the future as well! :-)

Please Sign in or register to post replies

Write your reply to:

Draft