ExtJS网格删除行 - 获取所选行

时间:2013-03-16 16:46:45

标签: extjs

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

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

当我无法获取View并因此得到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);
    }
});

2 个答案:

答案 0 :(得分: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/


第二个选项可以设置click listener而不是handler并指定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/

答案 1 :(得分:1)

问题是'grid'在处理函数范围内不可用,或者它不是你实际预期的那个。

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()......