简体   繁体   中英

ExtJS: How to dynamically add child nodes to treepanel

How can you add a child node to an existing TreePanel programmatically using JavaScript?

I have a TreePanel that displays the active layers of a map (using GeoExt):

treeConfig = new OpenLayers.Format.JSON().write([{
    nodeType: "gx_baselayercontainer",
    text: "Base layers",
    expanded: true
    }, {
    nodeType: "gx_overlaylayercontainer",
    text: "Overlays",
    expanded: true,
    loader: {
        baseAttrs: {
            radioGroup: "foo",
            uiProvider: "use_radio"
        }
    }
}], true);

treePanel = new Ext.tree.TreePanel({
    id: 'mainpanel',
    border: true,
    region: "west",
    title: "Map layers",
    width: 200,
    split: true,
    collapsible: true,
    margins: '0 0 5 5',
    collapseMode: "mini",
    autoScroll: true,
    loader: new Ext.tree.TreeLoader({
        applyLoader: false,
        uiProviders: {
            "use_radio": LayerNodeUI
        }
    }),
    root: {
        nodeType: "async",
        children: Ext.decode(treeConfig)
    },
    listeners: {
        "radiochange": function(node){
            alert(node.layer.name + " is now the the active layer.");
        }
    },
    rootVisible: false,
    lines: false
});

The user should be able to add an overlay layer by pressing a button, however I cannot seem to find any examples on how to accomplish this.

Any ideas?

通过getNodeByIdgetRootNode获取父节点,并使用appendChild添加子节点。

这是动态附加子树到树面板的示例代码

Ext.getCmp('treeid').getRootNode().appendChild(response from database);

See the example at:

http://dev.geoext.org/geoext/trunk/geoext/examples/layercontainer.html

Is your layers property of the treePanel defined?

treePanel.layers.add(layer);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM