最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Creating a Menu from JSON - Stack Overflow

programmeradmin3浏览0评论

I am trying to create a dynamic menu using jQuery UI.

I will be fetching entries from a JSON file and creating my menu items.I decided to do a small demo before i try this on a larger scale.Here's my fiddle which works the way i want it to work. Now I cant get this to work with a JSON file.

WORKING FIDDLE

Here is the JSON

var JSON = 
    {
       menu: 
          [
             {name: 'Croatia', link: '0', sub: null},
             {name: 'England', link: '1', sub: 
                [
                   {name: 'Arsenal',link: '0-0', sub: null},
                   {name: 'Liverpool',link: '0-1', sub: null},
                   {name: 'Manchester United',link: '0-2', sub: null}
                ]
             },
             {name: 'Spain', link: '2', sub: 
                [
                   {name: 'Barcelona',link: '2-0', sub: null},
                   {name: 'Real Madrid',link: '2-1', sub: null}
                ]
              },        
              {name: 'Germany', link: '3',sub: 
                [
                   {name: 'Bayern Munich',link: '3-1', sub: null},
                   {name: 'Borrusia Dortmund',link: '3-2', sub: null}
                ]
              }
          ]
    }

How can i design my entire menu using the values from the JSON where the Li's will be something like the following.

<li><a href="#3-2">Borrusia Dortmund</a>
</li>

EDIT: The question may sound like i have not tried anything, but i have. its just the JSON part which i cannot understand, Jsfiddle does not read it properly. I am wondering if my jSON is not formatted properly. Any help would be appreciated

I am trying to create a dynamic menu using jQuery UI.

I will be fetching entries from a JSON file and creating my menu items.I decided to do a small demo before i try this on a larger scale.Here's my fiddle which works the way i want it to work. Now I cant get this to work with a JSON file.

WORKING FIDDLE

Here is the JSON

var JSON = 
    {
       menu: 
          [
             {name: 'Croatia', link: '0', sub: null},
             {name: 'England', link: '1', sub: 
                [
                   {name: 'Arsenal',link: '0-0', sub: null},
                   {name: 'Liverpool',link: '0-1', sub: null},
                   {name: 'Manchester United',link: '0-2', sub: null}
                ]
             },
             {name: 'Spain', link: '2', sub: 
                [
                   {name: 'Barcelona',link: '2-0', sub: null},
                   {name: 'Real Madrid',link: '2-1', sub: null}
                ]
              },        
              {name: 'Germany', link: '3',sub: 
                [
                   {name: 'Bayern Munich',link: '3-1', sub: null},
                   {name: 'Borrusia Dortmund',link: '3-2', sub: null}
                ]
              }
          ]
    }

How can i design my entire menu using the values from the JSON where the Li's will be something like the following.

<li><a href="#3-2">Borrusia Dortmund</a>
</li>

EDIT: The question may sound like i have not tried anything, but i have. its just the JSON part which i cannot understand, Jsfiddle does not read it properly. I am wondering if my jSON is not formatted properly. Any help would be appreciated

Share Improve this question edited Jan 15, 2015 at 18:10 MarsOne asked Sep 25, 2013 at 11:08 MarsOneMarsOne 2,1865 gold badges30 silver badges54 bronze badges 5
  • I haven't used dynamics -- are you using mustache? – streetlight Commented Sep 25, 2013 at 11:12
  • for json validation try jsonlint.com – Claudio Santos Commented Sep 25, 2013 at 11:15
  • 1 That's a JS object, not JSON, FWIW. – Andy Commented Sep 25, 2013 at 11:16
  • 1 You don't have JSON, you have an object, as already mentioned. Please post the code which does not work as expected and explain what does not work. – Felix Kling Commented Sep 25, 2013 at 11:30
  • 2 jsfiddle.net/9uhc3/5 – sunn0 Commented Sep 25, 2013 at 11:34
Add a comment  | 

3 Answers 3

Reset to default 8

Just iterate your JSON.menu array and generate the menu from it (renamed JSON -> data ...):

$(function () {
    var getMenuItem = function (itemData) {
        var item = $("<li>")
            .append(
        $("<a>", {
            href: '#' + itemData.link,
            html: itemData.name
        }));
        if (itemData.sub) {
            var subList = $("<ul>");
            $.each(itemData.sub, function () {
                subList.append(getMenuItem(this));
            });
            item.append(subList);
        }
        return item;
    };

    var $menu = $("#menu");
    $.each(data.menu, function () {
        $menu.append(
            getMenuItem(this)
        );
    });
    $menu.menu();
});

http://jsfiddle.net/9uhc3/5/

Like this (recursive function) :

function parseMenu(ul, menu) {
    for (var i=0;i<menu.length;i++) {
        var li=$(ul).append('<li><a href="'+menu[i].link+'">'+menu[i].name+'</a></li>');
        if (menu[i].sub!=null) {
            var subul=$('<ul id="submenu'+menu[i].link+'"></ul>');
            $(li).append(subul);
            parseMenu($(subul), menu[i].sub);
        }
    }
}

var menu=$('#menu');
parseMenu(menu, JSON.menu);

http://jsfiddle.net/uDTk4/ - including the JSON (object) from above.

Produced output / menu :

<ul id="menu">
    <li><a href="0">Croatia</a></li>
    <li><a href="1">England</a></li>
    <ul id="submenu1">
        <li><a href="0-0">Arsenal</a></li>
        <li><a href="0-1">Liverpool</a></li>
        <li><a href="0-2">Manchester United</a></li>
    </ul>
    <li><a href="2">Spain</a></li>
    <ul id="submenu2">
        <li><a href="2-0">Barcelona</a></li>
        <li><a href="2-1">Real Madrid</a></li>
    </ul>
    <li><a href="3">Germany</a></li>
    <ul id="submenu3">
        <li><a href="3-1">Bayern Munich</a></li>
        <li><a href="3-2">Borrusia Dortmund</a></li>
    </ul>
</ul>

Your json is invalid. Correct json is below.

{"menu":[
    {"name": "Croatia", "link": "0", "sub": null
    },
    {"name": "England", "link": "1", "sub": [
        {"name": "Arsenal","link": "0-0", "sub": null},
        {"name": "Liverpool","link": "0-1", "sub": null},
        {"name": "Manchester United","link": "0-2", "sub": null}
        ]},
    {"name": "Spain", "link": "2", "sub": [
        {"name": "Barcelona","link": "2-0", "sub": null},
        {"name": "Real Madrid","link": "2-1", "sub": null}
    ]},        
    {"name": "Germany", "link": "3","sub": [
        {"name": "Bayern Munich","link": "3-1", "sub": null},
        {"name": "Borrusia Dortmund","link": "3-2", "sub": null}
        ]}
]}
发布评论

评论列表(0)

  1. 暂无评论