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
    May 11, 2020 @ 11:01
    Matt
    0

    if statement adding a &&

    Hi,

    I've got the following loop which works fine;

            @* 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>
                }
    
            }
    
        }
    

    What I'm looking at changing the line;

    if (nochildren.Length < 1)
    

    To have something like

    if (nochildren.Length < 1 && (!item.Value<bool>("disableDropdown")))
    

    Basically I want to write it if has no or disableDropdown is ticked.

    Thanks

    Matt

  • Nik 1591 posts 7148 karma points MVP 6x c-trib
    May 11, 2020 @ 11:12
    Nik
    0

    Hi Matt,

    That looks like it should be okay, what problem are you having?

    Nik

  • Matt 353 posts 825 karma points
    May 11, 2020 @ 11:21
    Matt
    0

    Hi Nik,

    Hope your well.

    So even if I toggle the dropdown, the drop down doesn't disappear on the front end.

    I'm not getting no errors so its hard to try and pin point the error.

    Thanks

    Matt

  • Nik 1591 posts 7148 karma points MVP 6x c-trib
    May 11, 2020 @ 12:45
    Nik
    0

    Try this:

    @* Loop through the selection *@
        foreach (var item in selection)
        {
            var haschildren = item.Children.Any(x => x.IsVisible()).ToArray();
            if (haschildren  == false)
            {
                <a href="@item.Url" class="sub-menu-link w-nav-link">@item.Name</a>
            }
            else if(haschildren  && !item.Value<bool>("disableDropdown"))
            {
                <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>
            }
    
        }
    
  • Matt 353 posts 825 karma points
    May 11, 2020 @ 14:53
    Matt
    0

    Hi Nik,

    No joy, although I'm now getting an error;

    Compiler Error Message: CS1061: 'bool' does not contain a definition for 'ToArray' and no accessible extension method 'ToArray' accepting a first argument of type 'bool' could be found (are you missing a using directive or an assembly reference?)

    line 27

    var haschildren = item.Children.Any(x => x.IsVisible()).ToArray();
    
  • Nik 1591 posts 7148 karma points MVP 6x c-trib
    May 11, 2020 @ 20:45
    Nik
    0

    Ahh, sorry Matt, remove the .ToArray() bit from that line, I forgot when I was copying it from your initial post.

    Thanks

    Nik

  • Matt 353 posts 825 karma points
    May 12, 2020 @ 06:30
    Matt
    0

    enter image description here

    Sorry Nik I thought it was simple thing to do :(

  • Nik 1591 posts 7148 karma points MVP 6x c-trib
    May 12, 2020 @ 07:27
    Nik
    0

    Hi Matt,

    Sorry my fault for rushing again, try this (yes the ToArray() has been left there on purpose this time).

    var haschildren = item.Children.Where(x => x.IsVisible()).ToArray();
    if (haschildren.Any()  == false)
    

    You also might want to change the name of haschildren to children so it better represents what is being stored in it.

    THanks

    Nik

  • Matt 353 posts 825 karma points
    May 18, 2020 @ 07:36
    Matt
    0

    Hi Nik,

    Hope you had a good weekend.

    I tried the above but now getting the error;

    CS0019: Operator '&&' cannot be applied to operands of type 'IPublishedContent[]' and 'bool'

    Sorry noob coder here :)

    Matt

Please Sign in or register to post replies

Write your reply to:

Draft