Copied to clipboard

Flag this post as spam?

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


  • Johan 95 posts 264 karma points
    Nov 27, 2015 @ 14:55
    Johan
    0

    Making sure the navigation has pages

    I want to make sure my navigation has a page before looping. I know this can be solved with an if-statement but I don't know where in my code to put it

    Here is my navigation code:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        int num = 1;    
        var rootNode = Model.Content.AncestorOrSelf(1);
    
        // home node is hardcoded - this might not be right?
        <ul id="menu">
            <li>
                <a href="/" class="drop">Hem</a>
            </li><!-- End Home Item -->
            @foreach (var mainNode in rootNode.Children())
            {
                int childCount = 1;
                int numChildren = mainNode.Children().Count();
                <li>
                    <a href="@mainNode.Url" class="drop">@mainNode.Name</a>
                    <div id=nav-@num class="dropdown_5columns"> 
                        <!-- Begin 2 columns container -->
                        <div class="col_5">
                            <h2>@mainNode.Name</h2>
                        </div>
    
                        @* note if you want ALL descendants change .Children to .Descendats*@
                        @foreach (var childNode in mainNode.Children().Where("Visible"))
                        {
    
                            // if first node or new set of three open the div and ul @: is used to stop razor trying to
                            // "balance" the tags
                            if (childCount == 1 || (double)childCount % 3 == 1)
                            {
                                @:<div class="col_1">
                                    @:<ul>
                            }
                            <li><a href="@childNode.Url">@childNode.Name</a></li>
    
                            // close the div and list if this is either a multiple of 3 or the last one
                            if ((double)childCount % 3 == 0 || numChildren == childCount)
                            {
                                @:</ul>
                            @:</div>
                            }
    
                            childCount++;
    
                        }
    
                    </div>
    
                </li>
                 num++;
            }
        </ul>
    }
    
  • Steve Morgan 1349 posts 4459 karma points c-trib
    Nov 27, 2015 @ 15:36
    Steve Morgan
    0

    Hi,

    Hard to test without your content but you mean this?

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        int num = 1;  
        var rootNode = Model.Content.AncestorOrSelf(1);
    
        // home node is hardcoded - this might not be right?
        <ul id="menu">
            <li>
                <a href="/" class="drop">Home</a>
                <div class="dropdown_2columns">
                    <!-- Begin 2 columns container -->
                    <div class="col_2">
                        <h2>Welcome !</h2>
                    </div>
                </div><!-- End 2 columns container -->
            </li><!-- End Home Item -->
            @if (rootNode.Children().Any())
            {
                foreach (var mainNode in rootNode.Children())
                {
                    int childCount = 1;
                    int numChildren = mainNode.Children().Count();
    
                    if(numChildren > 0)
                    { 
                    <li>
                        <a href="@mainNode.Url" class="drop">@mainNode.Name</a>
                        <div id=nav-@num class="dropdown_5columns">
                            <!-- Begin 2 columns container -->
                            <div class="col_5">
                                <h2>@mainNode.Name</h2>
                            </div>
                            @* note if you want ALL descendants change .Children to .Descendats*@
                            @foreach (var childNode in mainNode.Children())
                            {
                                // if first node or new set of three open the div and ul @: is used to stop razor trying to
                                // "balance" the tags
                                if (childCount == 1 || (double)childCount % 3 == 1)
                                {
                                    @:<div class="col_1">
                                        @:<ul>
                                }
                                <a href="@childNode.Url">@childNode.Name</a>
    
                                // close the div and list if this is either a multiple of 3 or the last one
                                if ((double)childCount % 3 == 0 || numChildren == childCount)
                                {
                                    @:
                                </ul>
                                @:
                            </div>
                                }
                                childCount++;
                            }
                        </div>
                    </li>
                    num++;
                    }
                }
            }
        </ul>
    }
    
  • Johan 95 posts 264 karma points
    Nov 27, 2015 @ 15:41
    Johan
    0

    No, that didn't work.

    Here is my content:

    enter image description here

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Nov 27, 2015 @ 15:45
    Steve Morgan
    0

    What's the exact problem / error!

    What page are you on when you see the error - is it all?

  • Johan 95 posts 264 karma points
    Nov 27, 2015 @ 15:46
    Johan
    0

    I get error:

    Error loading Partial View script (file: ~/Views/MacroPartials/MainNavigation.cshtml)

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Nov 27, 2015 @ 15:48
    Steve Morgan
    0

    Bah - it's done that tag balancing tag trick again.

    Try:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        int num = 1;  
        var rootNode = Model.Content.AncestorOrSelf(1);
    
    
        // home node is hardcoded - this might not be right?
        <ul id="menu">
            <li>
                <a href="/" class="drop">Home</a>
                <div class="dropdown_2columns">
                    <!-- Begin 2 columns container -->
                    <div class="col_2">
                        <h2>Welcome !</h2>
                    </div>
                </div><!-- End 2 columns container -->
            </li><!-- End Home Item -->
            @if (rootNode.Children().Any())
            {
                foreach (var mainNode in rootNode.Children())
                {
                    int childCount = 1;
                    int numChildren = mainNode.Children().Count();
    
                    if(numChildren > 0)
                    { 
                    <li>
                        <a href="@mainNode.Url" class="drop">@mainNode.Name</a>
                        <div id=nav-@num class="dropdown_5columns">
                            <!-- Begin 2 columns container -->
                            <div class="col_5">
                                <h2>@mainNode.Name</h2>
                            </div>
                            @* note if you want ALL descendants change .Children to .Descendats*@
                            @foreach (var childNode in mainNode.Children())
                            {
                                // if first node or new set of three open the div and ul @: is used to stop razor trying to
                                // "balance" the tags
                                if (childCount == 1 || (double)childCount % 3 == 1)
                                {
                                    @:<div class="col_1">
                                        @:<ul>
                                }
                                <a href="@childNode.Url">@childNode.Name</a>
    
                                // close the div and list if this is either a multiple of 3 or the last one
                                if ((double)childCount % 3 == 0 || numChildren == childCount)
                                {
                                    @:</ul>
                                @:</div>
                                }
                                childCount++;
                            }
                        </div>
                    </li>
                    num++;
                    }
                }
            }
        </ul>
    }
    
  • Steve Morgan 1349 posts 4459 karma points c-trib
    Nov 27, 2015 @ 15:49
    Steve Morgan
    0

    Did you know if you go to the log (stored in \App_Data\Logs\UmbracoTraceLog.txt) you can see the last error. This helps fixing these sort of things.

    Steve

  • Johan 95 posts 264 karma points
    Nov 27, 2015 @ 15:58
    Johan
    0

    I don't get an error now but it didnt solve my problem.

    I notice that you removed the .Where("Visible"). That's bad because there are some some items that I want to hide from the menu and now they're back.

    Is it possible to check whether the drop-down doesn't have childNodes(or has them hidden from the menu)?

    If a parent doesn't have children or if they're hidden, not running the loop?

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Nov 27, 2015 @ 16:08
    Steve Morgan
    0

    You've pretty much answered your own question.

    Add .Where("Visible") whereever it checks for Children.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        int num = 1;  
        var rootNode = Model.Content.AncestorOrSelf(1);
    
    
        // home node is hardcoded - this might not be right?
        <ul id="menu">
            <li>
                <a href="/" class="drop">Home</a>
                <div class="dropdown_2columns">
                    <!-- Begin 2 columns container -->
                    <div class="col_2">
                        <h2>Welcome !</h2>
                    </div>
                </div><!-- End 2 columns container -->
            </li><!-- End Home Item -->
            @if (rootNode.Children().Where("Visible").Any())
            {
                foreach (var mainNode in rootNode.Children().Where("Visible"))
                {
                    int childCount = 1;
                    int numChildren = mainNode.Children().Where("Visible").Count();
    
                    if(numChildren > 0)
                    { 
                    <li>
                        <a href="@mainNode.Url" class="drop">@mainNode.Name</a>
                        <div id=nav-@num class="dropdown_5columns">
                            <!-- Begin 2 columns container -->
                            <div class="col_5">
                                <h2>@mainNode.Name</h2>
                            </div>
                            @* note if you want ALL descendants change .Children to .Descendats*@
                            @foreach (var childNode in mainNode.Children().Where("Visible"))
                            {
                                // if first node or new set of three open the div and ul @: is used to stop razor trying to
                                // "balance" the tags
                                if (childCount == 1 || (double)childCount % 3 == 1)
                                {
                                    @:<div class="col_1">
                                        @:<ul>
                                }
                                <a href="@childNode.Url">@childNode.Name</a>
    
                                // close the div and list if this is either a multiple of 3 or the last one
                                if ((double)childCount % 3 == 0 || numChildren == childCount)
                                {
                                    @:</ul>
                                @:</div>
                                }
                                childCount++;
                            }
                        </div>
                    </li>
                    num++;
                    }
                }
            }
        </ul>
    }
    
  • Johan 95 posts 264 karma points
    Nov 27, 2015 @ 16:13
    Johan
    0

    Hmm.. Now it removed the headmenu from the navigation. I want it to remove the drop-down, not the MainMenu

    Before: enter image description here

    Now:

    enter image description here

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Nov 27, 2015 @ 17:30
    Steve Morgan
    0

    Misunderstood your first question. Just took out the first if..

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        int num = 1;  
        var rootNode = Model.Content.AncestorOrSelf(1);
    
    
        // home node is hardcoded - this might not be right?
        <ul id="menu">
            <li>
                <a href="/" class="drop">Home</a>
                <div class="dropdown_2columns">
                    <!-- Begin 2 columns container -->
                    <div class="col_2">
                        <h2>Welcome !</h2>
                    </div>
                </div><!-- End 2 columns container -->
            </li><!-- End Home Item -->
    
                foreach (var mainNode in rootNode.Children().Where("Visible"))
                {
                    int childCount = 1;
                    int numChildren = mainNode.Children().Where("Visible").Count();
    
                    if(numChildren > 0)
                    { 
                    <li>
                        <a href="@mainNode.Url" class="drop">@mainNode.Name</a>
                        <div id=nav-@num class="dropdown_5columns">
                            <!-- Begin 2 columns container -->
                            <div class="col_5">
                                <h2>@mainNode.Name</h2>
                            </div>
                            @* note if you want ALL descendants change .Children to .Descendats*@
                            @foreach (var childNode in mainNode.Children().Where("Visible"))
                            {
                                // if first node or new set of three open the div and ul @: is used to stop razor trying to
                                // "balance" the tags
                                if (childCount == 1 || (double)childCount % 3 == 1)
                                {
                                    @:<div class="col_1">
                                        @:<ul>
                                }
                                <a href="@childNode.Url">@childNode.Name</a>
    
                                // close the div and list if this is either a multiple of 3 or the last one
                                if ((double)childCount % 3 == 0 || numChildren == childCount)
                                {
                                    @:</ul>
                                @:</div>
                                }
                                childCount++;
                            }
                        </div>
                    </li>
                    num++;
                    }
                }
    
        </ul>
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft