reactjs 如何将行数据传递到MUI React类型脚本中的工具栏

d8tt03nd  于 2023-01-04  发布在  React
关注(0)|答案(1)|浏览(109)

因为我想在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>
    );
}
pjngdqdw

pjngdqdw1#

非常感谢MUI团队的帮助,现在问题已经解决。在这里找到了这个问题的解释:https://github.com/mui/mui-x/issues/7377
此处的演示:https://codesandbox.io/s/sleepy-lehmann-vgvlq8?file=/demo.tsx
我把它抄在这里,希望对有同样疑问的人有所帮助

function Auctions({ selectionModel }) {
  const hasSelectedRows = selectionModel.length > 0;

  return (
    <React.Fragment>
      <IconButton disabled={!hasSelectedRows}>
        <DeleteIcon color={hasSelectedRows ? "error" : "disabled"} />
      </IconButton>
    </React.Fragment>
  );
}

function DataToolbar({ selectionModel }) {
  return (
    <GridToolbarContainer>
      <Auctions selectionModel={selectionModel} />
      <GridToolbarQuickFilter />
      <GridToolbarColumnsButton />
      <GridToolbarDensitySelector />
    </GridToolbarContainer>
  );
}

export default function FlexLayoutGrid() {
  const { data } = useDemoData({
    dataSet: "Commodity",
    rowLength: 5,
    maxColumns: 6
  });

  const [selectionModel, setSelectionModel] = React.useState<
    GridSelectionModel
  >([]);

  return (
    <div style={{ height: 400, width: "100%" }}>
      <DataGrid
        {...data}
        onSelectionModelChange={(newSelectionModel) => {
          setSelectionModel(newSelectionModel);
        }}
        selectionModel={selectionModel}
        components={{ Toolbar: DataToolbar }}
        checkboxSelection
        componentsProps={{
          toolbar: {
            selectionModel: selectionModel
          }
        }}
      />
    </div>
  );
}

相关问题