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

헤더 글자를 실행 중에 바꾸기Change header text at runtime

헤더 글자는 컬럼 정의의 제목이라서, 그 컬럼의 제목만 바꿔 다시 넣으면 돼요. 폭·순서·고정은 그대로 남아요. 월이 바뀔 때 '3월 지급액'처럼 제목을 갈아 끼울 때 쓸 수 있어요.A header's text is the column's title, so put the list in again with only that title changed; width, order and pin stay. Handy when a title such as March payout ($) must change over time.

입문Beginner
직접 써 보기Live preview헤더 글자를 실행 중에 바꾸기Change header text at runtime

코드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">
    <input
      type="text"
      aria-label="금액 제목"
      placeholder="금액 제목 (예: 8월 지급액)"
      id="title"
    />
    <button type="button" id="month">'3월 지급액'으로</button>
    <button type="button" id="reset">처음 제목으로</button>
  </div>
  <div id="grid" style="height: 170px"></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);
document.getElementById('title').addEventListener('input', (event) => actions.title(api, event.target.value));
document.getElementById('month').addEventListener('click', () => actions.month(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));
example.js
const DEFAULT_TITLE = '금액(원)';
let title = DEFAULT_TITLE;
const columns = () => [
  { id: 'id', title: '번호', width: 80, align: 'end' },
  { id: 'name', title: '이름', width: 130 },
  { id: 'team', title: '팀', width: 130 },
  {
    id: 'amount',
    title,
    kind: 'number',
    width: 150,
    align: 'end',
    formatValue: (value) => Number(value).toLocaleString('ko-KR'),
  },
];
export const options = {
  columns: columns(),
  rows: [
    { id: 1, name: '김민준', team: '개발팀', amount: 4200000 },
    { id: 2, name: '이서연', team: '디자인팀', amount: 1850000 },
    { id: 3, name: '박지호', team: '영업팀', amount: 960000 },
  ],
  rowKey: 'id',
};
export function setup(_api) {
  title = DEFAULT_TITLE;
  return () => { };
}
// 헤더 제목은 컬럼 정의의 title이에요. 그 컬럼만 바꾼 정의로 setColumns를 부르면 폭·순서·고정은 그대로 남아요.
// 입력칸을 비우면 처음 제목으로 돌아가요.
export const actions = {
  title: (api, value) => {
    title = (value ?? '').trim() || DEFAULT_TITLE;
    api.setColumns(columns());
  },
  month: (api) => {
    title = '3월 지급액(원)';
    api.setColumns(columns());
  },
  reset: (api) => {
    title = DEFAULT_TITLE;
    api.setColumns(columns());
  },
};

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">
    <input
      type="text"
      aria-label="Amount title"
      placeholder="Amount title (e.g. August payout)"
      id="title"
    />
    <button type="button" id="month">Set "March payout ($)"</button>
    <button type="button" id="reset">Reset title</button>
  </div>
  <div id="grid" style="height: 170px"></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);
document.getElementById('title').addEventListener('input', (event) => actions.title(api, event.target.value));
document.getElementById('month').addEventListener('click', () => actions.month(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));
example.js
const DEFAULT_TITLE = 'Amount ($)';
let title = DEFAULT_TITLE;
const columns = () => [
  { id: 'id', title: 'No.', width: 80, align: 'end' },
  { id: 'name', title: 'Name', width: 130 },
  { id: 'team', title: 'Team', width: 130 },
  {
    id: 'amount',
    title,
    kind: 'number',
    width: 170,
    align: 'end',
    formatValue: (value) => Number(value).toLocaleString('en-US'),
  },
];
export const options = {
  columns: columns(),
  rows: [
    { id: 1, name: 'Liam Carter', team: 'Dev', amount: 4200 },
    { id: 2, name: 'Emma Stone', team: 'Design', amount: 1850 },
    { id: 3, name: 'Noah Parker', team: 'Sales', amount: 960 },
  ],
  rowKey: 'id',
};
export function setup(_api) {
  title = DEFAULT_TITLE;
  return () => { };
}
// The header title is the title in the column definition. Calling setColumns with a definition that changes only that column keeps the width, order and pinning.
// Emptying the input goes back to the initial title.
export const actions = {
  title: (api, value) => {
    title = (value ?? '').trim() || DEFAULT_TITLE;
    api.setColumns(columns());
  },
  month: (api) => {
    title = 'March payout ($)';
    api.setColumns(columns());
  },
  reset: (api) => {
    title = DEFAULT_TITLE;
    api.setColumns(columns());
  },
};