0

I have a json object like

var data = [
        {
        "parent": "Home",
        "child": [

                ]
        },
        {"parent": "Services",
        "child": [
                {"title":"Infrastruture"},
                {"title":"Development"}

                ]
        },
        {
        "parent": "Sector",
        "child": [
                {"title":"Recruitment Consultant"},
                {"title":"Change Management"},
                {"title":"Industrial Relations"}
                ]
        },          
        ]

I want to make a horizontal menu with respect to this json. I am new json object.So how can i do this?

2
  • 2
    Have you tried anything? Commented Feb 21, 2013 at 11:54
  • yes.. i did this using jq widgets.but there i used id. But i want the json like the above code mentioned. Commented Feb 21, 2013 at 11:55

2 Answers 2

1

This should get it done.

Markup

<ul id="nav"></ul>

JavaScript

var data = [
    {
    "parent": "Home",
    "child": [

            ]
    },
    {"parent": "Services",
    "child": [
            {"title":"Infrastruture"},
            {"title":"Development"}

            ]
    },
    {
    "parent": "Sector",
    "child": [
            {"title":"Recruitment Consultant"},
            {"title":"Change Management"},
            {"title":"Industrial Relations"}
            ]
    },          
    ];
$(function() {
    var nav = $("#nav");
    $.each(data,function(idx, obj){
        if(obj.child.length>0)
        {
            nav.append('<li><a href="#">'+obj.parent+'</a><ul id="'+obj.parent+'">'); //Create Parent menu and attach Child menu items list with ID same as parent menu name.
            var parent = $("#"+obj.parent); //Select this parent to insert child items.

            //Insert child menu items.
            $.each(obj.child, function(idx, childobj){
                parent.append('<li><a href="#">'+childobj.title+'</a></li>');
            });
            nav.append('</ul></li>'); //Close each tag.
        }
        else
        {
            nav.append('<li><a href="#">'+obj.parent+'</a></li>'); //No child menu items present, just create parent menu.
        }
    });
});

This could be semantically wrong since we've got so much of markup coupled with JS (not a good idea), but as long as structure remains the same, it is fine to do this way.

Demo Fiddle

P.S.: I'm leaving you with the styling part. Hint: Provide classes along with markup in JS itself. ;-)

Sign up to request clarification or add additional context in comments.

6 Comments

If i have another subchild field for recruitment menu..How can i integrate that?
@NithinViswanath: Then you'll have to add another $.each under new condition within if block, and that should be quite easy to do from above example.
@NithinViswanath: You're asking to add another child to your data JSON object or you want to know how to create another sub-menu from child menu item?
i know that..but how to create sub child menu?
@NithinViswanath: Dude!! as I said its pretty simple if you understood how above code in my answer works, but if you just want code to copy-paste then its not what SO is exactly made for.
|
0

You can access your json like a Javascript Object. You can do things like this:

for (var i = 0; i < data.length; i++) {

    createHeaderLink(data[i].parent);
    createChildLinks(data[i].child);

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.