简体   繁体   English

React Mui data-grid-pro 可扩展行,可以从父级重置模型

[英]React Mui data-grid-pro expandable rows with possibility to reset models from the parent

I have data-grid-pro component with these parameters我有带有这些参数的 data-grid-pro 组件

<DataGridPro
        apiRef={apiRef}
        density="comfortable"
        autoHeight
        headerHeight={75}
        getRowId={(row) => row.domain_id}
        loading={tableData?.length === 0}
        rows={tableData}
        columns={columns}
        initialState={savedState.initialState}
        pageSize={pageSize}
        rowsPerPageOptions={[10, 25, 50, 100]}
        onPageSizeChange={(newPageSize) => setPageSize(newPageSize)}
        getDetailPanelHeight={getDetailPanelHeight}
        getDetailPanelContent={renderRowSubComponent}
        pagination
        components={{ Toolbar: CustomToolBar }}
        componentsProps={{ toolbar: { onResetFilters: handleResetFilters } }}
        }}
/>

renderRowSubComponent is doing some async call and making expandable Data-grid pro children content with header (it means the data -grid have possibility to filter, sort..etc) renderRowSubComponent 正在做一些异步调用,并使可扩展的 Data-grid pro 子内容具有 header(这意味着数据网格有可能进行过滤、排序等)

I have made a custom toolbar CustomToolBar, where onResetFilters is doing model cleanup apiRef.current.setSortModel([]), apiRef.current.setFilterModel({ items: [] })...etc我制作了一个自定义工具栏 CustomToolBar,其中 onResetFilters 正在执行 model 清理 apiRef.current.setSortModel([]), apiRef.current.setFilterModel({ items: [] })...等

<GridToolbarContainer>
      <GridToolbarColumnsButton sx={{ ml: 1 }} />
      <GridToolbarFilterButton sx={{ ml: 1 }} />
      <GridToolbarDensitySelector sx={{ ml: 1 }} />
      <GridToolbarExport sx={{ ml: 1 }} />
      <Button
        color="primary"
        rel="noopener"
        size="small"
        startIcon={<RotateLeft />}
        onClick={() => onResetFilters(apiRef.current)}
      >
        {RESET_FILTERS}
      </Button>
    </GridToolbarContainer>

Is it possible from the parent toolbar make cleanup for all instances?是否可以从父工具栏对所有实例进行清理? I don't want have toolbar for each expanded rows, I want to have one parent toolbar, and clear models from it.我不希望每个展开的行都有工具栏,我想要一个父工具栏,并从中清除模型。

To make it you can pass componentsProps to the DataGrid为此,您可以将componentsProps传递给DataGrid

components={{ Toolbar: CustomToolBar }}
componentsProps={{ toolbar: { onResetFilters: handleResetFilters } }}

on the handleResetFilters method you will handle it like this在 handleResetFilters 方法上,您将像这样处理它

const handleResetFilters = () => {
    apiRef.current.setSortModel([]);
    apiRef.current.setFilterModel({ items: [] });
    apiRef.current.setSelectionModel([]);
    apiRef.current.setColumnVisibilityModel({});
    apiRef.current.setEditRowsModel({});
    apiRef.current.setPinnedColumns([]);
    apiRef.current.setEditRowsModel({});
    // clear children instances
    Object.entries(apiRefs).forEach(([key, ref]) => {
      if (ref !== null) {
        ref.current.setSortModel([]);
        ref.current.setFilterModel({ items: [] });
        ref.current.setSelectionModel([]);
        ref.current.setColumnVisibilityModel({});
        ref.current.setPinnedColumns([]);
      } else {
        // remove unused keys
        delete apiRefs[key];
      }
    });
  };

Where apiRef is useGridApiRef from "@mui/x-data-grid-pro" and apiRefs is the state of the parent component setup on the children instances.其中 apiRef 是来自“@mui/x-data-grid-pro”的 useGridApiRef, apiRefs是子实例上父组件设置的 state。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM