smartzeng

像向日葵一样向着太阳

使用 Material UI 框架中的 Data Grid:深入探索列定义与管理

person  smartzeng    watch_later 2024-10-05 19:46:38
visibility 152    class Data Grid,Column    bookmark 专栏

Material UI 的 Data Grid 组件提供了一种强大且灵活的方式来展示和管理数据。在本篇博客中,我们将深入探讨 Data Grid 中关于列的定义、尺寸、可见性、自定义列、列头、列菜单、列跨越、列组、列排序及列固定等功能。通过示例代码,帮助你全面掌握这些功能的使用。

1. 安装 MUI X Data Grid

在开始之前,请确保你已安装必要的依赖:

npm install @mui/material @emotion/react @emotion/styled @mui/x-data-grid

2. 基本列定义

2.1 列定义基础

列定义通过 columns 属性实现,每一列都是一个对象,包含字段名、标题和其他属性。

import * as React from 'react';
import { DataGrid } from '@mui/x-data-grid';

const columns = [
  { field: 'id', headerName: 'ID', width: 90 },
  { field: 'name', headerName: 'Name', width: 150 },
  { field: 'age', headerName: 'Age', type: 'number', width: 110 },
  { field: 'email', headerName: 'Email', width: 200 },
];

const rows = [
  { id: 1, name: 'John Doe', age: 35, email: 'john@example.com' },
  { id: 2, name: 'Jane Smith', age: 42, email: 'jane@example.com' },
  { id: 3, name: 'Mike Johnson', age: 28, email: 'mike@example.com' },
];

const BasicDataGrid = () => {
  return (
    <div style={{ height: 400, width: '100%' }}>
      <DataGrid rows={rows} columns={columns} pageSize={5} />
    </div>
  );
};

export default BasicDataGrid;

3. 列尺寸

3.1 自定义列宽

通过设置 width 属性可以自定义列的宽度。如果需要自适应宽度,可以使用 flex 属性:

const columns = [
  { field: 'id', headerName: 'ID', width: 90 },
  { field: 'name', headerName: 'Name', flex: 1 },
  { field: 'age', headerName: 'Age', type: 'number', width: 110 },
  { field: 'email', headerName: 'Email', width: 200 },
];

3.2 自定义行高

可以通过 getRowHeight 方法动态设置行高:

const getRowHeight = (params) => {
  return params.row.age > 30 ? 60 : 40;
};

const DynamicHeightDataGrid = () => {
  return (
    <div style={{ height: 400, width: '100%' }}>
      <DataGrid
        rows={rows}
        columns={columns}
        pageSize={5}
        getRowHeight={getRowHeight}
      />
    </div>
  );
};

4. 列可见性

4.1 列的隐藏与显示

可以通过在列定义中使用 hide 属性来控制列的可见性:

const columns = [
  { field: 'id', headerName: 'ID', width: 90 },
  { field: 'name', headerName: 'Name', width: 150 },
  { field: 'age', headerName: 'Age', width: 110, hide: true },
  { field: 'email', headerName: 'Email', width: 200 },
];

4.2 列菜单

列菜单允许用户快速隐藏或显示列。可以在 DataGrid 中启用这一功能:

const columns = [
  { field: 'id', headerName: 'ID', width: 90 },
  { field: 'name', headerName: 'Name', width: 150, hideable: true },
  { field: 'age', headerName: 'Age', width: 110, hideable: true },
  { field: 'email', headerName: 'Email', width: 200, hideable: true },
];

// DataGrid中自动启用列菜单

5. 自定义列

5.1 自定义列内容

可以通过 renderCell 方法自定义列的显示内容:

const columns = [
  { field: 'id', headerName: 'ID', width: 90 },
  { field: 'name', headerName: 'Name', width: 150 },
  {
    field: 'age',
    headerName: 'Age',
    width: 110,
    renderCell: (params) => (
      <strong>{params.value > 30 ? 'Adult' : 'Young'}</strong>
    ),
  },
];

6. 列头与样式

6.1 自定义列头

可以通过 renderHeader 方法自定义列头:

