본문으로 건너뛰기Skip to content
예제 첫화면Examples home

긴 헤더 제목을 두 줄로 나누기Break a long header title into two lines

제목이 길면 컬럼이 넓어지는 대신 줄바꿈으로 두 줄로 나눠 보여요. 제목에 줄바꿈을 넣고 헤더 높이만 키우면 돼요.Instead of widening a column for a long title, split it into two lines. Add a line break to the title and make the header taller.

입문Beginner
직접 써 보기Live preview긴 헤더 제목을 두 줄로 나누기Break a long header title into two lines

코드Code

탭을 골라 코드를 복사하세요. 위 화면은 이 코드를 그대로 실행한 결과예요.Pick a tab and copy the code. The preview above runs exactly this code.

이 화면은 새 모양 옵션을 켠 모양이에요: density: 'spacious', iconStyle: 'soft', motion: true (코드에 없으면 기본 모양으로 보여요)This preview has the new-look options on: density: 'spacious', iconStyle: 'soft', motion: true (without them in your code you get the default look)

빌드 도구(Vite 등)를 쓰는 프로젝트용 모듈 파일이에요. 설치 없이 바로 돌려 보려면 HTML 탭이나 직접 해보기를 쓰세요.

버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.

index.html
<div id="app">
  <div class="toolbar">
    <select aria-label="제목 줄 수" id="lines">
      <option value="two" selected>두 줄</option>
      <option value="one">한 줄</option>
    </select>
  </div>
  <div id="grid" style="height: 280px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options } from './example';
const api = createGrid(document.getElementById('grid'), options);
document.getElementById('lines').addEventListener('change', (event) => actions.lines(api, event.target.value));
example.js
const products = [
  '무선 이어폰',
  '스마트 워치',
  '노트북 거치대',
  '보조배터리',
  '블루투스 스피커',
  '웹캠',
];
// 제목에 줄바꿈(\n)을 넣으면 헤더가 두 줄로 나뉘어요. 헤더 높이를 두 줄이 들어갈 만큼 키워 주세요.
// 같은 컬럼의 두 가지 제목을 상수로 둬요. 어느 쪽을 적용해도 결과는 항상 같아요.
const TITLES = {
  two: { stock: '현재\n재고(개)', incoming: '입고\n예정(개)', price: '판매가\n(부가세 포함)' },
  one: { stock: '현재 재고(개)', incoming: '입고 예정(개)', price: '판매가(부가세 포함)' },
};
export const options = {
  columns: [
    { id: 'product', title: '상품명', width: 150 },
    { id: 'stock', title: TITLES.two.stock, kind: 'number', width: 100, align: 'end' },
    { id: 'incoming', title: TITLES.two.incoming, kind: 'number', width: 100, align: 'end' },
    {
      id: 'price',
      title: TITLES.two.price,
      kind: 'number',
      width: 130,
      align: 'end',
      formatValue: (value) => `${Number(value).toLocaleString('ko-KR')}원`,
    },
  ],
  rows: products.map((product, i) => ({
    id: i + 1,
    product,
    stock: 12 + ((i * 37) % 90),
    incoming: (i * 25) % 120,
    price: 19900 + i * 12000,
  })),
  rowKey: 'id',
  headerHeight: 56,
};
export const actions = {
  // 같은 컬럼 제목을 한 줄/두 줄로 바꿔요. 두 줄일 때만 헤더를 높여요.
  lines: (api, value) => {
    const titles = value === 'one' ? TITLES.one : TITLES.two;
    api.setColumns(api
      .getColumns()
      .map((c) => ({ ...c, title: titles[c.id] ?? c.title })));
    api.setOptions({ headerHeight: value === 'one' ? 36 : 56 });
  },
};

Module files for a project with a bundler such as Vite. To run it with no setup, use the HTML tab or Try it.

When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.

index.html
<div id="app">
  <div class="toolbar">
    <select aria-label="Title lines" id="lines">
      <option value="two" selected>Two lines</option>
      <option value="one">One line</option>
    </select>
  </div>
  <div id="grid" style="height: 280px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options } from './example';
const api = createGrid(document.getElementById('grid'), options);
document.getElementById('lines').addEventListener('change', (event) => actions.lines(api, event.target.value));
example.js
const products = [
  'Wireless earbuds',
  'Smartwatch',
  'Laptop stand',
  'Power bank',
  'Bluetooth speaker',
  'Webcam',
];
// A line break (\n) in the title splits the header into two lines. Make the header tall enough for both lines.
// The two titles of the same columns are kept as constants. Whichever one you apply, the result is always the same.
const TITLES = {
  two: { stock: 'In stock\n(units)', incoming: 'Incoming\n(units)', price: 'Price\n(incl. tax)' },
  one: { stock: 'In stock (units)', incoming: 'Incoming (units)', price: 'Price (incl. tax)' },
};
export const options = {
  columns: [
    { id: 'product', title: 'Product', width: 170 },
    { id: 'stock', title: TITLES.two.stock, kind: 'number', width: 100, align: 'end' },
    { id: 'incoming', title: TITLES.two.incoming, kind: 'number', width: 100, align: 'end' },
    {
      id: 'price',
      title: TITLES.two.price,
      kind: 'number',
      width: 130,
      align: 'end',
      formatValue: (value) => `$${Number(value).toLocaleString('en-US')}`,
    },
  ],
  rows: products.map((product, i) => ({
    id: i + 1,
    product,
    stock: 12 + ((i * 37) % 90),
    incoming: (i * 25) % 120,
    price: 20 + i * 12,
  })),
  rowKey: 'id',
  headerHeight: 56,
};
export const actions = {
  // Switch the same column titles between one and two lines. The header is made taller only for two lines.
  lines: (api, value) => {
    const titles = value === 'one' ? TITLES.one : TITLES.two;
    api.setColumns(api
      .getColumns()
      .map((c) => ({ ...c, title: titles[c.id] ?? c.title })));
    api.setOptions({ headerHeight: value === 'one' ? 36 : 56 });
  },
};