6

尝试获取所选行时出现以下错误:

Uncaught TypeError: Object #<HTMLDivElement> has no method 'getView'

当我无法获取视图并因此无法获取 SelectionModel 时,如何获取当前选定的行?

我的查看代码:

Ext.define('MyLodge.view.content.MemberGrid', {
extend: 'Ext.grid.Panel',
alias: 'widget.membergrid',



initComponent: function(){

    var rowEditing = Ext.create('Ext.grid.plugin.RowEditing');

    var store = Ext.create('MyLodge.store.Members');

    Ext.apply(this, {
        height: this.height,
        plugins: [rowEditing],
        store: store,
        stripeRows: true,
        columnLines: true,
        columns: [{
            id       :'id',
            text: 'ID',
            width: 40,
            sortable: true,
            dataIndex: 'id'
        },{
            text   : 'Name',
            flex: 1,
            sortable : true,
            dataIndex: 'name',
            field: {
                xtype: 'textfield'
            }
        },{
            text   : 'E-Mail',
            width    : 150,
            sortable : true,
            dataIndex: 'email',
            field: {
                xtype: 'textfield'
            }
        },{
            text   : 'Href',
            width    : 200,
            editable: false,
            sortable : true,
            dataIndex: 'href'
        }],
        dockedItems: [{
            xtype: 'toolbar',
            items: [{
                text: 'Add',
                iconCls: 'icon-add',
                handler: function(){
                    // empty record
                    store.insert(0, new MyLodge.model.Member());
                    rowEditing.startEdit(0, 0);
                }
            }, {
                text: 'Delete',
                iconCls: 'icon-delete',
                handler: function(){
                    var selection = grid.getView().getSelectionModel().getSelection()[0];
                    if (selection) {
                        store.remove(selection);
                    }
                }
            },'-',{
                text: 'Save',
                iconCls: 'icon-save',
                handler: function(){
                    store.sync({
                        success: function(response){
                            store.load()
                        }
                    });

                }
            },{
                text: 'Refresh',
                handler: function(){
                    store.load();
                }
            }]
        }]
    });

    this.callParent(arguments);
    }
});
4

2 回答 2

9

一种选择是将scope作为变量添加到闭包中:

initComponent: function () {
    var me = this;
    .
    .

因此,在您的处理程序中,您可以使用me来引用网格:

{
    text: 'Delete',
    iconCls: 'icon - delete ',
    handler: function () {
        var selection = me.getView().getSelectionModel().getSelection()[0];
        if (selection) {
            store.remove(selection);
        }
    }
}

工作示例:http: //jsfiddle.net/Sn4fC/


第二个选项可以设置 aclick listener而不是 thehandler并指定scope:

{
    text: 'Delete',
    iconCls: 'icon - delete ',
    listeners: {
        click: {
            scope: this,
            fn: function () {
                var selection = this.getView().getSelectionModel().getSelection()[0];
                if (selection) {
                    store.remove(selection);
                }
            }
        }
    }
}

工作示例:http: //jsfiddle.net/XyBbF/

于 2013-03-16T21:13:27.857 回答
1

问题是“网格”在处理函数范围内不可用,或者它不是您实际期望的那个。

handler: function(){
      //.... keep the debug point here in browser developer tool and verify the value of grid in console. It is definitely not an instance of the grid u expected hence it wont have getView() method.
      var selection = grid.getView().getSelectionModel().getSelection()[0];

}

尝试获取网格的引用并在处理程序中使用它,如下所示:

Ext.define('MyLodge.view.content.MemberGrid', {
  extend: 'Ext.grid.Panel',
  alias: 'widget.membergrid',
  id: 'membergrid',
  ......

handler: function(){
   var grid = Ext.getCmp('membergrid'); // where 'membergrid' is the id defined in grid config
   var selection = grid.getView()......
于 2013-03-16T20:03:45.207 回答