머리글 제목 고치기와 상태 저장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.
코드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)
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<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>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));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.',
});
},
};<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>머리글 제목 고치기와 상태 저장</title>
</head>
<body>
<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>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
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()로 처음 제목으로 돌아와요.
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 = () => { };
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}"`,
}));
}
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.',
});
},
};
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
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));
</script>
</body>
</html>버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<button
type="button"
disabled={!api}
onClick={() => api && actions.focusHeader(api)}
>담당 머리글 포커스</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.save(api)}
>상태 저장</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.reset(api)}
>처음으로</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.restore(api)}
>상태 복원</button>
</div>
<Grid style={{ height: 200 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
task: string;
owner: string;
hours: number;
}
const en = document.documentElement.dataset.lang === 'en';
const rows: Row[] = 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: GridOptions<Row> = {
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: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => {
api.focusHeader('owner');
say({
ko: '"담당" 머리글에 포커스를 뒀어요. F2를 누르면 제목 입력칸이 열려요.',
en: 'Focused the Owner header. Press F2 to open the title input.',
});
},
save: (api: GridApi<Row>) => {
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: GridApi<Row>) => {
api.resetColumnState();
say({ ko: '처음 제목으로 되돌렸어요.', en: 'Restored the initial titles.' });
},
restore: (api: GridApi<Row>) => {
if (saved === '')
return say({ ko: '먼저 "상태 저장"을 눌러 주세요.', en: 'Press "Save state" first.' });
api.setColumnState(JSON.parse(saved));
say({
ko: '저장한 상태(제목 포함)를 복원했어요.',
en: 'Restored the saved state, titles included.',
});
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<button
type="button"
:disabled="!api"
@click="api && actions.focusHeader(api)"
>담당 머리글 포커스</button>
<button type="button" :disabled="!api" @click="api && actions.save(api)">상태 저장</button>
<button type="button" :disabled="!api" @click="api && actions.reset(api)">처음으로</button>
<button type="button" :disabled="!api" @click="api && actions.restore(api)">상태 복원</button>
</div>
<Grid style="height: 200px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
task: string;
owner: string;
hours: number;
}
const en = document.documentElement.dataset.lang === 'en';
const rows: Row[] = 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: GridOptions<Row> = {
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: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => {
api.focusHeader('owner');
say({
ko: '"담당" 머리글에 포커스를 뒀어요. F2를 누르면 제목 입력칸이 열려요.',
en: 'Focused the Owner header. Press F2 to open the title input.',
});
},
save: (api: GridApi<Row>) => {
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: GridApi<Row>) => {
api.resetColumnState();
say({ ko: '처음 제목으로 되돌렸어요.', en: 'Restored the initial titles.' });
},
restore: (api: GridApi<Row>) => {
if (saved === '')
return say({ ko: '먼저 "상태 저장"을 눌러 주세요.', en: 'Press "Save state" first.' });
api.setColumnState(JSON.parse(saved));
say({
ko: '저장한 상태(제목 포함)를 복원했어요.',
en: 'Restored the saved state, titles included.',
});
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<button
type="button"
[disabled]="!api"
(click)="api && actions.focusHeader(api)"
>담당 머리글 포커스</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.save(api)"
>상태 저장</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.reset(api)"
>처음으로</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.restore(api)"
>상태 복원</button>
</div>
<mx-grid style="height: 200px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
task: string;
owner: string;
hours: number;
}
const en = document.documentElement.dataset.lang === 'en';
const rows: Row[] = 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: GridOptions<Row> = {
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: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => {
api.focusHeader('owner');
say({
ko: '"담당" 머리글에 포커스를 뒀어요. F2를 누르면 제목 입력칸이 열려요.',
en: 'Focused the Owner header. Press F2 to open the title input.',
});
},
save: (api: GridApi<Row>) => {
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: GridApi<Row>) => {
api.resetColumnState();
say({ ko: '처음 제목으로 되돌렸어요.', en: 'Restored the initial titles.' });
},
restore: (api: GridApi<Row>) => {
if (saved === '')
return say({ ko: '먼저 "상태 저장"을 눌러 주세요.', en: 'Press "Save state" first.' });
api.setColumnState(JSON.parse(saved));
say({
ko: '저장한 상태(제목 포함)를 복원했어요.',
en: 'Restored the saved state, titles included.',
});
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<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>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));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.');
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Edit header titles and save them in the column state</title>
</head>
<body>
<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>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
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().
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 = () => { };
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}"`));
}
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.');
},
};
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
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));
</script>
</body>
</html>When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<button
type="button"
disabled={!api}
onClick={() => api && actions.focusHeader(api)}
>Focus Owner header</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.save(api)}
>Save state</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.reset(api)}
>Reset</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.restore(api)}
>Restore state</button>
</div>
<Grid style={{ height: 200 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
task: string;
owner: string;
hours: number;
}
const rows: Row[] = [
{ 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: GridOptions<Row> = {
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: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => {
api.focusHeader('owner');
say('Focused the Owner header. Press F2 to open the title input.');
},
save: (api: GridApi<Row>) => {
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: GridApi<Row>) => {
api.resetColumnState();
say('Restored the initial titles.');
},
restore: (api: GridApi<Row>) => {
if (saved === '') return say('Press "Save state" first.');
api.setColumnState(JSON.parse(saved));
say('Restored the saved state, titles included.');
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<button
type="button"
:disabled="!api"
@click="api && actions.focusHeader(api)"
>Focus Owner header</button>
<button type="button" :disabled="!api" @click="api && actions.save(api)">Save state</button>
<button type="button" :disabled="!api" @click="api && actions.reset(api)">Reset</button>
<button type="button" :disabled="!api" @click="api && actions.restore(api)">Restore state</button>
</div>
<Grid style="height: 200px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
task: string;
owner: string;
hours: number;
}
const rows: Row[] = [
{ 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: GridOptions<Row> = {
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: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => {
api.focusHeader('owner');
say('Focused the Owner header. Press F2 to open the title input.');
},
save: (api: GridApi<Row>) => {
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: GridApi<Row>) => {
api.resetColumnState();
say('Restored the initial titles.');
},
restore: (api: GridApi<Row>) => {
if (saved === '') return say('Press "Save state" first.');
api.setColumnState(JSON.parse(saved));
say('Restored the saved state, titles included.');
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<button
type="button"
[disabled]="!api"
(click)="api && actions.focusHeader(api)"
>Focus Owner header</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.save(api)"
>Save state</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.reset(api)"
>Reset</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.restore(api)"
>Restore state</button>
</div>
<mx-grid style="height: 200px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
task: string;
owner: string;
hours: number;
}
const rows: Row[] = [
{ 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: GridOptions<Row> = {
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: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => {
api.focusHeader('owner');
say('Focused the Owner header. Press F2 to open the title input.');
},
save: (api: GridApi<Row>) => {
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: GridApi<Row>) => {
api.resetColumnState();
say('Restored the initial titles.');
},
restore: (api: GridApi<Row>) => {
if (saved === '') return say('Press "Save state" first.');
api.setColumnState(JSON.parse(saved));
say('Restored the saved state, titles included.');
},
};