Ext.grid.Panel从tbar按钮获取所选记录

时间:2011-12-06 13:54:30

标签: extjs4

我创建了一个View extend Ext.grid.Panel并且还附加了一个tbar,在工具栏中我得到了2个按钮[Add]和[Remove]这个问题我只专注于[Remove]命令。 / p>

像往常一样,我想抓住我想要删除的网格中的当前所选记录。

所以在控制器中:

init: function() {
 this.control({
   'extendgridlist button[action=remove]': {
      click: this.removeCurrentRow;
    }
 });
}

removeCurrentRow: function(t){
 // how do i get current selected record
}

4 个答案:

答案 0 :(得分:5)

removeCurrentRow: function(t){
 var grid = t.up('gridpanel');
 var arraySelected =grid.getSelectionModel().getSelection();
 //assuming you have a single select, you have the record at index 0;
 var record = arraySelected[0]
}

答案 1 :(得分:1)

'nscrob'的答案应该可以正常工作,我只是想指出一种替代方法。每个Ext组件都可以有一个'id'字段。因此,如果您在创建网格时为网格提供了以下配置选项:

id:'my_grid_id'

然后,您可以从removeCurrentRow函数内的任何位置执行以下操作:

var grid = Ext.getCmp('my_grid_id');
var rows = grid.getSelectionModel().getSelection();
if(!rows.length)
{   //in case this fires with no selection
    alert("No Rows Selected!");
    return;
}
var row = rows[0];

正如我所说的,与其他答案类似,但只是另一种访问网格的方式。

答案 2 :(得分:1)

如果网格是selType = "cellModel",请使用以下代码:

var grid = Ext.getCmp('id-of-grid');
var recid = grid.getSelectionModel().getCurrentPosition();

if(recid && recid.row){
  var r = grid.getStore().getAt(recid.row)   
  alert(r.data.anyofgridfieldid)
}

更多详情:http://as400samplecode.blogspot.com/2012/01/extjs-grid-selected-row-cell.html

答案 3 :(得分:0)

...
xtype: 'button',
text: 'Edit',
handler: function() {
    onEdit(this.up('theGrid')); // alias: 'widget.theGrid' in definition
}

...
function onEdit(theGrid) {

if (theGrid.getSelectionModel().hasSelection()) {    
        var rows = theGrid.getSelectionModel().getSelection();
        var row = rows[0];
        console.log('Count Rows Selected : ' + rows.length);
        console.log('The Row : ' + row); // columns: 'id', 'name', 'age'
        console.log('Student Id: ' + row.get('id'));
        console.log('Student Name: ' + row.get('name'));
        console.log('Student Age: ' + row.get('age'));
    }
    else {
        console.log('No Row Selected.');
    }
}