0

我正在使用 Ext.component.Query 获取一个对象。我需要检查对象是否存在。如果对象存在,我需要删除该对象。谁能告诉我该怎么做?

谢谢

4

5 回答 5

2

简单检查Ext.ComponentQuery

var check = Ext.ComponentQuery.query('yourXtype');
if (check.length > 0)
    //do something
else
    //do other something
于 2013-09-09T07:24:01.340 回答
2

正如其他海报所提到的,您正在寻找的方法是Ext.ComponentQuery,它返回一个数组,然后您可以检查 via 的长度length,这反过来会告诉您该对象是否存在。如果对象存在,则可以destroy()通过Ext.AbstractComponent

我制作了一个 jsFiddle 示例,演示了您在此处尝试执行的操作:http: //jsfiddle.net/mPYPw/

小提琴中的代码:

Ext.create('Ext.panel.Panel', {
    name : 'myPanel',
    title: 'Panel 1',
    width: 200,
    html: '<b>Its a panel!</b>',
    renderTo: Ext.getBody()
});

Ext.create('Ext.panel.Panel', {
    name : 'myPanel',
    title: 'Panel 2',
    width: 200,
    html: 'Look, another panel!',
    renderTo: Ext.getBody(),
    dockedItems: [{
    xtype: 'toolbar',
    dock : 'bottom',
    items: [{
        text: 'Destroy all panels!',
        handler: function(){
            // Here we can query for the panels
            var panels = Ext.ComponentQuery.query('panel[name=myPanel]'),
                 trees = Ext.ComponentQuery.query('treepanel');

            // @param {Ext.panel.Panel[]} panels Array of panel components
            if(panels.length > 0){
                alert("About to destroy " + panels.length + " Panels!");
                Ext.each(panels, function(panel){
                    panel.destroy();
                });
            }

            // There are no tree panels
            if(!trees.length){
                alert("There are no tree panels to destroy!");
            }
        }
    }]
    }]
});
于 2013-09-10T15:24:35.930 回答
1

我知道其中一个版本的文档搞砸了......我不知道它在 Ext JS 4.2 中是否仍然相同,但在 4.1.1 中,您可以使用类似于此的东西通过 xtype 查询 Ext JS 对象:

Ext.ComponentQuery.query('xtype');

IE

Ext.ComponentQuery.query('gridpanel');
于 2013-09-04T14:00:28.847 回答
0

我认为Ext.ComponentQuery-method-query解释了它。

于 2013-09-04T16:21:56.527 回答
0

首先要注意 Ext.getCmp("id") 适用于小型应用程序。如果您倾向于拥有大型应用程序,您可以选择组件查询。这可以通过两种方式完成,您可以使用“Xtype”或“组件 ID”(注意组件 ID 必须以 # 为前缀)。

于 2013-09-05T07:58:40.667 回答