Copied to clipboard

Flag this post as spam?

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


  • Nate M 18 posts 148 karma points
    Feb 22, 2022 @ 22:23
    Nate M
    0

    How to get content from parent node to display in child nodes?

    I have a navigation menu in a parent node. I need that menu to propagate down to all the child nodes. This should be much like the navigation of the root directory, where all the pages inside the root display the same navigation bar.

    Here is what I have currently:

    //Root Navigation
    var primaryNav = site.Children.Where(x => x.DocumentTypeAlias == "primaryNavigationContainer").FirstOrDefault();
    var navItemCollection = primaryNav?.Children.Where(x => x.DocumentTypeAlias == "primaryNavigationItem").ToList();
    
    var ancestors = Model.Content.Ancestors().Where("Visible").OrderBy(x => x.Level);
    
    //Navigation Inside Directory
    var content = Model.Content;
    var subNav = content.Children.Where(x => x.DocumentTypeAlias == "sectionNavigationContainer").FirstOrDefault();
    var subNavItemCollection = subNav?.Children.Where(x => x.DocumentTypeAlias == "sectionNavItem").ToList();
    
    //Get the URL for the current page
    var url = HttpContext.Current.Request.Url.AbsolutePath;
    bool hasSubNav = false;
    
    //If URL to the page contains a specific string
    if(url.ToString().ToUpper().Contains("StringSpecifier")){
        primaryNav = subNav;                       //Replace the primary navigation with the section sub navigation
        navItemCollection = subNavItemCollection;  //Replace the primary collection with the section sub navigation collection
        hasSubNav = true;                          //Set hasSubNav property
    }
    

    This displays fine in the Directory node, but it does not propagate to the pages inside the directory node.

    Can anyone help me with how to get the sub navigation to display for the pages inside the directory node?

    • Nate M
  • Ambert van Unen 175 posts 819 karma points c-trib
    Feb 23, 2022 @ 08:17
    Ambert van Unen
    1

    If I understand correctly, childs of a Directory aren't shown when you navigate to a child of that directory.

    This is the result of var content = Model.Content.

    You could perhaps do it like the following:

    var content = Model.Content.AncestorOrSelf<Directory>();

    This is written out of my head as I don't have a V7 running, but something similar like this should exist I ebelieve. It would get the current content, and if thats not of type Directory, would look upwards in the tree untill it finds one such type.

  • Korey Kautzer 5 posts 76 karma points
    Feb 23, 2022 @ 09:15
    Korey Kautzer
    1

    The read-only children property returns a live HTMLCollection which contains all of the child elements of the element upon which it was called.

    Element.children includes only element nodes. To get all child nodes, including non-element nodes like text and comment nodes, use Node.childNodes.

    epayitonline

  • Nate M 18 posts 148 karma points
    Feb 23, 2022 @ 21:39
    Nate M
    100

    I stumbled across what I was looking for.

    var content = Model.Content;
    
    //Should have been 
    
    var content = Model.Content.Parent;
    

    Thank you Ambert and Korey. Your responses led me in the right direction.

Please Sign in or register to post replies

Write your reply to:

Draft