【问题标题】:How to called this.getRowStyle function forcefully in Angular 7?如何在 Angular 7 中强制调用 this.getRowStyle 函数?
【发布时间】:2021-12-05 11:14:26
【问题描述】:

我正在使用 getRowStyle 函数,但它似乎只在加载或编辑网格时运行一次。如何手动强制运行此功能以便加载行颜色。

conditonalFormatter() {
    this.GridRef.rowConditionalFormatting('red', this.rowFormatData);
}

rowConditionalFormatting(rowColor, rowFormatData) {
    this.rowFormatData = rowFormatData;
    this.getRowStyle = params => {
            for (let i = 0; i < this.rowFormatData.length; i++) {
                const columnHeader = this.rowFormatData[i].Name;
                const operator = this.rowFormatData[i].Operator;
                const cellValue = this.rowFormatData[i].Value;
                if (params.data[columnHeader] == cellValue) {
                    return {background: rowColor };
                }
            } 
        }
}

【问题讨论】:

  • 请提供Minimal, Reproducible Example,否则我们不知道您已经尝试过什么。
  • @Batajus - 请参考 abv 函数。它在加载时工作,但如果我从其他地方调用这个函数,那么 this.getRowStye 将不会被调用。
  • 你能不能也分享一下你调用的代码sn-ps this.getRowStyle
  • @Batajus 我使用引用从差异组件中调用了它。谢谢。
  • 你试过rowClassRules 吗?

标签: angular angular7 ag-grid ag-grid-angular


【解决方案1】:

如果您想再次手动应用行样式,则需要使用 refreshCells() 手动刷新 Aggrid。

一般来说,只有那些数据发生变化的行会被重绘,而其他行会被忽略。在您的情况下,我们将需要强制 agggrid 重绘行,因此必须使用 force=true

gridApi.refreshCells({
      force: true // skips change detection -> always refreshes the row
   })

你会得到gridApi对象,在aggrid提供的onGridReady回调中。

public onGridReady(params) {
    /**
    * Function is called when grid is instantiated.
    * @param params Ag grid params
    */
    this.gridApi = params.api;
    this.gridColumnApi = params.columnApi;
  }

HTML 代码:

<ag-grid-angular class="ag-theme-alpine" [columnDefs]="columnDefs [gridOptions]="gridOptions" (gridReady)="onGridReady($event)"></ag-grid-angular>

【讨论】:

    【解决方案2】:

    经过几次尝试,我能够按照下面的代码实现我的目标,只需在外面调用 this.getRowStyle -

    rowConditionalFormatting(rowColor, rowFormatData) {
       this.rowFormatData = rowFormatData;
       this.rowColor = rowColor;    
    }
    
     this.getRowStyle = params => {
        for (let i = 0; i < this.rowFormatData.length; i++) {
            const columnHeader = this.rowFormatData[i].Name;
            const operator = this.rowFormatData[i].Operator;
            const cellValue = this.rowFormatData[i].Value;
            if (params.data[columnHeader] == cellValue) {
                return {background: this.rowColor };
            }
        }    
     }
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    • 上面的代码没有调用this.getRowStyle,它将函数定义lambda表达式。在此表达式中,this.rowFormatData 的属性被评估并根据匹配的params.data[columnHeader](即当前单元格值)动态返回样式。
    • this.getRowStyle 本身是一个函数,它会自动调用。在控制台中打印参数并检查。参考 - ag-grid.com/angular-data-grid/row-styles
    猜你喜欢
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    • 2013-03-02
    • 2013-02-27
    • 2020-07-04
    • 1970-01-01
    相关资源
    最近更新 更多