2017-04-04 21 views
1

我在我的Angular 4應用程序中使用了ag-Grid的免費版本。Angular4 ag-Grid:GridOptions的API未定義

在下面的代碼我想在構造函數中自動調整電網:

constructor(private modalService: NgbModal) { 
    this.gridOptions = <GridOptions>{}; 

    const columnDefs = [...]; 
    const rowData = [...]; 

    this.gridOptions.columnDefs = columnDefs; 
    this.gridOptions.rowData = rowData; 

    this.gridOptions.api.sizeColumnsToFit(); 
} 

但在開發工具,我得到以下錯誤:

ERROR TypeError: Cannot read property 'sizeColumnsToFit' of undefined 
+0

難道@Jarod莫澤的答案回答你的問題?我無法讓我的網格在angular4中工作。如果你的代碼正在工作,你能否發佈它,以便我也可以得到一些幫助。 謝謝 –

回答

0

的gridOptions.api是隻有在創建新的agGrid.Grid實例後纔可用。例如:

this.gridOptions = <GridOptions>{}; 
//just an empty object right now 

const columnDefs = [...]; 
const rowData = [...]; 

this.gridOptions.columnDefs = columnDefs; 
this.gridOptions.rowData = rowData; 
// all the gridOptions has right now is columnDefs and rowData attributes 

this.gridOptions.api.sizeColumnsToFit(); 

//wherever you create the grid instance... 
new agGrid.Grid(gridDiv, gridOptions) 
//now the gridOptions object that you created has been filled out 
//  with the api and columnApi attributes and functions 
+0

如何在新的agGrid.Grid(gridDiv,gridOptions)中使用gridDiv。它是一個模板變量? –

0

掛鉤API函數對onGridReady事件的API對象,至極,你需要在構造函數中設置...

constructor() { 
    this.gridOptions = <GridOptions>{ 
     onGridReady:() => { 
     this.gridOptions.api.addEventListener('rowClicked', this.myRowClickedHandler); 
     } 
    }; 

myRowClickedHandler(event) { 
    this.createdDate = event.data.createdDate; 
}