Copied to clipboard

Flag this post as spam?

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


  • Matt 353 posts 825 karma points
    Feb 05, 2020 @ 08:12
    Matt
    0

    Hi all,

    I'm creating a sideNav using the partial view which comes with Umbraco, I've added an if statement to my code so if it has children to use another css class to generate the drop down, else use normal css.

    My issue is the css works with my if statement, but I'm getting 2 lots of Navigation;

    Here is my code

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @using Umbraco.Core.Models.PublishedContent
    @using Umbraco.Web
    
    
    @*
        This snippet creates links for every single page (no matter how deep) below
        the page currently being viewed by the website visitor, displayed as nested unordered HTML lists.
    *@
    
    <div data-collapse="medium" data-animation="default" data-duration="400" class="sidenavbar w-nav">
        <nav role="navigation" class="side-nav w-clearfix w-nav-menu">
    
            @{ var selection = Model.AncestorsOrSelf().FirstOrDefault(a => a.Level == 2).Children.Where(x => x.IsVisible()); }
    
            @* Ensure that the Current Page has children *@
            @if (selection.Count() > 0)
            {
                @* Get the first page in the children, where the property umbracoNaviHide is not True *@
                var naviCount = selection.FirstOrDefault()?.Level;
    
    
    
                @* Loop through the selection *@
                foreach (var item in selection)
                {
                    var nochildren = item.Children.Where(x => x.IsVisible()).ToArray();
                    var haschildren = item.Children.Where(x => x.IsVisible()).ToArray();
                    if (nochildren.Length < 1)
                    {
                        <a href="@item.Url" class="sub-menu-link w-nav-link">@item.Name</a>
                    }
                    else
                    {
                        <div data-delay="0" class="sub-nav-drop-down w-dropdown">
                            <div class="sub-menu-drop-down-toggle sub-menu-drop-link w-dropdown-toggle">
                                <div class="w-icon-dropdown-toggle"></div>
                                <div>@item.Name</div>
                            </div>
                            <nav class="dropdown-list sub-menu w-dropdown-list">
                                @ChildPages(haschildren)
                            </nav>
                        </div>
                    }
    
                    @* if this child page has any children, where the property umbracoNaviHide is not True *@
                    {
                        var children = item.Children.Where(x => x.IsVisible()).ToArray();
                        if (children.Length > 0)
                        {
                            @* Call our helper to display the children *@
                            @ChildPages(children)
                        }
                    }
    
                }
    
            }
    
            @helper ChildPages(IPublishedContent[] selection)
            {
                @* Ensure that we have a collection of pages *@
                if (selection.Length > 0)
                {
                    @* Get the first page in pages and get the level *@
                    var naviCount = selection[0].Level;
    
                    foreach (var item in selection)
                    {
    
                        <a class="sub-menu-link w-nav-link" href="@item.Url">@item.Name</a>
    
                        @* if the page has any children, where the property umbracoNaviHide is not True *@
                        {
                            var children = item.Children.Where(x => x.IsVisible()).ToArray();
                            if (children.Length < 3)
                            {
                                @* Recurse and call our helper to display the children *@
                                @ChildPages(children)
                            }
                        }
                    }
                }
            }
        </nav>
        <div data-ix="simple-menu-button" class="simple-menu-button w-nav-button">
            <div class="line-1 simple"></div>
            <div class="line-2 simple"></div>
            <div class="line-3 simple"></div>
        </div>
    </div>
    

    See image below, test 3 and test 4 are children of test 2 and are inside the dropdown as well as outside the drop down.

    enter image description here

  • Ambert van Unen 175 posts 817 karma points c-trib
    Feb 05, 2020 @ 08:32
    Ambert van Unen
    0

    You also call the @Childpages two times , so it makes sense ;-)

    First in the:

    foreach(var item in selection){ .. }
    

    You have an If and an else, but also a piece of code that always executes. In the else you call "@Childpages", but also in the code below that

    @* if this child page has any children, where the property umbracoNaviHide is not True *@
                { ... }
    

    Delete the bottom one, and it'll work ;-)

  • Matt 353 posts 825 karma points
    Feb 05, 2020 @ 08:43
    Matt
    0

    Hmmm thanks, although I removed it and I'm still getting double? Its probably something I've done wrong!

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @using Umbraco.Core.Models.PublishedContent
    @using Umbraco.Web
    
    
    @*
        This snippet creates links for every single page (no matter how deep) below
        the page currently being viewed by the website visitor, displayed as nested unordered HTML lists.
    *@
    
    <div data-collapse="medium" data-animation="default" data-duration="400" class="sidenavbar w-nav">
        <nav role="navigation" class="side-nav w-clearfix w-nav-menu">
    
            @{ var selection = Model.AncestorsOrSelf().FirstOrDefault(a => a.Level == 2).Children.Where(x => x.IsVisible()); }
    
            @* Ensure that the Current Page has children *@
            @if (selection.Count() > 0)
            {
                @* Get the first page in the children, where the property umbracoNaviHide is not True *@
                var naviCount = selection.FirstOrDefault()?.Level;
    
    
    
                @* Loop through the selection *@
                foreach (var item in selection)
                {
                    var nochildren = item.Children.Where(x => x.IsVisible()).ToArray();
                    var haschildren = item.Children.Where(x => x.IsVisible()).ToArray();
                    if (nochildren.Length < 1)
                    {
                        <a href="@item.Url" class="sub-menu-link w-nav-link">@item.Name</a>
                    }
                    else
                    {
                        <div data-delay="0" class="sub-nav-drop-down w-dropdown">
                            <div class="sub-menu-drop-down-toggle sub-menu-drop-link w-dropdown-toggle">
                                <div class="w-icon-dropdown-toggle"></div>
                                <div>@item.Name</div>
                            </div>
                            <nav class="dropdown-list sub-menu w-dropdown-list">
                                @ChildPages(haschildren)
                            </nav>
                        </div>
                    }
    
                    @* if this child page has any children, where the property umbracoNaviHide is not True *@
                    {
                        var children = item.Children.Where(x => x.IsVisible()).ToArray();
                        if (children.Length > 0)
                        {
                            @* Call our helper to display the children *@
                            @ChildPages(children)
                        }
                    }
    
                }
    
            }
    
            @helper ChildPages(IPublishedContent[] selection)
            {
                @* Ensure that we have a collection of pages *@
                if (selection.Length > 0)
                {
                    @* Get the first page in pages and get the level *@
                    var naviCount = selection[0].Level;
    
                    foreach (var item in selection)
                    {
    
                        <a class="sub-menu-link w-nav-link" href="@item.Url">@item.Name</a>
                    }
                }
            }
        </nav>
        <div data-ix="simple-menu-button" class="simple-menu-button w-nav-button">
            <div class="line-1 simple"></div>
            <div class="line-2 simple"></div>
            <div class="line-3 simple"></div>
        </div>
    </div>
    

    Then If I remove the following;

                    foreach (var item in selection)
                {
    
                    <a class="sub-menu-link w-nav-link" href="@item.Url">@item.Name</a>
                }
    

    It disappears as well as the content inside of my drop down disappears? weird...

  • Ambert van Unen 175 posts 817 karma points c-trib
    Feb 05, 2020 @ 08:57
    Ambert van Unen
    100

    Try removing

     @* if this child page has any children, where the property umbracoNaviHide is not True *@
                {
                    var children = item.Children.Where(x => x.IsVisible()).ToArray();
                    if (children.Length > 0)
                    {
                        @* Call our helper to display the children *@
                        @ChildPages(children)
                    }
                }
    

    instead, as you already do the same call in the if/else

  • Matt 353 posts 825 karma points
    Feb 05, 2020 @ 10:59
    Matt
    0

    Cheers that worked! Sorry I'm a rookie! :)

Please Sign in or register to post replies

Write your reply to:

Draft