ag-grid and angular, how to switch grid options dynamically

99封情书 提交于 2019-12-04 05:20:03

问题


I have two different grid configurations (represented as two gridOption objects). One is loading local data, where the other one is using an elasticsearch datasource with the infinite row model.

If I wire them in the template [gridOptions]="localOptions", [gridOptions]="elasticGridOptions" both work perfectly well.

This is my current component and (schematically) what I want to achieve:

@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??
  }

}

I wire [gridOptions] to a generic options object and then on a button click I try to switch between the two. The interesting thin is, even if I put this.gridOptions = this.localOptions; as a last line into onGridReady it also doesn't work. I have the feeling I need to somehow tell the grid to actually reload the options, or to reinitialize itself, but I couldn't find it in the documentation.

Cheers


回答1:


Currently, gridOptions can't be reloaded dynamically. Check here:

Dynamically Setting Properties for ag-Grid

Repopulate ag-grid with data

You need to re-render your page. Anyway, you can handle requirement's on resolver part to prepare your options and then use it in component.



来源:https://stackoverflow.com/questions/52519129/ag-grid-and-angular-how-to-switch-grid-options-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!