2017-04-04 30 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モーザーの答えはあなたの質問にお答えしましたあなたはコンストラクタで設定する必要がWICH、onGridReadyイベントのAPIオブジェクトにAPI関数をフック?私は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

は...

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

myRowClickedHandler(event) { 
    this.createdDate = event.data.createdDate; 
} 
関連する問題