Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm an trying to build a menu using response from json. I am currently able to build the parent menu but finding ut difficult to append the sub menus. When i click on the parent menu , it doesn't drop down to show the sub menu.


I have added a sample of the json response i am to use to build the menu and also the function i am using..

Also i have added a sample of how my html looks like

What I have tried:

JavaScript
{
    status: "Success",
    menus: [
    {
    menu_icon: null,
    menu_name: "Stock",
    submenu: [
    {
    menu_name: "Manage Stocks",
    controller_name: "ManageStocks",
    action_name: "ManageStocks"
    }
    ]
    },
    {
    menu_icon: null,
    menu_name: "Property",
    submenu: [
    {
    menu_name: "Manage Property",
    controller_name: "ManageProperty",
    action_name: "ManageProperty"
    }
    ]
    }
    ]
    }






<ul class="nav nav-main" id="menuList">
                <li class="nav-parent">
                    <a class="nav-link" asp-area="" asp-controller="Home" asp-action="Index">
                        class="fa fa-home">
                        <span>Dashboard</span>
                    </a>
                </li>
                <li class="nav-parent">
                    <a class="nav-link" href="#">
                        <span>Pages</span>
                    </a>
                    <ul class="nav nav-children">
                        <li>
                            <a class="nav-link" href="pages-signup.html">
                                Sign Up
                            </a>
                        </li>
                    </ul>
                </li></ul>






function buildMenu(parent, item) {
           $.each(item, function () {
               var li = $('<li class="nav-parent"><a class="nav-link" href="#"><span>' + this.menu_name + '</span></a></li>');
               li.appendTo(parent);
               if (this.submenu != null) {
                   var ul = $('<ul class="nav nav-children"></ul>');
                   ul.appendTo(li);
                   buildMenu($(ul), this.submenu);
               }

           });
       };
Posted
Updated 25-Jan-19 17:34pm
v8
Comments
ZurdoDev 21-Jan-19 7:17am    
You'll need to debug your code.

1 solution

You're not showing how you're invoking the dropdown, which doesn't help, however....

You're using "this" a lot, and inappropriately.

JavaScript
function buildMenu(parent, item) {
           $.each(item, function () {
               var li = $('<li class="nav-parent"><a class="nav-link" href="#"><span>' + item.menu_name + '</span></a></li>');
               li.appendTo(parent);
               if (item.submenu != null) {
                   var ul = $('<ul class="nav nav-children"></ul>');
                   ul.appendTo(li);
                   buildMenu($(ul), item.submenu);
               }

           });
       };


I don't know what your CSS looks like, but this should resolve the issues you're having with the menu building out.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900