因为我想在GridHeader中添加选择后的操作,所以我按照教程尝试将它们放在一起:https://mui.com/x/react-data-grid/selection/#controlled-selection
https://mui.com/x/react-data-grid/column-visibility/#column-visibility-panel 我相信这是一个非常普遍和通用的要求,当我选择一行或多行时,我无法将结果数据传递给GridToolbar子组件,似乎它不支持传递属性,有示例代码:
export defualt function App(props: any){
const {rows, columns} = props;
const [selectionModel, setSelectionModel] = useState<GridSelectionModel>([]);
return (
<DataGrid components={{ Toolbar: DataToolbar }}
onSelectionModelChange={(newSelectionModel) => {
setSelectionModel(newSelectionModel);
}}
selectionModel={selectionModel}
{...others} />
);
}
function DataToolbar (){
<GridToolbarContainer>
<Auctions />
<GridToolbarQuickFilter />
<GridToolbarColumnsButton />
<GridToolbarDensitySelector />
</GridToolbarContainer>
}
function Auctions (){
const apiRef = useGridApiContext();
const [selected, setSelected] = useState(false);
const handleSelected: GridEventListener<'rowSelectionCheckboxChange'> = (
params, event, details) => {
//Processing Event
setSelected(!selected);
};
useGridApiEventHandler(apiRef, 'rowSelectionCheckboxChange', handleSelected);
return (
<Fragment>
<IconButton disabled={!selected}><DeleteIcon color={selected? "error" : "disabled"} /></IconButton>
</Fragment>
);
}
1条答案
按热度按时间pjngdqdw1#
非常感谢MUI团队的帮助,现在问题已经解决。在这里找到了这个问题的解释:https://github.com/mui/mui-x/issues/7377
此处的演示:https://codesandbox.io/s/sleepy-lehmann-vgvlq8?file=/demo.tsx
我把它抄在这里,希望对有同样疑问的人有所帮助