咨询:MUI X Pro是否支持锁定列、嵌套表头与分组行功能组合使用?
MUI X Pro:锁定列、嵌套表头与分组行的组合使用
这三个功能完全可以组合使用,MUI X Pro的DataGrid组件对这类复杂表格场景提供了原生支持,不少开发者已经在实际项目中落地了类似需求。
核心实现思路
- 嵌套表头:通过给列配置添加
children属性,构建层级化的表头结构 - 锁定列:给需要固定的列设置
fixed: true(固定左侧)或fixed: 'right'(固定右侧)属性即可 - 分组行:开启
rowGrouping功能,可通过rowGroupingColumnMode指定分组方式,或直接在目标列配置里设置enableRowGrouping: true
简单示例代码
import { DataGridPro } from '@mui/x-data-grid-pro'; const columns = [ { field: 'id', headerName: 'ID', width: 90, fixed: true }, // 锁定左侧列 { field: 'company', headerName: '企业信息', children: [ // 嵌套表头结构 { field: 'name', headerName: '企业名称', width: 150 }, { field: 'industry', headerName: '所属行业', width: 120, enableRowGrouping: true }, // 开启该列分组 ], }, { field: 'financial', headerName: '财务数据', children: [ { field: 'revenue', headerName: '营收', width: 130 }, { field: 'profit', headerName: '利润', width: 130 }, ], }, ]; const rows = [ { id: 1, name: 'A公司', industry: '互联网', revenue: '2亿', profit: '3000万' }, { id: 2, name: 'B公司', industry: '互联网', revenue: '1.5亿', profit: '2000万' }, { id: 3, name: 'C公司', industry: '制造业', revenue: '5亿', profit: '8000万' }, ]; export default function CombinedGrid() { return ( <div style={{ height: 400, width: '100%' }}> <DataGridPro rows={rows} columns={columns} rowGroupingColumnMode="multiple" initialState={{ rowGrouping: { model: ['industry'] }, // 默认按行业分组 }} /> </div> ); }
你可以基于这个示例调整列结构、锁定位置和分组规则,适配自身业务需求。组合使用时三项功能不存在兼容性冲突,若遇到细节问题,可参考MUI X官方文档中各功能的单独说明进行调试。
内容的提问来源于stack exchange,提问作者jackjiang2
相关产品推荐
相关产品推荐

