[英]setColumnDefs does not work in Angular 5
As what has been suggested elsewhere, 正如其他地方所建议的那样,
setColumnDefs is not working for some ag-grids setColumnDefs对于某些农业电网不起作用
How to initialize ag-grid api in angular2 application 如何在angular2应用程序中初始化Ag-Grid API
I have already initialized the gridOptions in my class constructor. 我已经在我的类构造函数中初始化了gridOptions。 But when I tried to setColumnDefs, it still gave me error:
但是当我尝试设置setColumnDefs时,它仍然给我错误:
TypeError: Cannot read property 'setColumnDefs' of undefined TypeError:无法读取未定义的属性“ setColumnDefs”
What else am I missing here? 我在这里还想念什么?
export class ConfigurationComponent implements OnInit {
constructor(
private configurationService: ConfigurationService,
)
{
this.gridOptions = {
enableSorting: false,
rowData: this.tableData,
columnDefs: this.tableColumns,
onGridReady: () => {
this.gridOptions.api.sizeColumnsToFit();
this.gridOptions.api.setColumnDefs(this.tableColumns);
alert(this.gridOptions.api);
}
}
}
tableData: string[] = [];
tableList: string[] = [];
tableName: string;
tableColumns: [{headerName: string, field: string}] = [{headerName: "", field: ""}];
tableRecord: {};
gridOptions: GridOptions;
ngOnInit() {
this.retrieveTableList();
}
retrieveTableList(){
/*blah blah*/
}
retrieveTableData(){
/*blah blah*/
this.configurationService.retrieveTableData(this.schemaFullname, this.tableName).subscribe(data => {
/* GETTING tableColumn HERE from the service*/
this.gridOptions.api.setColumnDefs(this.tableColumns);
}, error => {
console.error(error);
this.alertService.error("Get table data error", "No table data retrieved from data source for " + this.tableName);
})
}
}
As your comment says, 如您的评论所述,
It works now after I added [gridOptions]="gridOptions" in html.
我在html中添加[gridOptions] =“ gridOptions”后,现在可以使用。
Do you know why it worked? 你知道它为什么起作用吗?
As in your code, you are defining gridOptions
in your constructor. 就像在代码中一样,您正在构造函数中定义
gridOptions
。 In your onGridReady
function, nobody knows from where api
property (and sizeColumnsToFit
, etc methods) gets added. 在你
onGridReady
功能,没有人从那里知道api
属性( sizeColumnsToFit
等方法)被添加。
this.gridOptions = {
enableSorting: false,
rowData: this.tableData,
columnDefs: this.tableColumns,
onGridReady: () => {
this.gridOptions.api.sizeColumnsToFit();
this.gridOptions.api.setColumnDefs(this.tableColumns);
alert(this.gridOptions.api);
}
}
When you add [gridOptions]="gridOptions"
in your component, ag-grid component uses the gridOptions
object and injects other apis
for you. 当您在组件中添加
[gridOptions]="gridOptions"
时, ag-grid组件将使用gridOptions
对象并为您注入其他apis
。 Hence, it works afterwards. 因此,它随后将起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.