Search code examples
angulartypescriptag-gridag-grid-angular

How can I dynamically change the options of ag-Grid?


I am creating an ag-grid with the below code. I would like to dynamically change the rowSelection option from single to multiple onClick of a button.

<ag-grid-angular 
  #validateGrid
  style="width: 780px; height: 260px;" 
  class="ag-theme-balham"
  rowSelection="single"
  suppressRowClickSelection="false"
  suppressHorizontalScroll="true" 
  [rowData]="rowData" 
  [columnDefs]="columnDefs"
  [defaultColDef]="defaultColDef" 
  [getRowNodeId]="getRowNodeId"
  (selectionChanged)="onSelectionChanged()"
  (gridReady)="onGridReady($event)" 
  enableCellTextSelection=true>
</ag-grid-angular>

In my Typescript code, I have tried this.gridOptions.rowSelection = 'multiple'; but this does not work. Is there any way to do this?


Solution

  • Try this:

    your tmpl:

    <ag-grid-angular 
      #validateGrid
      style="width: 780px; height: 260px;" 
      class="ag-theme-balham"
      rowSelection="{{rowSelection}}" // Updated
      suppressRowClickSelection="false"
      suppressHorizontalScroll="true" 
      [rowData]="rowData" 
      [columnDefs]="columnDefs"
      [defaultColDef]="defaultColDef" 
      [getRowNodeId]="getRowNodeId"
      (selectionChanged)="onSelectionChanged()"
      (gridReady)="onGridReady($event)" 
      enableCellTextSelection=true>
    </ag-grid-angular>
    

    your ts.

    public rowSelection: string = 'single';
    
    public changeRowSelection(): void {
        this.rowSelection = 'multiple'; // this is example
    }