开发者

Extjs: Loading Dynamic Data for Tree

I am new to Extjs, I want to load data dynamically into the tree. The server returns me data, which is not in json format i.e the structure of data is pretty much messed up. What URL should I specify in my code and how do i deal with the format of data. There is no way to differentiate between parent and child when looking at the data returned by the server. I am trying to build a tree based on following code...

Ext.onReady(function() {

    var Tree = Ext.tree;

    var tree = new Tree.TreePanel({
        useArrows: true,
        autoScroll: true,
        animate: true,
        enableDD: true,
        containerScroll: true,
        border: false,
        // auto crea开发者_StackOverflow中文版te TreeLoader
        dataUrl:'????????????????????',
        root: {
            nodeType: 'async',
            text: 'ALL',
            draggable: false,
            id: ''
        }
    });

    // render the tree
    tree.render('tree-div');
    tree.getRootNode().expand();
}); 


When passing the dataUrl config option you are telling the Tree Component to use the default TreeLoader which only accepts a JS array (JSON) as output from the server. If you are passing in other data from the server, be it XML, YAML, or other mumbojumbo like you said, you will have to write your own TreeLoader.

Check the documentation on the Ext.tree.Treeloader or the source of TreeLoader for a start, that might get you a long way in understanding what actually needs to be done to write your own Loader that accepts your server output.


By adding json object directly to tree.....

Java code:

        for (Object object : objJSONArray) {
            i++;

            JSONObject jsobject = (JSONObject) object;
            if (i == 1) {

                treedata = jsobject;
                System.out.println("  rootnode is:::" + treedata);
            } else {
                String nodeparent = jsobject.getString("NODE_PARENT");
                String parentId = treedata.get("ID").toString();

                if (nodeparent.equals(parentId)) {
                    treedata.put("children", jsobject);
                    System.out.println("treedata  " + treedata);
                } else {
                    Set jsonKeys = treedata.keySet();
                    Iterator itr = jsonKeys.iterator();
                    while (itr.hasNext()) {
                        String keys = itr.next().toString();
                        Object keyObject = treedata.get(keys);
                        if (keyObject instanceof JSONObject) {
                            JSONObject jso = treedata.getJSONObject(keys);
                            String childParentID = jso.getString("ID");
                            if (childParentID.equals(nodeparent)) {
                                jso.put("children", jsobject);
                                treedata.put(keys, jso);
                            }
                        }
                    }
                }
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