gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:55:19 28 4
gpt4 key购买 nike

我想通过 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com