我有一个带有用户的数据表,我想在行上进行删除按钮工作,但是看来它不能通过反应手段来完成。
数据杂志是这样使用的:
<DataGrid
rows={users}
columns={columns}
pageSize={5}
checkboxSelection
/>
我有一个带有自定义RenderCell函数的列,显示一些动作按钮。列定义是:
{
field: "actions",
headerName: "",
width: 120,
type: "",
sortable: false,
renderCell: (
params: GridCellParams
): React.ReactElement<any, string | React.JSXElementConstructor<any>> => {
return (
<UserRowActions
userId={params.getValue(params.id, "id")?.toString()!}
/>
);
}
}
参数对象提供了一些属性,但我不知道如何执行这样的操作:删除单击按钮的行,在UserRowActions
组件中定义的按钮。我还想找到是否可以使用今天的MUI DataGrid组件进行此操作。 我不知道该怎么办,因为API现在看起来对我没有反应。 我使用:
"@material-ui/core": "^4.12.1",
"@material-ui/data-grid": "^4.0.0-alpha.30",
"react": "^16.14.0",
I专门用于数据网格动作按钮:
export const DataGridContext = React.createContext<{ deleteUser?: (uid: string) => void }>({});
// ...
const { data: users, isLoading, isError } = useGetUsersQuery();
const [usersRows, setUsersRows] = useState<IUser[]>([]);
useEffect(() => {
if (typeof users !== 'undefined') {
setUsersRows(users);
}
}, [users]);
<DataGridContext.Provider value={{ deleteUser: (uid: string) => {
const newRows = [...usersRows];
const idx = newRows.findIndex(u => u.id === uid);
if (idx > -1) {
newRows.splice(idx, 1);
setUsersRows(newRows);
}
}}}>
<DataGrid
rows={usersRows} // ...
/>
</DataGridContext.Provider>
// In the UserRowActions component:
const dataGrid = useContext(DataGridContext);
// ...
dataGrid.deleteUser!(userId);