使用JSON将多级项添加到无序列表

时间:2013-05-01 19:13:50

标签: javascript jquery drop-down-menu

我需要使用下面的json对象构建嵌套的unorder列表,每个json对象都有id和父id,root或top元素parent id为0

 JSON Object 

{
   "menu": [
         {"id":1,"name":"Dashboard","parent":0},
         {"id":2,"name":"Dashboard1","parent":0 },
         {"id":3,"name":"Dashboard2","parent":0},
         {"id":4,"name":"Dashboard3","parent":0},
         {"id":5,"name":"Dashboard4","parent":1},
         {"id":6,"name":"Dashboard5","parent":1},
         {"id":7,"name":"Dashboard6","parent":1},
         {"id":8,"name":"Dashboard7","parent":2},
         {"id":9,"name":"Dashboard8","parent":5}
          ]

}

预期结果:

<ul class="menu" >
    <li >Dashboard</li>
    <li >Dashboard1
        <ul>
            <li> Dashboard4</li>
            <li> Dashboard5
                <ul>
                    <li> Dashboard8</li>                                                
                </ul>
            </li>
            <li> Dashboard6</li>                            
        </ul>
    </li>
    <li >Dashboard2
        <ul>
            <li> Dashboard7</li>                                                
        </ul>
    </li>
    <li >Dashboard3</li>                
</ul>

我试过

 menuArr=[];
  $.each(menu,function(i,val){
     var parent=$(".menu");
     if(val.parent!=0){
        parent= menuArr[val.parent];
     }
     menuLi= $('<li>'+val.name+'</li>');
     parent.append(menuli);
     menuArr[val.id]=menuli;

  }); 

3 个答案:

答案 0 :(得分:3)

这个怎么样?

http://jsfiddle.net/LCFH5/2/

<强> HTML

<ul id="menu"></ul>

<强> JS

$(function() {

    var data = {
        "menu": [
            {"id":5,"name":"Dashboard4","parent":1},
            {"id":1,"name":"Dashboard","parent":0},
            {"id":2,"name":"Dashboard1","parent":0 },
            {"id":3,"name":"Dashboard2","parent":0},
            {"id":4,"name":"Dashboard3","parent":0},
            {"id":6,"name":"Dashboard5","parent":1},
            {"id":7,"name":"Dashboard6","parent":1},
            {"id":8,"name":"Dashboard7","parent":2},
            {"id":9,"name":"Dashboard8","parent":5}
        ]
    };

    var ul = $('#menu');
    var items = data.menu;
    var add = function(item) {

        var parent;

        if(item.parent > 0) {

            parent = ul.find('#n' + item.parent + ' > ul');

            if(parent.length < 1) {

                $.each(items, function(i) {

                    if(this.id === item.parent) {

                        parent = add(items.splice(i, 1)[0]);

                        return false;

                    }

                });

            }

        } else {

            parent = ul;

        }

        return parent.append('<li id="n' + item.id + '">' + item.name + '<ul></ul></li>');

    };

    while(items.length > 0) {

        add(items.shift());

    }

});

它更清晰,并且会处理未分类的数据。

答案 1 :(得分:1)

试试这个: - http://jsfiddle.net/ngRns/

  var root;
for (var key in json) {
    root = key;
    break
}
var ul = $('<ul>',{'class':key,'data-level':0});

var prev = undefined;
$.each(json[root], function (_, obj) {
    if (prev && obj.parent > prev.parent) {
        ('[data-level=' + prev.parent + ']', ul).find('li:last').append($('<ul data-level=' + obj.parent + '>'));
    }
    ul.find('[data-level=' + obj.parent + ']').andSelf().last().append($('<li>', {
        text: obj.name
    }));
    prev = obj;
});

更新 http://jsfiddle.net/XAcqf/

var ul = $('',{     '阶级':关键 });

$.each(json[root], function (_, obj) {
    var li = ul.find('li#' + obj.parent);
    if (li.length != 0) {
        li.last().append($('<ul>').append($('<li>', {
            text: obj.name,
            id: obj.id
        })));;
    } else {
        ul.append($('<li>', {
            text: obj.name,
            id: obj.id
        }));
    }

});

答案 2 :(得分:1)

启动此标记:

<ul class="menu"></ul>

和这个(我把JSON放在一个对象中,因为我不知道你的来源:

var myj = {
    "menu": [{
        "id": 1,
            "name": "Dashboard",
            "parent": 0
    }, {
        "id": 2,
            "name": "Dashboard1",
            "parent": 0
    }, {
        "id": 3,
            "name": "Dashboard2",
            "parent": 0
    }, {
        "id": 4,
            "name": "Dashboard3",
            "parent": 0
    }, {
        "id": 5,
            "name": "Dashboard4",
            "parent": 1
    }, {
        "id": 6,
            "name": "Dashboard5",
            "parent": 1
    }, {
        "id": 7,
            "name": "Dashboard6",
            "parent": 1
    }, {
        "id": 8,
            "name": "Dashboard7",
            "parent": 2
    }, {
        "id": 9,
            "name": "Dashboard8",
            "parent": 5
    }]
};

我使用了这段代码:

menuArr = [];
$.each(myj.menu, function (i, val) {
    var menuLi
    var parent = $(".menu");
    if (val.parent != 0) {
        parent = menuArr[val.parent];
    }
    menuLi = $('<li>' + val.name + '</li>');
    parent.append(menuLi);
    menuArr[val.id] = menuLi;
});

发现的问题:

  • 没有数据对象(JSON)......没什么大不了的。
  • 你的menuLi在某些地方是menuli

示例:http://jsfiddle.net/xnfQk/