본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Data Grid - 열 헤더

원문 보기 위키 갱신

열 헤더를 커스터마이즈할 수 있어요.

출처: MUI X documentation

본문

헤더는 다음과 같이 구성할 수 있어요:

  • headerName: 열 헤더 셀에 렌더링되는 열의 제목.
  • description: 열 헤더 이름이 완전히 표시되지 않을 때 툴팁으로 렌더링되는 열의 설명.
import { DataGrid } from '@mui/x-data-grid';

const rows = [
  {
    id: 1,
    username: '@MUI',
    age: 20,
  },
];

export default function HeaderColumnsGrid() {
  return (
    <div style={{ height: 250, width: '100%' }}>
      <DataGrid
        columns={[
          {
            field: 'username',
            headerName: 'Username',
            rowHeader: true,
            description:
              'The identification used by the person with access to the online service.',
          },
          { field: 'age', headerName: 'Age' },
        ]}
        rows={rows}
      />
    </div>
  );
}

커스텀 헤더 렌더러

renderHeader 메서드로 각 헤더의 모양을 커스터마이즈할 수 있어요. headerName 속성보다 우선합니다.

const columns: GridColDef[] = [
  {
    field: 'date',
    width: 150,
    type: 'date',
    renderHeader: (params: GridColumnHeaderParams) => (
      <strong>
        {'Birthday '}
        <span role="img" aria-label="enjoy">
          🎂
        </span>
      </strong>
    ),
  },
];
import { DataGrid, GridColDef } from '@mui/x-data-grid';

const columns: GridColDef[] = [
  {
    field: 'date',
    rowHeader: true,
    width: 150,
    type: 'date',
    renderHeader: () => (
      <strong>
        {'Birthday '}
        <span role="img" aria-label="enjoy">
          🎂
        </span>
      </strong>
    ),
  },
];

const rows = [
  {
    id: 1,
    date: new Date(1979, 0, 1),
  },
  {
    id: 2,
    date: new Date(1984, 1, 1),
  },
  {
    id: 3,
    date: new Date(1992, 2, 1),
  },
];

export default function RenderHeaderGrid() {
  return (
    <div style={{ height: 300, width: '100%' }}>
      <DataGrid rows={rows} columns={columns} />
    </div>
  );
}

헤더 높이

기본적으로 열 헤더의 높이는 56픽셀이에요. 이는 Material Design 가이드라인의 높이와 일치해요.

columnHeaderHeight prop으로 기본값을 덮어쓸 수 있어요.

import { DataGrid, GridColDef } from '@mui/x-data-grid';

const rows = [
  {
    id: 1,
    username: '@MUI',
    age: 20,
  },
];

const columns: GridColDef[] = [
  {
    field: 'username',
    headerName: 'Username',
    rowHeader: true,
    description:
      'The identification used by the person with access to the online service.',
  },
  { field: 'age', headerName: 'Age' },
];

export default function HeaderHeight() {
  return (
    <div style={{ height: 250, width: '100%' }}>
      <DataGrid columns={columns} rows={rows} columnHeaderHeight={36} />
    </div>
  );
}

헤더 스타일링

자세한 내용은 헤더 스타일링 섹션을 참고하세요.

API

더 알아보기 (Learn more)