Data Grid - 행 스패닝(Row spanning)
여러 행에 걸쳐 셀을 확장합니다.
본문
기본적으로 Data Grid의 각 셀은 한 행의 높이를 차지합니다. 행 스패닝 기능을 사용하면 한 열의 셀이 여러 행을 채울 수 있습니다.
활성화하려면 Data Grid에 rowSpanning prop을 전달하세요.
Data Grid가 같은 열에서 반복되는 값을 가진 연속된 셀을 자동으로 병합합니다. 아래 데모에서 확인할 수 있습니다 — 토글 버튼을 꺼서 실제 행을 확인해 보세요:
import * as React from 'react';
import Box from '@mui/material/Box';
import { DataGrid, GridColDef } from '@mui/x-data-grid';
import FormControlLabel from '@mui/material/FormControlLabel';
import Switch from '@mui/material/Switch';
export default function RowSpanning() {
const [enabled, setEnabled] = React.useState(true);
return (
<Box sx={{ width: '100%' }}>
<FormControlLabel
checked={enabled}
onChange={(event) => setEnabled((event.target as HTMLInputElement).checked)}
control={<Switch />}
label="Enable row spanning"
/>
<Box sx={{ height: 300 }}>
<DataGrid
rows={rows}
columns={columns}
density="compact"
showCellVerticalBorder
showColumnVerticalBorder
disableRowSelectionOnClick
rowSpanning={enabled}
hideFooter
sx={{
'& .MuiDataGrid-row:hover': {
backgroundColor: 'transparent',
},
'& .bold': {
fontWeight: 'bold',
},
}}
/>
</Box>
</Box>
);
}
const columns: GridColDef<(typeof rows)[number]>[] = [
{
field: 'code',
headerName: 'Item Code',
width: 85,
cellClassName: ({ row }) => (row.summaryRow ? 'bold' : ''),
},
{
field: 'description',
headerName: 'Description',
rowHeader: true,
width: 170,
},
{
field: 'quantity',
headerName: 'Quantity',
width: 80,
// Do not span the values
rowSpanValueGetter: () => null,
},
{
field: 'unitPrice',
headerName: 'Unit Price',
type: 'number',
valueFormatter: (value) => (value ? `$${value}.00` : ''),
},
{
field: 'totalPrice',
headerName: 'Total Price',
type: 'number',
valueGetter: (value, row) => value ?? row?.unitPrice,
valueFormatter: (value) => `$${value}.00`,
cellClassName: ({ row }) => (row.summaryRow ? 'bold' : ''),
},
];
const rows = [
{
id: 1,
code: 'A101',
description: 'Wireless Mouse',
quantity: 2,
unitPrice: 50,
totalPrice: 100,
},
{
id: 2,
code: 'A102',
description: 'Mechanical Keyboard',
quantity: 1,
unitPrice: 75,
},
{
id: 3,
code: 'A103',
description: 'USB Dock Station',
quantity: 1,
unitPrice: 400,
},
{
id: 4,
code: 'A104',
description: 'Laptop',
quantity: 1,
unitPrice: 1800,
totalPrice: 2050,
},
{
id: 5,
code: 'A104',
description: '- 16GB RAM Upgrade',
quantity: 1,
unitPrice: 100,
totalPrice: 2050,
},
{
id: 6,
code: 'A104',
description: '- 512GB SSD Upgrade',
quantity: 1,
unitPrice: 150,
totalPrice: 2050,
},
{
id: 7,
code: 'TOTAL',
totalPrice: 2625,
summaryRow: true,
},
];
:::info
이 데모에서 quantity 열은 colDef.rowSpanValueGetter prop을 사용해 의도적으로 행 스패닝 계산에서 제외했습니다.
자세한 내용은 행 스패닝 셀 커스터마이즈 섹션을 참고하세요. :::
:::warning
행 스패닝은 스패닝된 셀의 높이를 rowHeight 배수만큼 늘리는 방식으로 동작하므로, 가변적이거나 동적인 높이에서는 제대로 동작하지 않습니다.
:::
행 스패닝 셀 커스터마이즈
두 개의 prop을 사용해 행 스패닝이 동작하는 방식을 커스터마이즈할 수 있습니다:
colDef.rowSpanValueGetter: 행 스패닝에 사용할 값을 제어합니다colDef.valueGetter: 행 스패닝 로직과 셀 값을 모두 제어합니다
이를 통해 병합되지 않아야 할 반복 값이 있을 때 원치 않는 행 스패닝을 방지할 수 있습니다.
다음 예시에서는 rowSpanValueGetter를 사용해 같은 사람에게 속하지 않은 age 셀들이 병합되지 않도록 합니다.
import Box from '@mui/material/Box';
import { DataGrid, GridColDef } from '@mui/x-data-grid';
export default function RowSpanningCustom() {
return (
<Box sx={{ height: 350, width: '100%' }}>
<DataGrid
rows={rows}
columns={columns}
showCellVerticalBorder
showColumnVerticalBorder
disableRowSelectionOnClick
hideFooter
rowSpanning
sx={{
'& .MuiDataGrid-row:hover': {
backgroundColor: 'transparent',
},
}}
/>
</Box>
);
}
const columns: GridColDef[] = [
{
field: 'name',
headerName: 'Name',
rowHeader: true,
width: 200,
editable: true,
},
{
field: 'designation',
headerName: 'Designation',
width: 200,
editable: true,
},
{
field: 'department',
headerName: 'Department',
width: 150,
editable: true,
},
{
field: 'age',
headerName: 'Age',
type: 'number',
width: 100,
valueFormatter: (value) => {
return `${value} yo`;
},
rowSpanValueGetter: (value, row) => {
return row ? `${row.name}-${row.age}` : value;
},
},
];
const rows = [
{
id: 1,
name: 'Andrew Clark',
designation: 'React Engineer',
department: 'Engineering',
age: 25,
},
{
id: 2,
name: 'Andrew Clark',
designation: 'Technical Interviewer',
department: 'Human resource',
age: 25,
},
{
id: 3,
name: 'Cynthia Duke',
designation: 'Technical Team Lead',
department: 'Engineering',
age: 25,
},
{
id: 4,
name: 'Jordyn Black',
designation: 'React Engineer',
department: 'Engineering',
age: 31,
},
{
id: 5,
name: 'Rene Glass',
designation: 'Ops Lead',
department: 'Operations',
age: 31,
},
];
열 스패닝과 함께 사용하기
행 스패닝은 열 스패닝과 함께 사용해 여러 행과 열을 동시에 확장하는 셀을 만들 수 있습니다. 아래 데모에서 확인하세요:
import Box from '@mui/material/Box';
import { DataGrid, GridColDef } from '@mui/x-data-grid';
const rows = [
{
id: 0,
day: 'Monday',
time: '9:00 AM - 10:30 AM',
course: 'Advanced Mathematics',
instructor: 'Dr. Smith',
room: 'Room 101',
notes: 'Midterm exam',
},
{
id: 1,
day: 'Monday',
time: '10:30 AM - 12:00 PM',
course: 'Advanced Mathematics',
instructor: 'Dr. Smith',
room: 'Room 101',
notes: 'Midterm exam',
},
{
id: 2,
day: 'Tuesday',
time: '9:00 AM - 10:30 AM',
course: 'Advanced Mathematics',
instructor: 'Dr. Smith',
room: 'Room 101',
notes: 'Practical and lab work',
},
{
id: 3,
day: 'Tuesday',
time: '10:30 AM - 12:00 PM',
course: 'Introduction to Biology',
instructor: 'Dr. Johnson',
room: 'Room 107',
notes: 'Lab session',
},
{
id: 4,
day: 'Wednesday',
time: '9:00 AM - 10:30 AM',
course: 'Computer Science 101',
instructor: 'Dr. Lee',
room: 'Room 303',
notes: 'Class',
},
{
id: 5,
day: 'Wednesday',
time: '10:30 AM - 12:00 PM',
course: 'Computer Science 101',
instructor: 'Dr. Lee',
room: 'Room 303',
notes: 'Lab session',
},
{
id: 6,
day: 'Thursday',
time: '9:00 AM - 11:00 AM',
course: 'Physics II',
instructor: 'Dr. Carter',
room: 'Room 104',
notes: 'Project Discussion',
},
{
id: 7,
day: 'Thursday',
time: '11:00 AM - 12:30 PM',
course: 'Physics II',
instructor: 'Dr. Carter',
room: 'Room 104',
notes: 'Project Discussion',
},
{
id: 8,
day: 'Friday',
time: '9:00 AM - 11:00 AM',
course: 'Physics II',
instructor: 'Dr. Carter',
room: 'Room 104',
notes: 'Project Submission',
},
{
id: 9,
day: 'Friday',
time: '11:00 AM - 12:30 PM',
course: 'Literature & Composition',
instructor: 'Prof. Adams',
room: 'Lecture Hall 1',
notes: 'Reading Assignment',
},
];
const columns: GridColDef[] = [
{
field: 'day',
headerName: 'Day',
rowHeader: true,
},
{
field: 'time',
headerName: 'Time',
rowHeader: true,
minWidth: 160,
},
{
field: 'course',
headerName: 'Course',
minWidth: 140,
colSpan: 2,
valueGetter: (_, row) => `${row?.course} (${row?.instructor})`,
cellClassName: 'course-instructor--cell',
},
{
field: 'instructor',
headerName: 'Instructor',
minWidth: 140,
hideable: false,
},
{
field: 'room',
headerName: 'Room',
minWidth: 120,
},
{
field: 'notes',
headerName: 'Notes',
minWidth: 180,
},
];
export default function RowSpanningClassSchedule() {
return (
<Box style={{ display: 'flex', flexDirection: 'column', width: '100%' }}>
<DataGrid
columns={columns}
rows={rows}
rowSpanning
disableRowSelectionOnClick
hideFooter
showCellVerticalBorder
showColumnVerticalBorder
sx={{
'& .MuiDataGrid-row:hover': {
backgroundColor: 'transparent',
},
[`& .course-instructor--cell`]: {
textAlign: 'center',
fontWeight: 'bold',
},
}}
/>
</Box>
);
}
:::warning 행 스패닝은 정렬이나 필터링 같은 기능과 잘 동작하지만, 열 스패닝과 함께 사용할 때는 모든 것이 예상대로 동작하는지 반드시 확인하세요. :::
데모
아래 데모는 열 스패닝 문서의 달력을 행 스패닝 기능을 사용해 다시 만든 것입니다:
import Box from '@mui/material/Box';
import { DataGrid, GridColDef } from '@mui/x-data-grid';
const slotTimesLookup = {
0: '09:00 - 10:00',
1: '10:00 - 11:00',
2: '11:00 - 12:00',
3: '12:00 - 13:00',
4: '13:00 - 14:00',
5: '14:00 - 15:00',
6: '15:00 - 16:00',
7: '16:00 - 17:00',
};
const days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
type Subject =
'Maths' | 'English' | 'Lab' | 'Chemistry' | 'Physics' | 'Music' | 'Dance';
type Row = { id: number; time: string; slots: Array<Subject | ''> };
const rows: Array<Row> = [
{
id: 0,
time: slotTimesLookup[0],
slots: ['Maths', 'Chemistry', 'Physics', 'Music', 'Maths'],
},
{
id: 1,
time: slotTimesLookup[1],
slots: ['English', 'Chemistry', 'English', 'Music', 'Dance'],
},
{
id: 2,
time: slotTimesLookup[2],
slots: ['English', 'Chemistry', 'Maths', 'Chemistry', 'Dance'],
},
{
id: 3,
time: slotTimesLookup[3],
slots: ['Lab', 'Physics', 'Maths', 'Chemistry', 'Physics'],
},
{
id: 4,
time: slotTimesLookup[4],
slots: ['', '', '', '', ''],
},
{
id: 5,
time: slotTimesLookup[5],
slots: ['Lab', 'Maths', 'Chemistry', 'Chemistry', 'English'],
},
{
id: 6,
time: slotTimesLookup[6],
slots: ['Music', 'Lab', 'Chemistry', 'English', ''],
},
{
id: 7,
time: slotTimesLookup[7],
slots: ['Music', 'Dance', '', 'English', ''],
},
];
const slotColumnCommonFields: Partial<GridColDef> = {
sortable: false,
filterable: false,
pinnable: false,
hideable: false,
cellClassName: (params) => params.value,
};
const columns: GridColDef<Row>[] = [
{
field: 'time',
headerName: 'Time',
rowHeader: true,
width: 120,
},
{
field: '0',
headerName: days[0],
valueGetter: (value, row) => row?.slots[0],
...slotColumnCommonFields,
},
{
field: '1',
headerName: days[1],
valueGetter: (value, row) => row?.slots[1],
...slotColumnCommonFields,
},
{
field: '2',
headerName: days[2],
valueGetter: (value, row) => row?.slots[2],
...slotColumnCommonFields,
},
{
field: '3',
headerName: days[3],
valueGetter: (value, row) => row?.slots[3],
...slotColumnCommonFields,
},
{
field: '4',
headerName: days[4],
valueGetter: (value, row) => row?.slots[4],
...slotColumnCommonFields,
},
];
const rootStyles = {
display: 'flex',
flexDirection: 'column',
width: '100%',
'& .Maths': {
backgroundColor: 'rgba(157, 255, 118, 0.49)',
},
'& .English': {
backgroundColor: 'rgba(255, 255, 10, 0.49)',
},
'& .Lab': {
backgroundColor: 'rgba(150, 150, 150, 0.49)',
},
'& .Chemistry': {
backgroundColor: 'rgba(255, 150, 150, 0.49)',
},
'& .Physics': {
backgroundColor: 'rgba(10, 150, 255, 0.49)',
},
'& .Music': {
backgroundColor: 'rgba(224, 183, 60, 0.55)',
},
'& .Dance': {
backgroundColor: 'rgba(200, 150, 255, 0.49)',
},
};
export default function RowSpanningCalendar() {
return (
<Box sx={rootStyles}>
<DataGrid
columns={columns}
rows={rows}
rowSpanning
disableRowSelectionOnClick
hideFooter
showCellVerticalBorder
showColumnVerticalBorder
sx={{
'& .MuiDataGrid-row:hover': {
backgroundColor: 'transparent',
},
}}
/>
</Box>
);
}