ag-grid 和 Angular,如何动态切换网格选项

Tom*_*Tom 5 ag-grid ag-grid-ng2 angular

我有两种不同的网格配置(表示为两个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??
  }

}
Run Code Online (Sandbox Code Playgroud)

我连接[gridOptions]到一个通用选项对象,然后单击按钮,尝试在两者之间切换。有趣的是,即使我把它this.gridOptions = this.localOptions;作为最后一行onGridReady也不起作用。我感觉我需要以某种方式告诉网格实际重新加载选项,或者重新初始化自身,但我在文档中找不到它。

干杯

un.*_*ike 2

目前,gridOptions无法动态重新加载。检查这里:

动态设置 ag-Grid 的属性

用数据重新填充 ag-grid

您需要重新渲染您的页面。无论如何,您可以处理部分要求resolver来准备您的内容options,然后在component.