radsin
radsin

Reputation: 105

I want to set checkbox to true based on a condition in ag-grid

I have a button which basically imports some data. This imported data needs to be compared with the data inside already loaded ag-grid and if there is a match, set the cehckbox of that particlar row node to true.

This is the button which checks for the condition:

    enableCheck() {
    alert('works');
    if (this.DataService.isNotBlank(this.rowDataImport)) {
        for (let i = 0; i < this.rowDataImport.length; i++) {
            if (this.DataService.isNotBlank(this.rowData)) { 
                for (let j = 0; j < this.rowData.length; j++) { 
                    if (this.DataService.isNotBlank(this.rowData[j].calDate)) {
                        for (const calDates of this.rowData[j].calDate) {
                            if (
                            this.rowDataImport[i].isin === calDates.isin ||
                            this.rowDataImport[i].portfolioName === calDates.portfolioName ||
                            this.rowDataImport[i].valuationDate === calDates.valuationDate
                                ) 
                                {
                                     // alert('true')
                                    this.checkValue = true;
                                } else {
                                    this.checkValue = false;                                        
                                }
                        }
                    }
                }
            }
        }
      }

}

The this.checkValue is a flag which will be true if match is found.

  public gridColumnDefs = [
        {
            headerName: 'Portfolio Name',
            field: 'portfolioName',
            cellRenderer: 'agGroupCellRenderer',
            headerCheckboxSelection: true,
            headerCheckboxSelectionFilteredOnly: true,
            checkboxSelection: true,
            pinned: 'left',
            filter: true,
            cellRendererParams:(params) => {
                console.log(params);
                if (this.checkValue) {
                 params.node.selected = true;
                }
            }
        },
]

here I used cellRendererParams. But this will only for on load I guess. What to do if I want to update the ag-grid row from a value outside i.e. from import check as given above?

Upvotes: 2

Views: 6263

Answers (2)

radsin
radsin

Reputation: 105

I manpulated a bit and did the below which worked like a charm.

 this.gridApi.forEachNode(node => {
                    if (node.data.isin === this.rowDataImport[i].isin &&
                            node.data.portfolioName === this.rowDataImport[i].portfolioName &&
                            node.data.valuationDate === this.rowDataImport[i].valuationDate
                    ) {
                        node.setSelected(true);
                    }

Upvotes: 1

shutsman
shutsman

Reputation: 2510

First of all, you should add id for each row in defaultColDef

this.defaultColDef = {
  getRowNodeId: data => data.id,
};

Then you can find this id and set the checkbox to true.

Also, you can find separate field by name. It is really easy, you should use the next combination

  selectRow() {
    this.gridApi.forEachNode(node => {
        if (node.id == 1 || node.id == 2 || node.data.country == 'Australia') {
            node.setSelected(true);
        }
    });
  }

Working example: https://plnkr.co/edit/ijgg6bXVleOAmNL8

In this example when we click on the button - we set the checkbox to true for two rows with id 1 and 2 and for each field that has country 'Australia'

And in your case, you are using incorrect configuration.

You should you cellRenderer

cellRenderer: params => {
  if(params.value === 'Ireland') {
    params.node.setSelected(true)
  }
  return params.value
},

One more example: https://plnkr.co/edit/PcBklnJVT2NsNbm6?preview

Upvotes: 2

Related Questions