材质 UI Datagrid 粘性标题

Kel*_*vin 6 datagrid material-ui

是否可以使 Material UI Datagrid 标题具有粘性?似乎这可以通过 Material UI Tables 实现。我无法找到使用数据网格来完成此操作的选项。

697*_*697 5

正如评论中提到的,这是默认行为。然而,很少有错误可以阻止这种默认功能。就我而言,这是我错过的财产: autoHeight={true},它应该是false或只是将其删除。

这是一个详细的代码示例,下面是 SandBox 的链接:

import { DataGrid, GridToolbar } from "@mui/x-data-grid";
import React from "react";

export default function AdvanceTable() {
  
  const [pageSize, setPageSize] = React.useState(5);
  const [editRowsModel, setEditRowsModel] = React.useState({});

  const handleEditRowsModelChange = React.useCallback((model) => {
    // Set current edited cell
    setEditRowsModel(model);
  });

  return (
    <div>
      <div style={{ width: "100%", height: "80vh" }}>
        <DataGrid
          components={{
            Toolbar: GridToolbar
          }}
          rows={rows}
          columns={columns}
          disableSelectionOnClick
          checkboxSelection
          onEditRowsModelChange={handleEditRowsModelChange}
          pagination
          pageSize={pageSize}
          onPageSizeChange={(newPageSize) => setPageSize(newPageSize)}
          rowsPerPageOptions={[5, 10, 15]}
        />
      </div>
      <div style={{ marginBottom: 8 }}>
        <code>Edited table values: {JSON.stringify(editRowsModel)}</code>
      </div>
    </div>
  );
  
  // mock data:
  const columns = [
    { field: "id", headerName: "ID", width: 70 },
    {
      field: "firstName",
      headerName: "First name",
      editable: true,
      width: 180
    },
    { field: "lastName", headerName: "Last name", editable: true, width: 180 },
    {
      field: "age",
      headerName: "Age",
      type: "number",
      editable: true,
      width: 150
    }
  ];

  const rows = [
    { id: 1, lastName: "Snow", firstName: "Jon", age: 35 },
    { id: 2, lastName: "Lannister", firstName: "Cersei", age: 42 },
    { id: 3, lastName: "Lannister", firstName: "Jaime", age: 45 },
    { id: 4, lastName: "Stark", firstName: "Arya", age: 16 },
    { id: 5, lastName: "Targaryen", firstName: "Daenerys", age: null },
    { id: 6, lastName: "Melisandre", firstName: null, age: 150 },
    { id: 7, lastName: "Clifford", firstName: "Ferrara", age: 44 },
    { id: 8, lastName: "Frances", firstName: "Rossini", age: 36 },
    { id: 9, lastName: "Roxie", firstName: "Harvey", age: 65 },
    { id: 41, lastName: "Stark", firstName: "Arya", age: 16 },
    { id: 51, lastName: "Targaryen", firstName: "Daenerys", age: null },
    { id: 61, lastName: "Melisandre", firstName: null, age: 150 },
    { id: 71, lastName: "Clifford", firstName: "Ferrara", age: 44 },
    { id: 81, lastName: "Frances", firstName: "Rossini", age: 36 },
    { id: 91, lastName: "Roxie", firstName: "Harvey", age: 65 }
  ];
}
Run Code Online (Sandbox Code Playgroud)

沙盒示例


Lit*_*ike 5

官方的解决方案似乎是要么指定固定高度,要么将表格放置在弹性容器内。

如果您想将表格部分放置在可滚动页面/容器的下方,那么这两个都不是很好的选择,因为您可能最终会得到嵌套的滚动条。

通常在这种情况下,您希望使用它autoHeight来显示完整的网格,并拥有一个真正的position: "sticky"标题,就像使用常规 HTML 一样<table>

DataGrid在撰写本文时不支持这种开箱即用的功能,但您仍然可以通过调整内部 .css 类来实现它。我确实想添加一个免责声明,即该解决方案会覆盖一些内部布局属性,因此在未来版本中存在可能会中断的风险。

export const StickyDataGrid = styled(DataGrid)(({theme}) => ({
    '& .MuiDataGrid-columnHeaders': {
        position: "sticky",
        // Replace background colour if necessary
        backgroundColor: theme.palette.background.paper,
        // Display header above grid data, but below any popups
        zIndex: theme.zIndex.mobileStepper - 1,
    },
    '& .MuiDataGrid-virtualScroller': {
        // Undo the margins that were added to push the rows below the previously fixed header
        marginTop: "0 !important"
    },
    '& .MuiDataGrid-main': {
        // Not sure why it is hidden by default, but it prevented the header from sticking
        overflow: "visible"
    }
}))
Run Code Online (Sandbox Code Playgroud)