2016-02-09 121 views
1

我已經在angular2中設置了ag-grid,它工作正常,但我無法獲得所選行的值......在我的控制檯窗口中沒有錯誤...這就是我如何初始化網格......如何獲取angular2中ag網格中選定行的數據?

import {Component} from 'angular2/core'; 


@Component({ 
selector: 'aggride', 
template: ` 

<div class="tr-card" > 
<ag-grid-ng2 #agGrid of mgrid class="ag-fresh" rowHeight="40px"  
       [columnDefs]="columnDefs" 
       [rowData] = "rowData" 
    enableCellExpressions="true" 
enableSorting="true" 
    unSortIcon="true" 
rowSelection="single" 
(getSelectedRows) = "getSelectedRows()" 
(onSelectionChanged) = "onSelectionChanged()" 
> 
</ag-grid-ng2> 
</div> 
`, 
directives: [(<any>window).ag.grid.AgGridNg2], 
}) 

這我裏面的類代碼以獲取選定值

export class AgGride { 
gridOptions = { 
    columnDefs: 'columnDefs', 
    rowData: 'rowData', 
    rowSelection: 'single', 
    getSelectedRows: 'getSelectedRows', 
    onSelectionChanged: 'onSelectionChanged' 
}; 

columnDefs = [ 
    { headerName: "Make", field: "make", editable: true }, 
    { headerName: "Model", field: "model", editable: true }, 
    { headerName: "Color", field: "Color", editable: true } 
]; 

rowData = [ 
    { make: "Toyota", model: "Celica", Color: "Red"}, 
    { make: "Ford", model: "Mondeo", Color: "Blue"}, 
    { make: "Tata", model: "X100", Color: "Blue"}, 
    { make: "Volvo", model: "X5", Color: "White"},  
]; 

mgrid: any; 
onSelectionChanged() { 
    var selectedRows = this.mgrid.ag.this.gridOptions.getSelectedRows(); 
    console.log(selectedRows); 


} 
} 

有人請告訴我,我怎麼能改正我的錯誤,這樣我會盡快選定行的數據/值我控制檯窗口...

回答

3

在模板上,例如:

(rowClicked)='onRowClicked($event)' 
(cellClicked)='onCellClicked($event)' 
(selectionChanged) = 'onSelectionChanged($event)' 

,然後在組件類:

onRowClicked(event: any) { console.log('row', event); } 
onCellClicked(event: any) { console.log('cell', event); } 
onSelectionChanged(event: any) { console.log("selection", event); } 

使用Chrome的控制檯來檢查事件對象的內容。

0

您可以使用api.getSelectedRows()返回所選行數據的數組。

public getSelectedRows(){ 
     let rowsSelection = this.gridOptions.api.getSelectedRows(); 
     console.info(rowsSelection); 
     } 

這對我很有用。

相關問題