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

머리글 제목 고치기와 상태 저장Edit header titles and save them in the column state

titleEditable로 머리글 제목을 두 번 눌러(또는 F2) 고치면, 바뀐 제목이 getColumnState()의 title에 실려요. setColumnState로 복원하고 resetColumnState로 처음 제목으로 돌아와요.With titleEditable you can edit a header title by double-clicking it (or F2). The changed title goes into the title of getColumnState(); setColumnState restores it and resetColumnState brings back the initial titles.

활용Intermediate
직접 써 보기Live preview머리글 제목 고치기와 상태 저장Edit header titles and save them in the column state

코드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">
    <button type="button" id="focusHeader">담당 머리글 포커스</button>
    <button type="button" id="save">상태 저장</button>
    <button type="button" id="reset">처음으로</button>
    <button type="button" id="restore">상태 복원</button>
  </div>
  <div id="grid" style="height: 200px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('focusHeader').addEventListener('click', () => actions.focusHeader(api));
document.getElementById('save').addEventListener('click', () => actions.save(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));
document.getElementById('restore').addEventListener('click', () => actions.restore(api));
example.js
const en = document.documentElement.dataset.lang === 'en';
const rows = en
  ? [
    { id: 1, task: 'Fix login screen', owner: 'Hana Kim', hours: 6 },
    { id: 2, task: 'Review checkout flow', owner: 'Dan Lee', hours: 3 },
    { id: 3, task: 'New icon set', owner: 'Sue Park', hours: 8 },
  ]
  : [
    { id: 1, task: '로그인 화면 수정', owner: '김하늘', hours: 6 },
    { id: 2, task: '결제 흐름 검토', owner: '이도윤', hours: 3 },
    { id: 3, task: '새 아이콘 세트', owner: '박서연', hours: 8 },
  ];
// titleEditable: true — 머리글 렌더러 없이도 제목을 두 번 누르거나(포커스한 머리글에서는 F2) 고칠 수 있어요.
// 바꾼 제목은 getColumnState()의 title에 실리고, setColumnState()로 복원하고 resetColumnState()로 처음 제목으로 돌아와요.
export const options = {
  columns: [
    { id: 'task', title: en ? 'Task' : '작업', width: 180, titleEditable: true },
    { id: 'owner', title: en ? 'Owner' : '담당', width: 110, titleEditable: true },
    { id: 'hours', title: en ? 'Hours' : '시간', kind: 'number', width: 90, align: 'end' },
  ],
  rows,
  rowKey: 'id',
  ariaLabel: en ? 'Saved header titles' : '머리글 제목 상태 저장',
};
let saved = '';
let say = () => { };
export function setup(api, log) {
  say = log;
  saved = '';
  log({
    ko: '"작업"이나 "담당" 제목을 두 번 눌러 고친 뒤 "상태 저장" → "처음으로" → "상태 복원"을 눌러 보세요.',
    en: 'Rename the Task or Owner title by double-clicking it, then press "Save state", "Reset" and "Restore state".',
  });
  return api.on('headerRenamed', (e) => log({
    ko: `headerRenamed: ${e.columnId} → "${e.title}"`,
    en: `headerRenamed: ${e.columnId} → "${e.title}"`,
  }));
}
export const actions = {
  focusHeader: (api) => {
    api.focusHeader('owner');
    say({
      ko: '"담당" 머리글에 포커스를 뒀어요. F2를 누르면 제목 입력칸이 열려요.',
      en: 'Focused the Owner header. Press F2 to open the title input.',
    });
  },
  save: (api) => {
    const state = api.getColumnState();
    saved = JSON.stringify(state);
    const titles = state.filter((s) => s.title !== undefined).map((s) => `${s.id}="${s.title}"`);
    say({
      ko: `저장했어요. 바뀐 제목만 title에 실려요: ${titles.join(', ') || '없음'}`,
      en: `Saved. Only changed titles carry a title: ${titles.join(', ') || 'none'}`,
    });
  },
  reset: (api) => {
    api.resetColumnState();
    say({ ko: '처음 제목으로 되돌렸어요.', en: 'Restored the initial titles.' });
  },
  restore: (api) => {
    if (saved === '')
      return say({ ko: '먼저 "상태 저장"을 눌러 주세요.', en: 'Press "Save state" first.' });
    api.setColumnState(JSON.parse(saved));
    say({
      ko: '저장한 상태(제목 포함)를 복원했어요.',
      en: 'Restored the saved state, titles included.',
    });
  },
};

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">
    <button type="button" id="focusHeader">Focus Owner header</button>
    <button type="button" id="save">Save state</button>
    <button type="button" id="reset">Reset</button>
    <button type="button" id="restore">Restore state</button>
  </div>
  <div id="grid" style="height: 200px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('focusHeader').addEventListener('click', () => actions.focusHeader(api));
document.getElementById('save').addEventListener('click', () => actions.save(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));
document.getElementById('restore').addEventListener('click', () => actions.restore(api));
example.js
const rows = [
  { id: 1, task: 'Fix login screen', owner: 'Liam Carter', hours: 6 },
  { id: 2, task: 'Review checkout flow', owner: 'Ethan Clark', hours: 3 },
  { id: 3, task: 'New icon set', owner: 'Emma Stone', hours: 8 },
];
// titleEditable: true: even without a header renderer you can edit the title by double-clicking it (or F2 on a focused header).
// A changed title is carried in the title of getColumnState(), restored with setColumnState() and reset to the initial title with resetColumnState().
export const options = {
  columns: [
    { id: 'task', title: 'Task', width: 180, titleEditable: true },
    { id: 'owner', title: 'Owner', width: 110, titleEditable: true },
    { id: 'hours', title: 'Hours', kind: 'number', width: 90, align: 'end' },
  ],
  rows,
  rowKey: 'id',
  ariaLabel: 'Saved header titles',
};
let saved = '';
let say = () => { };
export function setup(api, log) {
  say = log;
  saved = '';
  log('Rename the Task or Owner title by double-clicking it, then press "Save state", "Reset" and "Restore state".');
  return api.on('headerRenamed', (e) => log(`headerRenamed: ${e.columnId} → "${e.title}"`));
}
export const actions = {
  focusHeader: (api) => {
    api.focusHeader('owner');
    say('Focused the Owner header. Press F2 to open the title input.');
  },
  save: (api) => {
    const state = api.getColumnState();
    saved = JSON.stringify(state);
    const titles = state.filter((s) => s.title !== undefined).map((s) => `${s.id}="${s.title}"`);
    say(`Saved. Only changed titles carry a title: ${titles.join(', ') || 'none'}`);
  },
  reset: (api) => {
    api.resetColumnState();
    say('Restored the initial titles.');
  },
  restore: (api) => {
    if (saved === '')
      return say('Press "Save state" first.');
    api.setColumnState(JSON.parse(saved));
    say('Restored the saved state, titles included.');
  },
};