0

As per the requirement we will get values from api and those rows should be pre-selected when loading Ag Grid.


this.columnDefs = [
      {
        headerName: 'Option Code',
        field: 'Option',
        maxWidth: 125,
        checkboxSelection: true,
      }
];

this.addOption = ["000005","000010","000026"]; // External data 

On Grid Ready:

onGridReady(params) { 
    this.gridApi = params.api; 
    this.gridColumnApi = params.columnApi; 
    this.gridApi.getRenderedNodes().forEach(function (node:any) {
        node.setSelected(this.addOption.includes(
            node.data.Option.toString()
        )); 
     }); 
}

We are trying below code to pre-select default checkbox and it is not working.

selectAllAmerican(val) { 
    this.gridApi.getRenderedNodes().forEach(function (node:any) {
        node.setSelected(val.includes(node.data.Option.toString()));
    }); 
} 

The above code is working when we used under Get button. Kindly review and guide us.

jdaz
  • 5,964
  • 2
  • 22
  • 34
user13923157
  • 11
  • 1
  • 1
  • On Grid Ready: onGridReady(params) { this.gridApi = params.api; this.gridColumnApi = params.columnApi; this.gridApi.getRenderedNodes().forEach(function (node:any) { node.setSelected(this.addOption.includes(node.data.Option.toString())); }); } – user13923157 Jul 13 '20 at 14:38
  • We are trying below code to pre-select default checkbox and it is not working. selectAllAmerican(val) { this.gridApi.getRenderedNodes().forEach(function (node:any) { node.setSelected(val.includes(node.data.Option.toString())); }); } The above code is working when we used under Get button. Kindly review and guide us . – user13923157 Jul 13 '20 at 14:38
  • 1
    Hi there, could you put all the code and explanation into the question, rather than adding it as comments. That means everything is in the same place, in the question. – Michelle Jul 13 '20 at 15:16
  • Added comments into question body – jdaz Jul 13 '20 at 23:03
  • As per the requirement we will get values from api and those rows should be pre-selected when loading Ag Grid and when user select/Un Select the check box need to perform the task – user13923157 Jul 14 '20 at 18:22

2 Answers2

2

Answering late , I faced a similar problem , and the solution is to put the below line when the rowData or grid data is ready be it and also in GridReady method. so mention this in both places .

this.gridApi?.forEachNode(function (node) {
                        node.setSelected(true);
            });
Shaswata
  • 1,049
  • 1
  • 9
  • 27
0

As per the requirement we will get values from api and those rows should be pre-selected when loading Ag Grid and when user select/Un Select the check box need to perform the task. Column defination this.columnDefs = [ { headerName: 'Option Code', field: 'Option', maxWidth: 125, checkboxSelection: true, } ];

Grid Defination :

  <ag-grid-angular
  #agGrid
  style="width: 375px; height: 100%; display: inline-block;"
  *ngIf="Flag12"
  id="myGrid"
  class="userclass"
  [enableBrowserTooltips]="true"
  [columnDefs]="columnDefs"
  [defaultColDef]="defaultColDef"
  [suppressRowClickSelection]="true"
  [rowSelection]="rowSelection"
  [rowDeselection]="true"
  [rowData]="rowData"

(rowSelected)="onRowSelected($event)"

  (gridReady)="onGridReady($event)"
  >

this.addOption = ["000005","000010","000026"]; // External data

Two Questions : 1.How to pre-select checkbox based on external variable this.addOption . 2. rowSelected : In case of pr-select(when we are enabling the checkbox from logic), rowSelected should disable and when user select/un Select checkbox rowSelected (need to perform some logic ), due to logic it may enable other check box from back-end. So it should work when user select and un select .

Kindly guide me .

user13923157
  • 11
  • 1
  • 1
  • Ag-grid not support all the the feature and I am not using ag-grid , since it is very difficult to implement all the option . It is working simple scenarios and I am not getting much support from forum . – user13923157 Jul 19 '20 at 17:25