Ag-grid单击后如何更新特定的单元格样式

时间:2019-08-21 07:44:38

标签: angularjs ag-grid

点击后,我使用refreshCells更新特定的单元格样式。但是,这不起作用,并且当我设置refreshCells({ force: true })时,整列将更新我不想要的新样式。有什么建议吗?

    vm.gridOptions = {
        columnDefs: vm.columnDefs,
        rowData: vm.rows,
        angularCompileRows: true,
        defaultColDef: {
            sortable: true,
            resizable: true,
        },
        onCellClicked: onCellClicked,
        onGridReady: function (params) {

        }
    }

    function onCellClicked(params) {
        const focusedCell =  params.api.getFocusedCell();
        focusedCell.column.colDef.cellStyle = { 'background-color': '#b7e4ff' };
        params.api.refreshCells({
            force: true // this updates the whole column, not only the clicked cell
        });
    }

1 个答案:

答案 0 :(得分:0)

我通过设置特定的rowNodescolumns解决了这个问题。

    function onCellClicked(params) {
        const focusedCell =  params.api.getFocusedCell();
        const rowNode = params.api.getRowNode(focusedCell.rowIndex);
        const column = focusedCell.column.colDef.field;
        focusedCell.column.colDef.cellStyle = { 'background-color': '#b7e4ff' };
        params.api.refreshCells({
            force: true,
            columns: [column],
            rowNodes: [rowNode]
        });
    }