【发布时间】:2019-03-02 07:32:46
【问题描述】:
我有两种不同的网格配置(表示为两个gridOption 对象)。一种是加载本地数据,另一种是使用具有无限行模型的 elasticsearch 数据源。
如果我将它们连接到模板[gridOptions]="localOptions",[gridOptions]="elasticGridOptions" 两者都可以很好地工作。
这是我当前的组件和(示意性地)我想要实现的目标:
@Component({
selector: 'app-gridtest',
styleUrls: ['./gridtest.component.css'],
template: `
<ag-grid-angular
id="grid"
style="width: 100vw;"
[style.height] = 'height'
class="ag-theme-balham ag-grid"
[columnDefs]="columnDefs"
[gridOptions]="gridOptions"
(gridReady)="onGridReady($event)"
></ag-grid-angular>
<button (click)="switchOptions()"></button>
`
})
export class GridtestComponent implements OnInit{
constructor(
private esDs: ElasticDatasource
) {}
height = '0px';
gridApi: any;
columnsApi: any;
gridOptions: {};
columnDefs = [
{headerName: 'Id', field: 'id'},
{headerName: 'Title', field: 'title'}
];
elasticGridOptions = {
rowModelType: 'infinite'
};
localOptions = {
rowData: [
{id: '1', title: 'foo'},
{id: '2', title: 'bar'},
{id: '3', title: 'baz'}
]
};
@HostListener('window:resize', ['$event'])
resizeGrid(){
this.height = `${$(window).innerHeight()-60}px`;
}
ngOnInit(): void {
this.elasticGridOptions['datasource'] = this.esDs;
}
onGridReady(params){
this.resizeGrid();
this.gridApi = params.api;
this.columnsApi = params.columnApi;
}
isElastic = false;
switchOptions(){
if(this.isElastic){
this.gridOptions = this.localOptions;
this.isElastic = false;
}else{
this.gridOptions = this.elasticGridOptions;
this.isElastic = true;
}
//reinitialize / update options or grid??
}
}
我将[gridOptions] 连接到一个通用选项对象,然后单击按钮尝试在两者之间切换。有趣的是,即使我将this.gridOptions = this.localOptions; 作为最后一行放入onGridReady,它也不起作用。我觉得我需要以某种方式告诉网格实际重新加载选项或重新初始化自身,但我在文档中找不到它。
干杯
【问题讨论】:
标签: angular ag-grid ag-grid-ng2