如何从数组创建jqtree

时间:2015-02-06 13:15:24

标签: javascript jqtree

var count = 0;
var subcount = 0;
var subdata = [];
var treeData = [];
for (var catId in cats) {
    for (var subCatId in cats[catId].m_itemGrpList) {
        subdata[subcount] = [{
                            id: subCatId,
                            label: GetDisplay(cats[catId].m_itemGrpList[subCatId])
        }];
        subcount = subcount + 1;
    }
    treeData[count] = [{
        id: catId,
        label: GetDisplay(cats[catId]),
        children: subdata
    }];
    count  = count + 1;
}

$tree.tree({
    data: treeData,
    onCreateLi: function(node, $li) {
        if (node.color) {
            var $title = $li.find('.jqtree-title');
            $title.addClass(node.color);
        }
    }
});

当我尝试这样的时候,数组对象被创建并存储在数组中,但是jqtree接受的是对象而不是数组中的对象。我怎样才能做到这一点。

这棵树有父节点,一个父节点可以有多个子节点。

1 个答案:

答案 0 :(得分:0)

我得到了答案。仅适用于其他初学者。我使用了错误的数据结构。我应该这样做:

var treeData = [];
    for (var catId in cats) {
        var subdata = [];
        var subCatObj;
        for (var subCatId in cats[catId].m_itemGrpList) {
            if(cats[catId].m_itemGrpList[subCatId].m_totalCount <=0)
                continue;
            subCatObj = {
                            id: subCatId,
                            label: GetDisplay(cats[catId].m_itemGrpList[subCatId])
                        }
            subdata.push(subCatObj);
        }
        var catObj = {
                        id: catId,
                        label: GetDisplay(cats[catId]),
                        children: subdataenter code here
                    }
        treeData.push(catObj);
    }