How to initialize ag-grid api in angular2 application

前端 未结 3 1106
离开以前
离开以前 2021-01-14 19:00

I am working on an application built using angular2 with typescript. I am using ag-grid to display data in grid but not able to find the grid api.

/// 

        
3条回答
  •  一生所求
    2021-01-14 19:37

    You want to initialize gridOptions as a property of the class, not just a variable. So it should be this.gridOptions:

    constructor(private _heroService: HeroService) {
    
        console.log("in Grid constructor...");
    
        this.columnDefs = [
            { headerName: "ID", field: "id", sortingOrder: ["asc", "desc"], editable: false, width: 100 },
            { headerName: "Name", field: "name", sortingOrder: ["asc", "desc"], editable: false, hide: false }
        ];
    
        this._heroService.getHeroes().then(heroes => this.rowData = heroes);
    
        this.gridOptions = {
            enableSorting: true,
            rowData: this.rowData,
            columnDefs: this.columnDefs,
            onReady: () => {
                this.gridOptions.api.sizeColumnsToFit();
                alert(this.gridOptions.api);
            }
        }
    }
    

提交回复
热议问题