const columns = [
  { field: 'id', headerName: 'ID', width: 90 },
  { field: 'name', headerName: 'Name', width: 150 },
  {
    field: 'age',
    headerName: 'Age',
    width: 110,
    renderHeader: () => <strong>Age (Years)</strong>,
  },
];

6.2 列样式

可以通过 headerClassNamecellClassName 来设置列头和单元格的样式:

const columns = [
  { field: 'id', headerName: 'ID', width: 90, headerClassName: 'header-style' },
  { field: 'name', headerName: 'Name', width: 150, cellClassName: 'cell-style' },
];

// CSS样式
.header-style {
  background-color: lightgrey;
}

.cell-style {
  color: blue;
}

7. 列跨越与分组

7.1 列跨越

可以使用 colSpan 方法实现列跨越功能:

const columns = [
  { field: 'id', headerName: 'ID', width: 90 },
  { field: 'name', headerName: 'Name', width: 150 },
  {
    field: 'age',
    headerName: 'Age',
    width: 110,
    renderCell: (params) => (
      <div style={{ gridColumn: 'span 2' }}>{params.value}</div>
    ),
  },
];

7.2 列分组

可以通过 groupingColDef 属性实现列分组:

const columns = [
  {
    field: 'group',
    headerName: 'Group',
    width: 150,
    groupable: true,
    renderCell: (params) => <strong>{params.value}</strong>,
  },
];

8. 列排序与固定

8.1 列排序

Data Grid 支持列的排序,可以通过 sortable 属性启用:

const columns = [
  { field: 'id', headerName: 'ID', width: 90, sortable: true },
  { field: 'name', headerName: 'Name', width: 150, sortable: true },
  { field: 'age', headerName: 'Age', width: 110, sortable: true },
];

8.2 列固定

可以通过 pinned 属性实现列的固定功能:

const columns = [
  { field: 'id', headerName: 'ID', width: 90, pinned: 'left' },
  { field: 'name', headerName: 'Name', width: 150 },
];

9. 列排序与重新排序

9.1 列重新排序

可以启用列的拖拽排序功能:

const columns = [
  { field: 'id', headerName: 'ID', width: 90, sortable: true },
  { field: 'name', headerName: 'Name', width: 150, sortable: true },
  { field: 'age', headerName: 'Age', width: 110, sortable: true },
  { field: 'email', headerName: 'Email', width: 200, sortable: true },
];

// 启用拖拽排序
<DataGrid
  rows={rows}
  columns={columns}
  pageSize={5}
  columnBuffer={2}
  draggable
/>

10. 示例综合应用

import * as React from 'react';
import { DataGrid } from '@mui/x-data-grid';
import { Button } from '@mui/material';

const columns = [
  { field: 'id', headerName: 'ID', width: 90 },
  { field: 'name', headerName: 'Name', flex: 1, hideable: true },
  {
    field: 'age',
    headerName: 'Age',
    width: 110,
    renderCell: (params) => (
      <Button variant="contained" color="primary">
        {params.value}
      </Button>
    ),
  },
  { field: 'email', headerName: 'Email', width: 200 },
];

const rows = [
  { id: 1, name: 'John Doe', age: 35, email: 'john@example.com' },
  { id: 2, name: 'Jane Smith', age: 42, email: 'jane@example.com' },
  { id: 3, name: 'Mike Johnson', age: 28, email: 'mike@example.com' },
];

const ComprehensiveDataGrid = () => {
  return (
    <div style={{ height: 400, width: '100%' }}>
      <DataGrid
        rows={rows}
        columns={columns}
        pageSize={5}
        columnBuffer={2}
        autoHeight
      />
    </div>
  );
};

export default ComprehensiveDataGrid;

11. 结论

Material UI 的 Data Grid 提供了强大的列管理功能,支持灵活的列定义、自定义列内容、列的可见性、排序、跨越及固定等功能。通过合理运用这些功能,可以构建出用户友好的数据表格。

希望本博客能帮助你更好地理解和使用 MUI X Data Grid 的列管理特性。如果你有任何问题或想法,欢迎在评论区分享!

chat评论区
评论列表
menu