Copied to clipboard

Flag this post as spam?

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


  • Matt 358 posts 841 karma points
    Apr 16, 2018 @ 10:22
    Matt
    0

    dynamic drop down menu

    Hello all,

    I'm setup a dynamic navigation menu. Single items work fine however I'm just wondering if someone can point me in the direction of how I can setup dynamic drop down items within the nav?

    Here is my code which I used for the dynamic nav bar.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{ 
        var homePage = Umbraco.TypedContentAtRoot().FirstOrDefault();
        var menuPages = homePage.Children().Where(f=>f.IsVisible());
        var isHomePage = homePage.Id == Model.Content.Id;
     }
      <div data-collapse="medium" data-animation="default" data-duration="400" class="navbar w-nav">
            <div class="nav-container w-container">
                <nav role="navigation" class="nav-menu w-nav-menu">
    
    
                    <a href="@homePage.Url" class="@(isHomePage ? "active" : string.Empty) nav-link w-nav-link">@homePage.Name</a>
    
    
    
        @foreach (var item in menuPages)
        {
            //set active class if current page is in this section
            var activeClass = !isHomePage && Model.Content.Path.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Contains(item.Id.ToString()) ? "active" : string.Empty;
    
    
    
                            <a href="@item.Url" class="@activeClass nav-link w-nav-link">@item.Name</a>
    
    
    
        }
        </nav>
    <div class="menu-button w-nav-button">
    <div class="mobile-icon w-icon-nav-menu"></div>
        </div>
        </div>
    </div>
    

    What I'm trying to achieve is the following format;

    • Nav Item
    • Nav Item
    • Nav Item
      • Sub Nav Item
      • Sub Nav Item
    • Nav Item

      ect ect

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Apr 16, 2018 @ 11:10
    Michaël Vanbrabandt
    0

    Hi Matt,

    have a look at this package:

    https://our.umbraco.org/projects/website-utilities/meganav/

    Maybe it can be the solution you are looking for.

    Have a nice day!

    /Michaël

  • Matt 358 posts 841 karma points
    Apr 16, 2018 @ 11:31
    Matt
    0

    Hello Michael,

    Thanks for the quick reply, do you know how its possible to achieve the following without using a 3rd party extension?

    Matt

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Apr 16, 2018 @ 11:42
    Michaël Vanbrabandt
    0

    Hi Matt,

    this is possible, but then we will need to know your way of how your content structure is build in the backoffice and how you want it to become.

    Can you give us more details about this or even screenshots?

    Thanks!

    /Michaël

  • Matt 358 posts 841 karma points
    Apr 16, 2018 @ 12:30
    Matt
    0

    Hello Michael,

    I've attached how my content looks.

    enter image description here

    For e.g I would like my menu dynamic menu to look like;

    • Home
    • About - (Not a page just a reference)
      • About us
      • IG Team
    • Page
    • Page
  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Apr 16, 2018 @ 12:39
    Michaël Vanbrabandt
    0

    Hi Matt,

    the meganav package is really usefull for this kind of tasks, what's the reason not to use 3th party tools?

    /Michaël

  • Matt 358 posts 841 karma points
    Apr 16, 2018 @ 12:58
    Matt
    0

    Hello Michael,

    Unfortunately the organisation this is being designed for are quite strict on allowing 3rd party applications/extensions, and where possible they try to avoid using them.

    If its going to be quite complex to do I can put the extension forward and see what they say.

    Is not an easy task?

    Matt

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Apr 16, 2018 @ 13:08
    Michaël Vanbrabandt
    0

    Hi Matt,

    You can do it yourself, but the way you describe makes me wondering why spent time on it when there is a package for it?

    If you really want to make it yourself you need to find a way of making it transparent for other users.

    1. Make a Menu doc type with a content picker ( which allows you to pick a content node from your content section )
    2. Set this Menu doc type to allow children of the same Menu doc type
    3. Make a root content node Menu Structure which contains the menu items structure
    4. Make a partial which renders the menu structure using the descendants of the Menu Structure node

    Hope this helps!

    /Michaël

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Apr 16, 2018 @ 13:56
    Nik
    0

    Hi Matt,

    What version of Umbraco are you currently running? You could do this using a Nested Content property editor (which is part of Umbraco as of, I think, 7.7). You would take a very similar approach to what Michaël has suggested, except you don't need to create a content node in your content tree. Instead you build a Menu Builder data type and add it as a property on your Root Node.

    This also gives you the opportunity to have multiple menu item types such as "Menu Label" and "Menu Item", where the Item has a link picker (incase you wanted to link externally) and the Label just has a text field. Both the Label and the Item could inherit from a "Menu item" that allows for a sub-menu to be added so you can perform your nesting.

    Just another idea :-)

    Nik

  • Matt 358 posts 841 karma points
    Apr 16, 2018 @ 14:06
    Matt
    0

    Hello Nik,

    I'm currently using version 7.9.2

    Is there a tutorial around to do such method?

    Matt

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Apr 16, 2018 @ 14:45
    Nik
    1

    Hi Matt,

    There isn't a tutorial but the following is a rough guide that I hope can help you get started.

    Step 1 - Creating initial document types

    1. I would create a folder called Nested content in the document types section in Umbraco (found in Settings).
    2. In this folder I could create 3 document types (without templates). Menu Item Base , Menu Item Label, and Menu Item Link. On Menu Item Label and Menu Item Link add a tab called Content and a property called Label (or something similar).
    3. Set both Menu Item Label and Menu Item Link to have a composition of Menu Item Base

    Step 2 - Create the nested content based data type

    1. Goto the Data Types node in the Developer section of Umbraco and create a folder called "Nested Content" (You don't have I just think it is better to group things together).
    2. Create a new Data Type, I'd call it Menu Builder. From the drop down of property editors, pick Nested Content.
    3. Add two document types to the options, these document types are our Menu Item Label and Menu Item Link. For each select the Content tab and in the label field add {{label}} (assuming you called the property label).

    Step 3 - Finish off your document types

    1. So, here we want to edit our Menu Item Base document type, add a tab called "Content" (same name as the tab on both the Menu Item Link and Menu Item Label document types. Add a property called "Submenu" of type "Menu Builder" (Our nested content menu builder type). Personally, I would reorder properties here so I can give the Submenu property a sort value of 10 to keep it at the bottom.

    2. The Menu Item Link needs to have a property added to it that allows you to add a link, be it a link picker for internal links or something else. That decision is up to you.

    So, at this point you should have 3 new document types that have a structure that appears similar to this:

    Menu Item Base - Content tab - Submenu Property Menu Item Label - Content tab - Label Property, Submenu Property Menu Item Link - content tab - Label Property, Link Property, Submenu Property

    Step 4) (I'm going to speed up a bit here now) Add a property to your Home Page of type Menu Builder. You might want to add a new tab for it but that is up to you.

    At this point, you can now build your menu in the back office.

    All that is left now is rendering out you menu, I say all, it will take a bit of time but effectively a property that is of type Nested Content will store it's information as an IEnumerable<IPublishedContent> so, assuming you've called your Menu property 'Menu' you can access it like this:

    var menuEntries = Model.Content.Site().GetPropertyValue<IEnumerable,<IPublishedContent>>("menu");
    

    You can then look around them as follows:

    foreach(var menuItem in menuEntries)
    {
        //do something here, check if this is a label or a link as you might want to render it out differently.
    
        //Now handle the submenu
        var subMenu = menuItem.GetPropertyValue<IEnumerable<IPublishedContent>>();
       if(subMenu != null && subMenu.Any()) //Checking we have entries
       {
             //In here you would handle the sub menu items.
             //Realistically, you would probably have some sort of
             // reccursive function call to handle this so depth of nested menus doesn't matter.
       }
    }
    

    I hope that this helps you :-)

    Nik

  • Matt 358 posts 841 karma points
    Apr 17, 2018 @ 07:05
    Matt
    0

    Hello Nik,

    thanks for that detailed explaination, I've installed a test copy of umbraco and will trial these suggested options.

    I've tried to install the Meganav extention which says everything has installed fine.

    However when I go to the next step of the usage in the documentation i don't see Meganav Data Type? I'm on version 7.9.2

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Apr 17, 2018 @ 07:21
    Nik
    0

    Hi Matt,

    Did you install it via NuGet or the Package Manager in the back office? I do find that the back office doesn't always show up new editors, so you might be getting an annoying caching issue. If you haven't, it could be worth restarting the website and seeing if it turns up then.

    Thanks,

    Nik

  • Matt 358 posts 841 karma points
    Apr 17, 2018 @ 07:23
    Matt
    0

    Hello Nik,

    Thanks for the quick reply, I installed via Nugget and tried multiple fresh installs/versions but still no joy.

    Matt

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Apr 17, 2018 @ 07:26
    Nik
    0

    Hey Matt,

    No problem, what version of .Net is your project set up for? I've had an issue where I was set up as .Net 4.5, but needed to be 4.6 to get MegaNav to work. If you are currently 4.5, update your project to 4.6.1 and re-install MegaNav

    update-package Cogworks.MegaNav -reinstall -ignoreDependencies
    

    I like the ignoreDependecies tag in that command as it means it won't try and re-install loads of things that aren't needed.

    Nik

  • Matt 358 posts 841 karma points
    Apr 17, 2018 @ 11:09
    Matt
    0

    Hello Nik,

    Perfect that worked a dream, I was running 4.5

    I'm still trying to get my head around how Meganav works. I followed the screenshots in the document and set my new data type up.

    I have a couple of questions;

    1) Do I need to add and set Meganav datatype on each document type I create? or just on the Home as when I set a few test pages up it looks nothing like the documentation when I'm looking to create something like;

    • Home
    • About
      • About us
      • What we do
    • Blog
    • Contact

    2) When I use the "ExampleNavigation.cshtml" which comes with Meganav I get the following error So I cant actually see how anything looks or works.

    The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel`1[Umbraco.Web.PublishedContentModels.Home]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Cogworks.Meganav.Models.MeganavItem]'. 
    
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
     Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel`1[Umbraco.Web.PublishedContentModels.Home]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Cogworks.Meganav.Models.MeganavItem]'.
    

    Thanks in advance,

    Matt

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Apr 17, 2018 @ 11:17
    Nik
    0

    Hi Matt,

    Okay, so if you are only having 1 navigation on your site, then you only need to add it to your root node. If you were displaying some sort of secondary menu on certain pages you might look at adding another one somewhere else for that.

    When you add nodes to the mega nav, you can move them in 4 directions.

    Up Down, left, and Right.

    Up and down sorts their position, left and right sorts their level.

    so, I would create 4 nodes initially, Home, About, Blog, and Contact. Then, I would add About us, drag this up to beneath About and right one notch. It can be a little fiddly so just persevere with it. Repeat this for the What we do node.

    With regards to rendering it out, it looks like you are using Models Builder, which is great.

    So I'm going to assume that you've called your menu property "Menu".

    So to render the menu using the example partial use the following line of code:

    @Html.Partial("ExampleNavigation", Model.Content.Menu)
    

    This should work because MegaNav comes with the correct property converter so Models Builder knows that the property is of type IEnumerable<MeganavItem>

    Let me know how you get on.

    Nik

  • Matt 358 posts 841 karma points
    Apr 17, 2018 @ 11:42
    Matt
    0

    Hello Nik,

    Thanks once I get the menu to output I shall have a play around on the test environment and see how things work out.

    I'm still getting an error tho;

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

    I assume this is because of "Menu" when you say my menu property what do you mean?

    Thanks in advance Nik, really appreciate it

    Matt

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Apr 17, 2018 @ 11:50
    Nik
    0

    Hi Matt,

    That's no problem, happy to help. So, when you've added a property to your home Document Type, (of type MegaNav) you've given it a name. When I've said "Menu" I've assumed that is what you've called the property. If you've called the property MegaNav, then replace Menu with MegaNav, etc.

    That make sense?

    Thanks,

    Nik

  • Matt 358 posts 841 karma points
    Apr 17, 2018 @ 12:38
    Matt
    0

    Hello Nik,

    Thanks that worked a treat I've added my CSS to the code and everything works fine for non child navigation.

    My next issue if your able to help is how can I get

    @RenderNavigation(item.Children)
    

    To only display on nodes with children when you hover over them?

    Here is my CSS style which I used for drop down navs.

    <div data-hover="1" data-delay="0" class="drop-down-menu w-dropdown">
        <div class="nav-link w-dropdown-toggle">
        <div>about</div>
    </div>
    <nav class="w-dropdown-list"><a href="#" class="drop-down-link w-dropdown-link">About</a><a href="#" class="drop-down-link w-dropdown-link">What we do</a></nav>
    </div>
    

    And here is my partial view

        <div data-collapse="medium" data-animation="default" data-duration="400" class="navbar w-nav">
        <div class="nav-container w-container">
        <nav role="navigation" class="nav-menu w-nav-menu">
        @using Cogworks.Meganav.Models
        @model IEnumerable<MeganavItem>
    
        @RenderNavigation(Model)
    
        @helper RenderNavigation(IEnumerable<MeganavItem> items)
    
        {
    
            if (items.Any())
            {
                    foreach (var item in items)
                    {
    
                            <a href="@item.Url" class="nav-link w-nav-link">@item.Title</a>
    
    @RenderNavigation(item.Children)
    
                    }
            }
        }
            </nav>
        <div class="menu-button w-nav-button">
        <div class="mobile-icon w-icon-nav-menu"></div>
            </div>
            </div>
        </div>
    

    Thanks

    Matt

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Apr 18, 2018 @ 07:21
    Nik
    0

    Hi Matt,

    So, generally the contents of a navigation are structured like this:

    <ul>
        <li><a>My first entry</a></li>
        <li><a>My second entry</a></li>
    <ul>
    

    The reason for this is so that when you need to do a drop down you can look to do something like this:

    <ul>
        <li><a>My first entry</a></li>
        <li>
            <a>My second entry</a>
            <ul class="subMenu">
                <li><a>My sub menu item</a></li>
                <li><a>My second sub menu item</a></li>
            </ul>
        </li>
    <ul>
    

    This allows you to have you association between each level of the menu. From this perspective, there are hundreds of tutorials about this sort of structure online, along with css snippets etc so rather than me try and provide something for you, it would be worth having a look around when it comes to that.

    You'll want to update your razor logic (particularly focussing on your helper) and think about how that renders out, for example, one option might be to adjust it so it's like this:

    @helper RenderNavigation(IEnumerable<MeganavItem> items)
    {
        if (items.Any())
        {
            <ul>
                @foreach (var item in items)
                {
                    <li>
                        <a href="@item.Url" class="nav-link w-nav-link">@item.Title</a>
                        @RenderNavigation(item.Children)
                    </li>
                }
            </ul>
        }
    }
    

    Thanks,

    Nik

  • Matt 358 posts 841 karma points
    Apr 18, 2018 @ 07:44
    Matt
    0

    Hello Nik,

    Thanks for this response, can I only achieve the drop down method if I use the list method?

      <div data-collapse="medium" data-animation="default" data-duration="400" class="navbar w-nav">
        <div class="nav-container w-container">
          <nav role="navigation" class="nav-menu w-nav-menu"><a href="#" class="nav-link w-nav-link">Home</a><a class="nav-link w-nav-link">About us</a>
            <div data-hover="1" data-delay="0" class="drop-down-menu w-dropdown">
              <div class="nav-link w-dropdown-toggle">
                <div>Your rights</div>
              </div>
              <nav class="w-dropdown-list"><a href="#" class="drop-down-link w-dropdown-link">Complaints procedure</a><a href="#" class="drop-down-link w-dropdown-link">Your rights</a></nav>
            </div>
          <div class="menu-button w-nav-button">
            <div class="mobile-icon w-icon-nav-menu"></div>
          </div>
        </div>
      </div>
    

    This is my complete HTML menu which works fine outside of meganav and works for single menu items. Is there no way to use this sort of menu structure with meganav?

    Really appreciate your help.

    Matt

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Apr 18, 2018 @ 09:17
    Nik
    0

    Hi Matt,

    I've never seen that sort of navigation structure and it's not one I would choose to use, I'm sure you could find a way but you'd probably need some javascript to help it.

    Maybe someone else could help with that but it's more a front end problem now rather than an Umbraco specific issue :-)

    Nik

  • Matt 358 posts 841 karma points
    Apr 24, 2018 @ 08:13
    Matt
    0

    Thanks for the help Nik.

    Anyone else here have any ideas how I can use the above nav with MegaNav? I have all the html/css just need help with adding meganav around it.

    Matt

  • Matt 358 posts 841 karma points
    Apr 24, 2018 @ 09:36
    Matt
    0

    Also I tried installing another test environment and I'm getting the following error again;

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

    I've checked my document type and "mainNavigation" is there...

    Soon as I get this menu resolved I'm going to create a tutorial so others don't suffer with the same headache as me!

  • kalgi kansara 18 posts 108 karma points
    Jul 28, 2021 @ 13:04
    kalgi kansara
    0

    Can you share tutorial link here??

  • Zubair Hasan 18 posts 88 karma points notactivated
    Oct 30, 2018 @ 16:57
    Zubair Hasan
    0

    Need someone help. I'm implementing the meganav but different errors occurred. I have read the discussion of Nik and Matt and try this but failed with an error.

    Error is "Object reference not set to an instance of an object."

    @{
    var homeNode = Model.Content.Site().OfType<Menus>();
    @Html.Partial("ExampleNavigation", homeNode.MenuBar)
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft