最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ExtJS: How to dynamically add child nodes to treepanel - Stack Overflow

programmeradmin3浏览0评论

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 acplish this.

Any ideas?

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 acplish this.

Any ideas?

Share Improve this question asked Jul 7, 2010 at 11:56 DavidRDavidR 2331 gold badge3 silver badges5 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Grab the parent node by getNodeById or getRootNode and add the child node with appendChild.

See the example at:

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

Is your layers property of the treePanel defined?

treePanel.layers.add(layer);

here is the sample code for dynamically append child to tree panel

Ext.getCmp('treeid').getRootNode().appendChild(response from database);
发布评论

评论列表(0)

  1. 暂无评论