2011-12-06 31 views
1

我創建了一個View擴展Ext.grid.Panel,並附加一個tbar,在工具欄中我在這個問題中有2個按鈕[Add]和[Remove] I我只關注[Remove]命令。Ext.grid.Panel從tbar按鈕中獲取選定的記錄

像往常一樣,我想保留在我想要刪除的網格中當前選定的記錄。

所以在控制器:

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

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

回答

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

通過「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]; 

正如我說,類似於其他答案,但只是訪問網格的另一種方式。

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.'); 
    } 
} 
相關問題