reactjs - AgGridReact - 当 isRowSelectable 更改时网格不会更新

标签 reactjs typescript ag-grid ag-grid-react

我想通过 isRowSelectable动态地作为 AgGridReact 的 Prop .在下面的玩具示例中,您希望单击“切换条件”按钮会更改网格中具有复选框的项目集。相反,网格没有变化。
截图:

interface Item { name: string, age: number, height: number}
const data: Item[] = [
    {name: "old and tall", age: 80, height: 80}, 
    {name: "old and short", age: 80, height: 20},
    {name: "young and tall", age: 10, height: 80}, 
    {name: "young and short", age: 10, height: 20}, 
]
const colDefs: ColDef[] = [
    {
        headerName: "",
        checkboxSelection: true,
        headerCheckboxSelection: true,
        headerCheckboxSelectionFilteredOnly: true,
        width: 60,
        sortable: false
    },
    { field: "name"},
    { field: "age"},
    { field: "height"}
]

const criteria1 = (node: RowNode) => node.data.age > 50
const criteria2 = (node: RowNode) => node.data.height > 50

export const DevPage: FunctionComponent = () => {

    const [isCriteria1, setIsCriteria1] = useState<boolean>(false)

    return ( <Fragment>
    
        <h2> {isCriteria1 ? "By Age" : "By Height"} </h2>
        <Button onClick = { () => setIsCriteria1(!isCriteria1) }> 
            Switch criteria
        </Button> 
        
        <div className="ag-theme-alpine" style={{height:"800px"}}>
            <AgGridReact
                rowSelection="multiple"
                columnDefs = {colDefs}
                rowData={data}
                isRowSelectable = {isCriteria1 ? criteria1 : criteria2}
                suppressRowClickSelection={true}
            />
        </div>
        
    </Fragment> )
}

最佳答案

作为解决方法,您可以强制所有 RowNode s 在 onClick 时重新评估可选属性事件触发。

const [rowData, setRowData] = React.useState<RowDataType[]>([]);
const criterial = React.useRef(true);
const isSelectable = (rowNode: RowNode) => {
  const result = rowNode.data.age > 25;
  const selectable = result === criterial.current;

  return selectable;
};
const toggleCriteria = () => {
  const updateSelectable = (rowNode: RowNode) => {
    rowNode.setRowSelectable(isSelectable(rowNode));
  };

  criterial.current = !criterial.current;
  gridApi.deselectAll();
  gridApi.forEachNodeAfterFilterAndSort(updateSelectable);
};

return (
  <>
    <button onClick={toggleCriteria}>Toggle Criteria</button>
    <AgGridReact
      rowSelection="multiple"
      suppressRowClickSelection
      columnDefs={columnDefs}
      onGridReady={onGridReady}
      isRowSelectable={isSelectable}
      rowData={rowData}
    />
  </>
);
现场演示
Edit 64014770/aggridreact-grid-does-not-update-when-isrowselectable-changes/

关于reactjs - AgGridReact - 当 isRowSelectable 更改时网格不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64014770/

相关文章:

Angular 和 ag-grid : cannot register framework component using frameworkComponents

java - Spring Boot + React CORS 问题,没有 spring security

javascript - 继续调用函数,直到满足条件

asp.net-mvc - Angular 2 (SystemJS) XHR 错误(未找到 404)/@angular/animations

angular - 在 Visual Studio 2015 中从类型转换为@types

javascript - 如何使用ag-grid加载动态列和行?

javascript - 转换成json对象标签值格式

reactjs - 从传递给通用 React 组件的回调返回的对象属性的 Typescript 验证?

typescript - Angular 2 使用 ngFor 设置和绑定(bind)复选框

javascript - 操作多个复选框选择代码如何工作?