Copied to clipboard

Flag this post as spam?

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


  • MB 273 posts 936 karma points
    Jul 11, 2016 @ 07:59
    MB
    0

    Navigation: add class to parent UL

    Using Umbraco7 I have made a PartialView based on Umbracos Sitemap. I have managed to add a "current" class to the current <li> but I would like to target the parent UL aswell.

    The reason why I need this is because when I access a page inside the UL my dropdown collapse. Example: www.sp34k.dk

    If I had a parenttocurrent class I could target the UL with CSS and give it a display block so even after the postback, the UL is still expanded.

    A quick overview:

    <ul>
       <li>
         <ul> <-This guy needs a Current/Parent Class
           <li class="current">
              Here you are
           </li>
         </ul>
    </li>
    

    Partial view:

        @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{ var selection = CurrentPage.Site(); }
    
        @Traverse(selection)
    
    
    @helper Traverse(dynamic node)
    {
    var maxLevelForSitemap = 3;
    
    var selection = node.Children.Where("Visible").Where("Level <= " + maxLevelForSitemap);
    
    if (selection.Any())
    {
        <ul>
            @foreach (var item in selection)
            {
               string cssClass = @item.Id != CurrentPage.Id ? "" : "current";
    
                <li class="@cssClass">
                    <a href="@item.Url">@item.Name</a>
    
                    @Traverse(item)
                </li>
            }
        </ul>
    }
    }
    
  • Tom Steer 161 posts 596 karma points
    Jul 11, 2016 @ 09:01
    Tom Steer
    2

    Hi Mike,

    You could add the following just before the UL:

    var ulCssClass = selection.Any(x => x.Id == CurrentPage.Id) ? "current" : "";
    
    <ul class="@ulCssClass">
    

    Cheers, Tom

  • MB 273 posts 936 karma points
    Jul 11, 2016 @ 11:36
    MB
    0

    Hey Tom,

    Thank you very much for your reply! I tried inserting the line you wrote and it triggeres this issue inside the Any()

    "cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type"

    I'm not sure exactly what that means.

  • Dennis Adolfi 1082 posts 6450 karma points MVP 6x c-trib
    Jul 11, 2016 @ 12:05
    Dennis Adolfi
    0

    Try:

    var ulCssClass = selection.Any("Id == " + CurrentPage.Id) ? "current" : "";
    
  • MB 273 posts 936 karma points
    Jul 11, 2016 @ 12:20
    MB
    0

    Hey buddy!

    If I insert that line I don't get an error line, but on the frontend it says: Error loading Partial View script (file: ~/Views/MacroPartials/navigation.cshtml)

  • Dennis Adolfi 1082 posts 6450 karma points MVP 6x c-trib
    Jul 11, 2016 @ 12:26
    Dennis Adolfi
    0

    Hi! :)

    That a generic error message, more info can be found in you logfile in /App_Data/logs. Can you check your latest logfile and post the last error message?

  • MB 273 posts 936 karma points
    Jul 11, 2016 @ 12:29
    MB
    0

    Dennis, this is the latest log file: http://sp34k.dk/error.txt

  • Dennis Adolfi 1082 posts 6450 karma points MVP 6x c-trib
    Jul 11, 2016 @ 12:34
    Dennis Adolfi
    100

    Okay, lets try this:

        @helper Traverse(dynamic node)
        {
        var maxLevelForSitemap = 3;
    
        var selection = node.Children.Where("Visible").Where("Level <= " + maxLevelForSitemap);
    
        if (selection.Any())
        {
    var ulCssClass = selection.Where("Id == " + CurrentPage.Id.ToString()).Any() ? "current" : "";
            <ul class="@ulCssClass">
                @foreach (var item in selection)
                {
                   string cssClass = @item.Id != CurrentPage.Id ? "" : "current";
    
                    <li class="@cssClass">
                        <a href="@item.Url">@item.Name</a>
    
                        @Traverse(item)
                    </li>
                }
            </ul>
        }
        }
    
  • MB 273 posts 936 karma points
    Jul 11, 2016 @ 12:38
    MB
    2

    Works like a charm buddy! This has been nagging me for way too long haha.

    Thanks to both you, Dennis and Tom for running through my problem and giving a helping hand.

    Kudos to both!

  • Dennis Adolfi 1082 posts 6450 karma points MVP 6x c-trib
    Jul 11, 2016 @ 12:40
    Dennis Adolfi
    1

    Great news Mike. Glad i could help! And thank you Tom for providing the original fix. Have a great day both of you!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies