简体   繁体   English

ExtJS button2 onClick按组件执行click button1

[英]ExtJS button2 onClick execute click button1 by component

How to execute clicked button1 when I click button 2 in formpanel extjs. 当我点击formpanel extjs中的按钮2时,如何执行单击的按钮1。 I try : 我尝试:

btnFire.fireEvent('click', btnFire);

but nothing happen. 但什么都没发生。 Here my code button : 这是我的代码按钮:

xtype: 'button',
text: 'Hitung',
itemId: 'sumBtn',
id: 'sumBtn',
name: 'sumBtn',
iconCls: '',
listeners : {
    'render' : function() {
        Ext.get('sumBtn').on('click', function(e) {
    // Here is button1 component        
    var btnFire = Ext.getCmp("ttransinboundawb_module_real_general_form_18_btn_sumBillBtn");
        // do execute button1 when button2 clicked    
        btnFire.fireEvent('click', btnFire);
        });
    }
}

Thanks for help 感谢帮助

You write a separate click event for both the buttons and then to achieve this first need to query the 1st button component. 您为这两个按钮编写了单独的单击事件,然后首先需要查询第一个按钮组件。

for that var cc = Ext.ComponentQuery.query('button'); for var cc = Ext.ComponentQuery.query('button'); Once you get the component of the first button in second button handler then you need to fire handler of the first button. 在第二个按钮处理程序中获得第一个按钮的组件后,您需要触发第一个按钮的处理程序。

Handler of second button code should be like this. 第二个按钮代码的处理程序应该是这样的。

{
        xtype: 'button',
        text: 'Button 2',
        id : 'bt2',
        handler: function() {
            var cc = Ext.ComponentQuery.query('button');
            for(var i=0; i<cc.length; i++){
                if(cc[i].id == 'bt1' ){
                    cc[i].handler();
                }
            }
            alert('You clicked the button2');
        }
    }

Or We can use 或者我们可以使用

var cc = Ext.getCmp('bt1');
    cc.handler();

I created a fiddle for you. 我为你创造了一个小提琴 Please check. 请检查。 Let me know if any concern. 如果有任何疑虑,请告诉我。

Here is another approach: 这是另一种方法:

It avoids the getCmp call and does not use hard coded IDs which couples your logic to specific IDs which may be error prone if you extend your application. 它避免了getCmp调用,并且不使用将您的逻辑耦合到特定ID的硬编码ID,如果您扩展应用程序,这些ID可能容易出错。 By using hard coded IDs you may run in conflicts with other parts of your application if you assign the same ID twice. 通过使用硬编码ID,如果您分配两次相同的ID,则可能会与应用程序的其他部分发生冲突。

Additional this approach uses the concept of ViewControllers and references which is the proposed way by Sencha nowadays to set up your view logic especially for large applications (see ViewControllers ). 此外,此方法使用ViewControllersreferences的概念,这是Sencha现在建议的方式来设置视图逻辑,尤其是对于大型应用程序(请参阅ViewControllers )。

Ext.define('MyApp.view.foo.FilterController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.FilterController',

    bt1Event: function () {
       alert('bt1 clicked');
    },

    bt2Event: function () {
        alert('bt2 clicked');
        // get a reference to the first button
        var bt1 = this.lookupReference('bt1');
        bt1.fireEvent('click', bt1);
    }
});

Ext.define('MyApp.view.foo.Foo', {
    extend: 'Ext.panel.Panel',
    bodyPadding: 5,  // Don't want content to crunch against the borders
    width: 300,
    title: 'Filters',
    controller : 'FilterController',
    defaults : {
        xtype : 'button'
    },
    items: [{
        text: 'Button 1',
        reference: 'bt1',
        listeners : {
            click: 'bt1Event' // handled by view controller
        }
    }, {
        text: 'Button 2',
        reference: 'bt2',
        listeners : {
            click: 'bt2Event' // handled by view controller
        }
    }]
});

Ext.create('MyApp.view.foo.Foo', {
    renderTo: Ext.getBody()
});

I created a fiddle to demonstrate this approach Sencha Fiddle 我创造了一个小提琴来展示这种方法Sencha Fiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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