如何使用JavaScript以编程方式将子节点添加到现有TreePanel?
我有一个TreePanel,显示地图的活动图层(使用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
});
用户应该能够通过按下按钮添加覆盖图层,但我似乎无法找到有关如何实现此目的的任何示例。
有什么想法吗?
答案 0 :(得分:3)
按getNodeById或getRootNode抓取父节点,并使用appendChild添加子节点。
答案 1 :(得分:0)
请参阅以下示例:
http://dev.geoext.org/geoext/trunk/geoext/examples/layercontainer.html
是否定义了treePanel的layers属性?
treePanel.layers.add(layer);
答案 2 :(得分:0)
这是动态附加子树到树面板的示例代码
Ext.getCmp('treeid').getRootNode().appendChild(response from database);