代码之家  ›  专栏  ›  技术社区  ›  DeLe

Extjs4.1-在树面板中获取选定项失败

  •  1
  • DeLe  · 技术社区  · 12 年前

    我有一个树面板,当我选择如下时,我会尝试获取节点 http://jsfiddle.net/kTedM/

     Ext.create('Ext.tree.Panel', {
            title: 'Simple Tree',
            width: 200,
            height: 200,
            store: store,
            rootVisible: false,
            dockedItems: [{
                xtype: 'toolbar',
                items: {
                    text: 'Get Selected nodes',
                    handler: function(){
                        var s = this.up('panel').getSelectionModel().getSelection();
                        if (s[0])
                            alert(s[0].data.text + ' was selected');
                        else alert('no selected');
                    }
                }
            }],
            renderTo: Ext.getBody()
        });
    

    但如果你遵循下面的步骤,你会看到错误。

    步骤1:运行代码并单击 get selected nodes 你会得到正确的警报是 no selected

    步骤2:双击 homework 节点并单击 获取选定的节点 你会看到的

    enter image description here

    但我看到那个节点没有被选中?如何解决,谢谢

    1 回复  |  直到 12 年前
        1
  •  2
  •   rixo    12 年前

    实际上,“家庭作业”节点 应该是 挑选出来的。双击它没有理由取消选择它。

    错误在于,这个节点被选中的事实没有在视觉上得到正确的表示。显然是个bug。

    Sencha已经在Ext4.2中修复了它。看见 this update of your fiddle ; 我刚刚将版本更改为4.2.0,没有什么令人惊讶的。。。

    所以,为了回答你的问题,我想说,为了修复它,你只需要升级到最后一个版本。我建议不要使用引入了几个新错误的最后4.2.1,而是使用4.2.0.x。

    现在,这里有一些代码,因为SO强迫我发布一些代码,以便被允许链接到小提琴:

    // Same code as you
    Ext.onReady(function () {
        var store = Ext.create('Ext.data.TreeStore', {
            fields: [
                    {name: 'id',     type: 'string'},
                    {name: 'text',     type: 'string'},
                    {name: 'selected', type: 'string'}
            ],
            root: {
                expanded: true,
                id: '0',
                children: [{
                    text: "detention",
                    id: '1',
                    leaf: true
                }, {
                    text: "homework",
                    id: '2',
                    expanded: true,
                    children: [{
                        id: '3',
                        text: "book report",
                        leaf: true
                    }, {
                        id: '4',
                        text: "alegrbra",
                        leaf: true,
                        selected: 'true'
                    }]
                }, {
                    id: '5',
                    text: "buy lottery tickets",
                    leaf: true
                }]
            }
        });
    
        Ext.create('Ext.tree.Panel', {
            title: 'Simple Tree',
            width: 200,
            height: 200,
            store: store,
            rootVisible: false,
            dockedItems: [{
                xtype: 'toolbar',
                items: {
                    text: 'Get Selected nodes',
                    handler: function(){
                        var s = this.up('panel').getSelectionModel().getSelection();
                        if (s[0])
                            alert(s[0].data.text + ' was selected');
                        else alert('no selected');
                    }
                }
            }],
            renderTo: Ext.getBody()
        });
    });