【问题标题】:ExtJS Grid Delete Row - Getting Selected RowExtJS 网格删除行 - 获取选定的行
【发布时间】:2013-03-17 05:13:27
【问题描述】:

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

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);
    }
});

【问题讨论】:

    标签: extjs


    【解决方案1】:

    一种选择是将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/

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 您的答案可能有效,但通过jsfiddle 提供示例要好得多。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-20
      • 2015-07-29
      • 2022-01-09
      • 2013-01-21
      • 1970-01-01
      相关资源
      最近更新 更多