简体   繁体   English

使用Ext.define()扩展网格面板时如何添加行双击事件侦听器?

[英]How to add row double click event listener when extending grid panel with Ext.define()?

I am extending GridPanel with Ext.define() (Ext v4). 我正在使用Ext.define()(Ext v4)扩展GridPanel。

I need to get the row data when a grid row is double clicked. 双击网格行时,我需要获取行数据。 At this point I cannot even get the event listener working: 此时我甚至无法让事件监听器工作:

Ext.define('Application.usersGrid', {
extend: 'Ext.grid.GridPanel',
alias: 'widget.usersgrid',


viewConfig: {
    listeners: {
        dblclick: function(dataview, index, item, e) {
            alert('dblclick');
        }
    }
},
...

What is wrong here? 这有什么不对?

If anyone needs the answer- this is the right way: 如果有人需要答案 - 这是正确的方法:

Ext.define('Application.usersGrid', {
extend: 'Ext.grid.Panel',
alias: 'widget.usersgrid',


viewConfig: {
    listeners: {
        itemdblclick: function(dataview, record, item, index, e) {
            alert('itemdblclick');
        }
    }
},
...

http://dev.sencha.com/new/ext-js/4-0/api/Ext.grid.GridView#event-itemdblclick http://dev.sencha.com/new/ext-js/4-0/api/Ext.grid.GridView#event-itemdblclick

You don't need to put the listener in the viewconfig. 您不需要将侦听器放在viewconfig中。 Here is my working configuration: 这是我的工作配置:

listeners : {
    itemdblclick: function(dv, record, item, index, e) {
        alert('working');
    }
},

Another thing is, you seems to have used Ext.grid.GridPanel in the extend property. 另一件事是,您似乎在extend属性中使用了Ext.grid.GridPanel But in documentation it's Ext.grid.Panel . 但在文档中它是Ext.grid.Panel But even with gridpanel, everything seems to work fine. 但即使使用gridpanel,一切似乎都运行良好。

I would suggest to use the Ext JS 4 terms as it might cause to application breakage later in other 4.x versions. 我建议使用Ext JS 4术语,因为它可能导致其他4.x版本中的应用程序破坏。

Now, if you are using the new MVC architecture, you will want to move these actions to the controller rather than the view. 现在,如果您正在使用新的MVC架构,则需要将这些操作移动到控制器而不是视图。 You can refer to the MVC Architecture guide for details. 有关详细信息,请参阅MVC体系结构指南。

With the MVC approach in ExtJS 4 there's another smart way too to define such handlers. 使用ExtJS 4中的MVC方法,还有另一种智能方法来定义这样的处理程序。 Some example code: 一些示例代码:

Ext.define('App.controller.Documents', {

  extend: 'Ext.app.Controller',
  stores: ['Documents'],
  models: ['Document'],
  views: [
    'document.List',
    'document.Edit',
    'document.Preview'
  ],  

  init: function() {

    this.control({

      /*  
       * a cool way to select stuff in ExtJS 4
       */
      'documentlist': {
        itemdblclick: this.editDocument
      },  

      /*  
       * simple access to components
       */
      'documentedit button[action=save]': {
        click: this.updateDocument
      },  

    }); 

  },  

  editDocument: function(grid, record) {
    var view = Ext.widget('documentedit');
    view.down('form').loadRecord(record);
  },  

  updateDocument: function(button) {
    var win = button.up('window'),  // new selection API
        form   = win.down('form'),  // in ExtJS 4
        record = form.getRecord(),
        values = form.getValues();

    record.set(values);
    win.close();
  }
});
listeners: {
        select: 'xxxx',

        itemdblclick: function (dv, record, item, index, e) {
            var myBtn = Ext.getCmp('btnEdit');
            myBtn.onClick();
        }
    },

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

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