Copied to clipboard

Flag this post as spam?

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


  • Anders 13 posts 133 karma points
    Jun 26, 2017 @ 18:54
    Anders
    0

    DropDown with highlights and removed mainlink

    Hi All

    I've created a dropdown menu, and I'm trying remove the "link" from the main drop down item. (The menuitem that is hovered, to activate the dropdown)

    Furthermore, I'm trying to highlight the main item, and the selected item in the dropdown. I use class="@(CurrentPage.Id == item.Id ? "current_page_item" : null)" to highlight the current page.

    Is there an elegant way to do it? When I try coding it, it gets a bit too messy, and I thought there should be a more cleaner way to solve it.

    The complete partial code:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{ var selection = CurrentPage.Site(); }
    
    <nav id="nav">
        @Traverse(selection)
    </nav>
    
    @helper Traverse(dynamic node)
    {
    var maxLevelForSitemap = 10;
    var selection = node.Children.Where("Visible").Where("Level <= " + maxLevelForSitemap);
    
    if (selection.Any())
    {
        <ul>
            <li class="@(CurrentPage.Url == "/" ? "current_page_item" : null)"><a href="/">Forside</a></li>
    
            @foreach (var item in selection)
            {
                <li class="[email protected] @(CurrentPage.Id == item.Id ? "current_page_item" : null)">
                    <a href="@item.Url">@item.Name</a>
                    @TraverseDescendants(item)
                </li>
            }
        </ul>
    }
    }
    
    @helper TraverseDescendants(dynamic node)
    {
    var maxLevelForSitemap = 10;
    var selection = node.Children.Where("Visible").Where("Level <= " + maxLevelForSitemap);
    
    if (selection.Any())
    {
        <ul>
            @foreach (var item in selection)
            {
                <li class="[email protected] @(CurrentPage.Id == item.Id ? "current_page_item" : null)">
                    <a href="@item.Url">@item.Name</a>
                </li>
            }
        </ul>
    }
    }
    
  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Jun 26, 2017 @ 22:19
    Alex Skrypnyk
    1

    Hi Anders

    If I understood the logic right, it should work:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{ var selection = CurrentPage.Site(); }
    
    <nav id="nav">
        @Traverse(selection)
    </nav>
    
    @helper Traverse(dynamic node)
    {
        var maxLevelForSitemap = 10;
        var selection = node.Children.Where("Visible").Where("Level <= " + maxLevelForSitemap);
    
        if (selection.Any())
        {
            <ul>
                @if (node.Level == 1)
                {
                    <li class="@( CurrentPage.Url == "/" ? "current_page_item" : null)"><a href="/">Forside</a></li>
                }
    
                @foreach (var item in selection)
                {
                    <li class="[email protected] @(CurrentPage.Id == item.Id ? "current_page_item" : null)">
                        <a href="@item.Url">@item.Name</a>
                        @Traverse(item)
                    </li>
                }
            </ul>
        }
    }
    

    Just check level of current node in Traverse helper, and you don't need TraverseDescendants

    Thanks,

    Alex

  • Anders 13 posts 133 karma points
    Jun 27, 2017 @ 17:23
    Anders
    0

    Hi Alex

    Thanks for answering my post.

    I't works great with the (node.Level == 1) - the code is much simpler.

    Do you have any idea how to solve the highlight part of the question? and how to make the menuitem with the dropdown "nonlinkable". So its only the children in the dropdown menu, that is clickable?

    Thanks :)

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Jun 27, 2017 @ 18:08
    Alex Skrypnyk
    1

    Hi Anders

    Try to use this code for "nonlinkable" items:

                    @foreach (var item in selection)
                    {
                        <li class="[email protected] @(CurrentPage.Id == item.Id ? "current_page_item" : null)">
                            @{
                                var itemHasChildren = item.Children.Where("Visible").Where("Level <= " + maxLevelForSitemap).Any();
                            }
                            @if (itemHasChildren)
                            {
                                <a href="@item.Url">@item.Name</a>
    
                                @Traverse(item)
                            }
                            else
                            {
                                <a href="javascript:void(0)">@item.Name</a>
                            }
                        </li>
                    }
    
  • Anders 13 posts 133 karma points
    Jun 29, 2017 @ 07:11
    Anders
    0

    Hi Alex

    Thanks for answering, again :)

    Your example worked perfectly when I switched the javascript:void(0) and @item.Url.

    So the if statement looks like:

    @if (itemHasChildren)
    {
        <a href="javascript:void(0)">@item.Name</a>    
        @Traverse(item)
    }
    else
    {
        <a href="@item.Url">@item.Name</a>
    }
    

    Why use javascript:void(0) instead of just # - Is it better practice?

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Jun 29, 2017 @ 07:27
    Alex Skrypnyk
    1

    Because if you press on link with # it will move your browser window to the top of the page, I don't like it

    Of course you can use some js handler for it, but it's easier to use javascript:void(0)

    Alex

  • Anders 13 posts 133 karma points
    Jun 29, 2017 @ 07:28
    Anders
    0

    Makes sense !

    Thanks for the tip !

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Jun 27, 2017 @ 18:10
    Alex Skrypnyk
    0

    Code for highlighting the item:

    <li class="[email protected] @(item.Path.Contains(CurrentPage.Id) ? "current_page_item" : null)">
    
  • Anders 13 posts 133 karma points
    Jun 29, 2017 @ 07:14
    Anders
    0

    As I read it, I was supposed to switch the CurrentPage.Id == item.Id with item.Path.Contains(CurrentPage.Id)

    The highlighting code throws an error:

    The best overloaded method match for 'string.Contains(string)' has some invalid arguments

    Guess it can't find the item.Path.Contains

    Do you have any ideas what the problem is here?

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Jun 29, 2017 @ 10:28
    Alex Skrypnyk
    0

    Hi Anders

    I rewrote your code to strongly typed, try it:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    
    @{ var selection = Umbraco.AssignedContentItem.Site(); }
    
    <nav id="nav">
        @Traverse(selection)
    </nav>
    
    @helper Traverse(IPublishedContent node)
    {
    
    var maxLevelForSitemap = 10;
    var selection = node.Children.Where(x => x.IsVisible()).Where(x => x.Level <= maxLevelForSitemap);
    
    if (selection.Any())
    {
            <ul>
                @if (node.Level == 1)
                {
                    <li class="@( Umbraco.AssignedContentItem.Url == "/" ? "current_page_item" : null)"><a href="/">Forside</a></li>
                }
    
                @foreach (var item in selection)
                {
                    <li class="[email protected] @(item.Path.Contains(Umbraco.AssignedContentItem.Id.ToString()) ? "current_page_item" : null)">
                        @{
                            var itemHasChildren = item.Children.Where("Visible").Where("Level <= " + maxLevelForSitemap).Any();
                        }
                        @if (itemHasChildren)
                        {
                            <a href="javascript:void(0)">@item.Name</a>
                            @Traverse(item)
                        }
                        else
                        {
                            <a href="@item.Url">@item.Name</a>
                        }
                    </li>
                }
            </ul>
        }
    }
    
  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Jul 03, 2017 @ 08:25
    Alex Skrypnyk
    0

    Hi Anders

    Did you try latest code snippet?

    Alex

  • Anders 13 posts 133 karma points
    Jul 05, 2017 @ 08:51
    Anders
    0

    Hi Alex

    Sorry for not answering to your reply.

    I've tried the code example you gave me, but it didn't work as intended.

    I don't know if I asked the right question. But to make it clearer, I'm trying to highlight the menu item that has one of its "children" activated, and highlight the activated child.

    Example:

            Menu Item1  -   Menu Item 2 - (((Menu Item 3)))
                                            Submenu item 1
                                            Submenu item 2
                                         (((Submenu item 3)))                              
                                            Submenu item 4
    

    ((( means highlighted )))

    Hope the example helps, otherwise don't hesitate to ask :)

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Jul 05, 2017 @ 16:11
    Alex Skrypnyk
    0

    Hi Anders

    Thanks, yes, I understood what did you mean.

    Try this code:

    <li class="[email protected] @(Umbraco.AssignedContentItem.IsDescendantOrSelf(item) ? "current_page_item" : "")">
    
  • Anders 13 posts 133 karma points
    Jul 05, 2017 @ 19:57
    Anders
    0

    Hi Alex

    The following error occurs:

    Severity Code Description Project File Line Suppression State Error CS1973 'IPublishedContent' has no applicable method named 'IsDescendantOrSelf' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.
    ViewsPartialsumbTopNavigation.cshtml

    Do you know how to casting the dynamic arguments or call the extensions method without the extension method syntax?

Please Sign in or register to post replies

Write your reply to:

Draft