简体   繁体   English

ag-grid(反应)根据 state 动态更改 rowStyle

[英]ag-grid (react) change rowStyle dynamically based on state

I need to change the rowStyle of few rows based on the "state"我需要根据“状态”更改几行的 rowStyle

const [highlightRowData, setHighlightRowData] = useState(0); const [highlightRowData, setHighlightRowData] = useState(0);

lets say this gets changed dynamically by some user action outside the grid.可以说这会因网格外的某些用户操作而动态更改。

this is my implementation of rowStyle:这是我对 rowStyle 的实现:

const rowStyle = (params) => {
    if (params.data.someData === highlightRowData) {
      return { background: "red" };
    }
    return { background: "yellow" };
};

but this state change doesn't get reflected in rowStyle.但此 state 更改并未反映在 rowStyle 中。

Not sure if you find an answer already, but like Pavan mentioned in the comment - you can checkout the gridApi.redrawRows() function described in the ag-grid doc in this link https://www.ag-grid.com/javascript-grid-refresh/#redraw-rows .不确定您是否已经找到答案,但就像评论中提到的 Pavan 一样 - 您可以在此链接https://www.ag-grid.com/javascript 中查看 ag-grid 文档中描述的 gridApi.redrawRows() function -网格刷新/#redraw-rows

To achieve your goal here, you need to call this function after you set your highlightRowData state. As you are using hook it may look like this:为了在这里实现您的目标,您需要在设置 highlightRowData state 后调用此 function。当您使用钩子时,它可能看起来像这样:

  useEffect(() => {
    // assume you have a gridApi reference here. If not yet, maybe you can hold it in a ref by useRef
    gridApi.redrawRows();
  }, [highlightRowData]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM