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

헤더에 마우스를 올리면 컬럼 설명이 나와요Show a column description on header hover

컬럼이 무슨 뜻인지 헤더에 마우스를 올리면 말풍선으로 알려 줘요. 터치 화면에서는 헤더를 누르면 떠요. 컬럼 묶음(그룹 헤더)에도 달 수 있고, 설명이 있는 곳만 제목 뒤에 ⓘ가 붙어요.Hover a header for a balloon explaining the column; on touch screens, tap it. Column groups can have one too, and only headers with a description carry an info mark.

활용Intermediate
직접 써 보기Live preview헤더에 마우스를 올리면 컬럼 설명이 나와요Show a column description on header hover

코드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="mode">
      <option value="on" selected>보이기</option>
      <option value="off">끄기</option>
    </select>
  </div>
  <div id="grid" style="height: 300px"></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('mode').addEventListener('change', (event) => actions.mode(api, event.target.value));
example.js
const regions = ['서울', '경기·인천', '강원', '충북', '부산', '제주'];
// 설명 글은 한곳에 모아 둬요. 줄바꿈은 \n이고, 길어도 여러 줄로 나뉘어 보여요.
const DESCRIPTIONS = {
  region: '영업 담당 권역이에요.\n경기와 인천은 한 권역으로 묶어요.',
  q1: '1월~3월 합계(만 원)\n반품은 이미 뺀 금액이에요.',
  rate: '(2분기 매출 ÷ 목표) × 100\n100% 이상이면 목표 달성이에요.\n' +
    '목표는 연초에 영업팀과 합의한 금액이고, 분기마다 조정될 수 있어요.',
  results: '분기별 매출 실적 묶음\n단위는 모두 만 원이에요.',
};
let tipOn = true;
// headerTooltip에 설명을 적으면 헤더에 마우스를 올릴 때(터치에서는 헤더를 누를 때) 말풍선으로 보여요.
// 설명이 있는 컬럼은 제목 뒤에 ⓘ를 붙여서 "여기 설명이 있어요"를 알려 줘요.
const tipOf = (id) => (tipOn ? DESCRIPTIONS[id] : undefined);
const withMark = (id, title) => tipOn && DESCRIPTIONS[id] ? `${title} ⓘ` : title;
// 컬럼 묶음(그룹 헤더)에도 설명을 달 수 있어요.
const groups = () => [
  {
    id: 'results',
    title: withMark('results', '분기 실적'),
    children: ['q1', 'q2'],
    headerTooltip: tipOf('results'),
  },
];
export const options = {
  columns: [
    { id: 'region', title: withMark('region', '지역'), headerTooltip: tipOf('region'), width: 120 },
    {
      id: 'q1',
      title: withMark('q1', '1분기'),
      headerTooltip: tipOf('q1'),
      kind: 'number',
      width: 110,
      align: 'end',
    },
    { id: 'q2', title: '2분기', kind: 'number', width: 110, align: 'end' },
    {
      id: 'rate',
      title: withMark('rate', '달성률'),
      headerTooltip: tipOf('rate'),
      kind: 'number',
      width: 110,
      align: 'end',
      formatValue: (value) => `${value}%`,
    },
    { id: 'note', title: '비고', width: 160 },
  ],
  headerGroups: groups(),
  rows: regions.map((region, i) => ({
    id: i + 1,
    region,
    q1: 5200 + ((i * 1370) % 4000),
    q2: 5800 + ((i * 910) % 4500),
    rate: 82 + ((i * 11) % 30),
    note: i % 2 === 0 ? '신규 거래처 증가' : '',
  })),
  rowKey: 'id',
};
export const actions = {
  // 설명을 켜고 끄고, ⓘ 표시도 함께 바꿔요(컬럼 정의만 새로 넘기면 폭·정렬 상태는 그대로예요).
  mode: (api, value) => {
    tipOn = value !== 'off';
    api.setColumns(api.getColumns().map((c) => ({
      ...c,
      title: withMark(c.id, (c.title ?? c.id).replace(' ⓘ', '')),
      headerTooltip: tipOf(c.id),
    })));
    api.setOptions({ headerGroups: groups() });
  },
};

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="Descriptions" id="mode">
      <option value="on" selected>Show</option>
      <option value="off">Off</option>
    </select>
  </div>
  <div id="grid" style="height: 300px"></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('mode').addEventListener('change', (event) => actions.mode(api, event.target.value));
example.js
const regions = ['Northeast', 'Mid-Atlantic', 'Midwest', 'South', 'Mountain', 'Pacific'];
// Keep the description texts in one place. A line break is \n, and long text is shown over several lines.
const DESCRIPTIONS = {
  region: 'The sales territory.\nNew England is grouped with the Northeast.',
  q1: 'January to March total ($)\nReturns are already deducted.',
  rate: '(Q2 sales ÷ target) × 100\n100% or more means the target is met.\n' +
    'The target is the amount agreed with the sales team at the start of the year and may be adjusted each quarter.',
  results: 'Sales results by quarter\nAll amounts are in US dollars.',
};
let tipOn = true;
// Write a description in headerTooltip and it shows as a bubble when the mouse is over the header (on touch, when the header is pressed).
// A column with a description gets an ⓘ after its title to say "there is a description here".
const tipOf = (id) => (tipOn ? DESCRIPTIONS[id] : undefined);
const withMark = (id, title) => tipOn && DESCRIPTIONS[id] ? `${title} ⓘ` : title;
// A column group (group header) can have a description too.
const groups = () => [
  {
    id: 'results',
    title: withMark('results', 'Quarterly results'),
    children: ['q1', 'q2'],
    headerTooltip: tipOf('results'),
  },
];
export const options = {
  columns: [
    {
      id: 'region',
      title: withMark('region', 'Region'),
      headerTooltip: tipOf('region'),
      width: 120,
    },
    {
      id: 'q1',
      title: withMark('q1', 'Q1'),
      headerTooltip: tipOf('q1'),
      kind: 'number',
      width: 110,
      align: 'end',
    },
    { id: 'q2', title: 'Q2', kind: 'number', width: 110, align: 'end' },
    {
      id: 'rate',
      title: withMark('rate', 'Rate'),
      headerTooltip: tipOf('rate'),
      kind: 'number',
      width: 110,
      align: 'end',
      formatValue: (value) => `${value}%`,
    },
    { id: 'note', title: 'Note', width: 160 },
  ],
  headerGroups: groups(),
  rows: regions.map((region, i) => ({
    id: i + 1,
    region,
    q1: (5200 + ((i * 1370) % 4000)) * 10,
    q2: (5800 + ((i * 910) % 4500)) * 10,
    rate: 82 + ((i * 11) % 30),
    note: i % 2 === 0 ? 'New accounts growing' : '',
  })),
  rowKey: 'id',
};
export const actions = {
  // Turn the descriptions on and off, and change the ⓘ mark with them (passing only new column definitions keeps the width and align state).
  mode: (api, value) => {
    tipOn = value !== 'off';
    api.setColumns(api.getColumns().map((c) => ({
      ...c,
      title: withMark(c.id, (c.title ?? c.id).replace(' ⓘ', '')),
      headerTooltip: tipOf(c.id),
    })));
    api.setOptions({ headerGroups: groups() });
  },
};