0

I am trying to add a json to the menu and show it on the frontend. I have created an HTML tree structure through recursive calling in javascript and I can see that structure seems ok, in the web console. The problem is that I am just seeing the parent object in the menu and the children objects do not appear as a submenu. I am not able to figure out what is the problem in the code and need some help.

Here is my javascript code:

$(function() {
    addMenuItems = function(parentMenuItem, currentNode){
        var divElement = document.createElement('div');
        // set the text for the list item
        divElement.innerHTML = currentNode.name;
        var listItem = document.createElement('li');
        listItem.appendChild(divElement);


        var currentMenuItem = document.createElement('ul');

        console.log("html current: " + parentMenuItem.innerHTML);
        console.log("html parent: " + listItem);

        // create the menu
        var child;

        for (child = 0; child < currentNode.children.length; child++){
            addMenuItems(currentMenuItem, currentNode.children[child]);
        }

        // add a submenu list to this list
        if(currentNode.children.length > 0){
            listItem.appendChild(currentMenuItem);
        }


        // add the child to the menu
        parentMenuItem.appendChild(listItem);

        console.log("html: " + parentMenuItem.innerHTML);

    };
    //End of function

    $("#menu").hide()

    var identifiedObjects = [];

    $("#Boma").mousedown(function(e) {
        if(e.button==2){
            var browserCoord = {x:e.pageX, y:e.pageY};
            console.log(browserCoord);
        }
    });

    document.getElementById("Boma").oncontextmenu = function () {return false;}

    $("#Boma").mousedown(function(e) {
        if(e.button==2)
        {
            $("#menu").css('left', e.pageX+5);
            $("#menu").css('top', e.pageY+5);
            $("#menu").fadeIn(100);
            $( "#menu" ).menu();
        }
    });

    $(document).click(function(e) {
        if(e.button==0){
            $("#menu").fadeOut(80);
        }
    });

    jQuery.getJSON("/new_veg.json", (data) => {
        console.log(data);
        addMenuItems(document.getElementById('menu'), data);
    });


});

Here is the json:

{
    "name":"vegetation",
    "id":"2",
    "children":[
        { 
            "name":"landuse",
            "id":"2.1",
            "children":[
                {
                    "name":"forest area",
                    "id":"2.1.1",
                    "children":[]
                },
                {
                    "name":"plantation",
                    "id":"2.1.2",
                    "children":[]
                }
            ]
        }
    ]
}
Salman
  • 393
  • 1
  • 7
  • 23
  • Not sure what you’re asking, it seems to at least build the structure fine: https://jsfiddle.net/yvr6ve79/ Not sure what more you expect at this point. – CBroe Jul 24 '17 at 10:04
  • @CBroe I want to see this structure in a right click menu in the form of menu and submenu. Currently I just see vegetation in the menu and not the rest of the json. – Salman Jul 24 '17 at 10:08
  • Well then create your own [mcve] that reproduces the problem, please. – CBroe Jul 24 '17 at 10:09
  • Check [this](https://stackoverflow.com/questions/19003285/creating-a-menu-from-json) and [this](https://stackoverflow.com/questions/9869332/build-a-menu-from-json) – Durga Jul 24 '17 at 10:16
  • @Durga Thanks alot! I managed to solve the problem. – Salman Jul 24 '17 at 11:33
  • @Salman Happy Coding :) – Durga Jul 24 '17 at 11:34

0 Answers0