为jqtree创建一个json树结构

时间:2015-06-24 16:34:18

标签: javascript jquery json jqtree

我正在尝试为构建jqtree的以下数据创建树json

var data = [
    {
        "director": "Name1",
        "name": "sub-sub-child1",
        "teamname": "sub-child1",
        "teamManager": "child1"
    },

    {
       "director": "Name1",
       "name": "sub-sub-child2",
       "teamname": "sub-child1",
       "teamManager": "child1"
    },
    {
        "director": "Name1",
        "name": "sub-sub-child2",
        "teamname": "sub-child2",
        "teamManager": "child2"
    },
    {
        "director": "Name2",
        "name": "sub-sub-child1",
        "teamname": "sub-child1",
        "teamManager": "child1"
    },
    {
        "director": "Name2",
        "name": "sub-sub-child2",
        "teamname": "sub-child2",
        "teamManager": "child2"
    },
    {
        "director": "Name3",
        "name": "sub-sub-child1",
        "teamname": "sub-child1",
        "teamManager": "child1"
    },
    {
        "director": "Name3",
        "name": "sub-sub-child2",
        "teamname": "sub-child2",
        "teamManager": "child2"
    }
];

console.log(data);

var aNode = [];
                var aTempNode  = [];
                $.each(data, function(index, value) {
                    //console.log(value);
                    var key = {};
                    if($.inArray(value.director, aTempNode) == -1) {
                        aTempNode.push(value.director);
                        key['label'] = value.director;
                        key['children'] = [{label: value.teamManager, children: [{label: value.teamname, children: [{label: value.name}]}]}];
                        aNode.push(key);
                    } else {
                        //console.log(aNode)
                        if(key['teamname'] == aNode.children) {

                        }
                    }

                });
                console.log(aNode);

树应该采用这种形式

Name1
 |
 |___child1
 |     |
 |     |___sub-child1
 |     |      |
 |     |      |___sub-sub-child1
 |     |      |___sub-sub-child2
 |     |
 |     |___sub-child2
 |            |
 |            |___sub-sub-child1
 |            |___sub-sub-child2
 |
 |____child2  

我已经完成了一半。我很困惑如何从这里开始。 fiddle

0 个答案:

没有答案