我想根据ag-grid中的条件将复选框设置为true

问题描述 投票:0回答:2

我有一个基本上可以导入一些数据的按钮。需要将此导入的数据与已加载的ag-grid中的数据进行比较,如果存在匹配项,请将该特定行节点的cehckbox设置为true。

这是检查条件的按钮:

    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;                                        
                                }
                        }
                    }
                }
            }
        }
      }

}

this.checkValue是一个标志,如果找到匹配项,则为true。

  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;
                }
            }
        },
]

这里我使用cellRendererParams。但这仅适用于负载。如果我想从外部值(即如上所述的导入检查)更新ag-grid行,该怎么办?

checkbox angular7 ag-grid
2个回答
0
投票

首先,您应该为defaultColDef中的每一行添加ID。>

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

然后您可以找到此ID并将复选框设置为true。

此外,您还可以按名称查找单独的字段。真的很简单,您应该使用下一个组合

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

工作示例:https://plnkr.co/edit/ijgg6bXVleOAmNL8

在此示例中,当我们单击按钮时,我们将ID为1和2的两行以及每个具有“澳大利亚”国家/地区的字段的复选框都设置为true。

并且在您的情况下,您使用的配置不正确。

您应该cellRenderer

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

另一个示例:https://plnkr.co/edit/PcBklnJVT2NsNbm6?preview

我做了一些操作,做了下面的工作,就像一个魅力。

 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);
                    }

0
投票

我做了一些操作,做了下面的工作,就像一个魅力。

© www.soinside.com 2019 - 2024. All rights reserved.