Dijit Tree未按预期呈现

时间:2014-02-25 18:11:24

标签: javascript json dojo dijit.tree

我正在尝试将json数据呈现为dojo树。

您可以在http://jsfiddle.net/F53Ge/38/

看到我在做什么
                                   require(["dojo/parser","dojo/json","dojo/store/Memory","dijit/tree/ObjectStoreModel","dijit/Tree", "dojo/window"], function              (parser,json,Memory,ObjectStoreModel,Tree,win) {



var data = [{"id":"root","team":[{"teamId":1,"teamname":"JMK_TEST_1","parentteamId":0,"associatedList":[{"type":"9117","number":"1011D1P"}]},{"teamId":174,"teamName":"JJG_PARENT_3","parentteamId":0,"associatedList":[{"type":"8205","number":"062072T"}]},{"teamId":172,"teamName":"JJG_PARENT_1","parentteamId":0,"subteamsList":[{"teamId":175,"teamName":"JJG_Subteam_1","parentteamId":172,"associatedList":[{"type":"8720","number":"12345"}]},{"teamId":176,"teamName":"JJG_Subteam_2","parentteamId":172,"associatedList":[{"type":"8720","number":"12345"}]}],"associatedList":[{"type":"7945","number":"KQZGTNC"}]},{"teamId":221,"teamName":"JJG_Parent_4","parentteamId":0,"subteamsList":[{"teamId":222,"teamName":"JJG_Subteam_4_1","parentteamId":221,"associatedList":[{"type":"9117","number":"10E7683"},{"type":"9119","number":"514DDB2"},{"type":"8233","number":"102FE9P"},{"type":"7978","number":"KDGYKLL"},{"type":"7978","number":"99A9880"}]}]},{"teamId":106,"teamName":"JMK_TEST","parentteamId":0,"subteamsList":[{"teamId":107,"teamName":"JMK_TEST1","parentteamId":106,"subteamsList":[{"teamId":173,"teamName":"JJG_PARENT_2","parentteamId":107,"subteamsList":[{"teamId":178,"teamName":"JJG_Subteam_2_1","parentteamId":173,"associatedList":[{"type":"9117","number":"10E7683"}]}],"associatedList":[{"type":"7945","number":"KQZGTNC"}]}]}]}]}];


var store = new Memory({
    data: data,
    getChildren: function(object){
        return object.team || [];
    }
});

  var model = new ObjectStoreModel({
store: store,
   query: { id:'root' },
    mayHaveChildren: function (item) {
        return "subteamsList" in item;
    }
});

var tree = new Tree({
    model: model,
    showRoot: false,
    autoExpand: true,
    persist: false,
    onClick: function (item, treeNode, e) {
        selectednodeid = this.selectedNode;
        win.scrollIntoView(selectednodeid);
        alert(selectednodeid);
    }
},"oopt_list");
tree.startup();


 });

首先,我看不到子节点,也不知道如何传递标签,因为它显示未定义的列表。

感谢任何帮助。

另请告诉我是否应该使用ForestModel。 基本上我试图在一个树中显示json数据,并想知道用户点击了哪个节点,所以我可以根据它做一些动作。

此致 熊蜂

1 个答案:

答案 0 :(得分:0)

要定义哪个属性应该用作标签,您需要定义' labelAttr'在模特身上。

为什么你看不到子节点的原因是,你存储的商店缺少idProperty。 (或者你在根项目上有不同的id属性' id'并且在其他项目上有不同的' teamId')。

使用ObjectStoreModel而不是ForestModel是正确的。您正在使用内存存储,它实现了新的dojo / store API。 (ForestModel应仅用于较旧的dojo / data API)

请参阅此处更新的示例 http://jsfiddle.net/F53Ge/42/

require(["dojo/parser","dojo/json","dojo/store/Memory","dijit/tree/ObjectStoreModel","dijit/    Tree", "dojo/window"], function (parser,json,Memory,ObjectStoreModel,Tree,win) {



var data = [{"teamId":"root","subteamsList":[{"teamId":1,"teamName":"JMK_TEST_1","parentteamId":0,"associatedList":[{"type":"9117","number":"1011D1P"}]},{"teamId":174,"teamName":"JJG_PARENT_3","parentteamId":0,"associatedList":[{"type":"8205","number":"062072T"}]},{"teamId":172,"teamName":"JJG_PARENT_1","parentteamId":0,"subteamsList":[{"teamId":175,"teamName":"JJG_Subteam_1","parentteamId":172,"associatedList":[{"type":"8720","number":"12345"}]},{"teamId":176,"teamName":"JJG_Subteam_2","parentteamId":172,"associatedList":[{"type":"8720","number":"12345"}]}],"associatedList":[{"type":"7945","number":"KQZGTNC"}]},{"teamId":221,"teamName":"JJG_Parent_4","parentteamId":0,"subteamsList":[{"teamId":222,"teamName":"JJG_Subteam_4_1","parentteamId":221,"associatedList":[{"type":"9117","number":"10E7683"},{"type":"9119","number":"514DDB2"},{"type":"8233","number":"102FE9P"},{"type":"7978","number":"KDGYKLL"},{"type":"7978","number":"99A9880"}]}]},{"teamId":106,"teamName":"JMK_TEST","parentteamId":0,"subteamsList":[{"teamId":107,"teamName":"JMK_TEST1","parentteamId":106,"subteamsList":[{"teamId":173,"teamName":"JJG_PARENT_2","parentteamId":107,"subteamsList":[{"teamId":178,"teamName":"JJG_Subteam_2_1","parentteamId":173,"associatedList":[{"type":"9117","number":"10E7683"}]}],"associatedList":    [{"type":"7945","number":"KQZGTNC"}]}]}]}]}];


    var store = new Memory({
        data: data,
        idProperty:"teamId",
        getChildren: function(object){
            return object.subteamsList || [];
        }
    });

   var model = new ObjectStoreModel({
    store: store,
       query: { teamId:'root' },
        mayHaveChildren: function (item) {
            console.log("may",item)
            return "subteamsList" in item;
        },labelAttr :"teamName"
    });
    var tree = new Tree({
        model: model,
        showRoot: false,
        autoExpand: true,
        persist: false,
        onClick: function (item, treeNode, e) {
            selectednodeid = this.selectedNode;
            win.scrollIntoView(selectednodeid);
            alert(selectednodeid);
        }
    },"oopt_list");
    tree.startup();


});