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

머리글에 버튼 넣기Put a button in a header

컬럼의 headerRenderer: 'button'은 머리글 제목 옆에 버튼을 넣어요. label, onClick, position, disabled를 headerRendererParams로 줘요. 버튼을 눌러도 정렬·끌기·크기 조절·메뉴는 일어나지 않고, 정렬이 꺼진 컬럼은 머리글에 포커스를 둔 채 Enter·Space로도 눌러요.A column's headerRenderer: 'button' puts a button next to the header title. label, onClick, position and disabled go in headerRendererParams. Pressing it does not sort, drag, resize or open the menu, and on a column without sorting Enter and Space press it while the header is focused.

입문Beginner
직접 써 보기Live preview머리글에 버튼 넣기Put a button in a header

코드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="reset">되돌리기</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('reset').addEventListener('click', () => actions.reset(api));
example.js
const rows = [
  { id: 1, task: '로그인 화면 수정', owner: '김하늘', done: false },
  { id: 2, task: '결제 흐름 검토', owner: '이도윤', done: true },
  { id: 3, task: '새 아이콘 세트', owner: '박서연', done: false },
  { id: 4, task: '배포 점검', owner: '최지우', done: false },
];
// headerRenderer: 'button'은 머리글 제목 옆에 버튼을 넣어요. 누르면 정렬·끌기·메뉴는 일어나지 않고 onClick만 불려요.
// 정렬이 꺼진 컬럼은 머리글에 포커스를 두고(Tab 대신 focusHeader) Enter·Space로도 눌러요.
export function setup(api, log) {
  const params = {
    label: '모두 완료로',
    onClick: () => {
      api.updateRows(api.getRows().map((row) => ({ ...row, done: true })));
      log({ ko: '모든 작업을 완료로 바꿨어요.', en: 'All tasks are marked done.' });
      api.setHeaderRendererParams('done', params); // 속성을 다시 알려 머리글을 다시 그려요(disabled를 다시 평가)
    },
    // 이미 모두 완료이면 막아요. 값은 머리글을 그릴 때 평가돼요.
    disabled: () => api.getRows().every((row) => row.done),
  };
  api.setColumns([
    { id: 'task', title: '작업', width: 170, sortable: true },
    { id: 'owner', title: '담당', width: 100, sortable: true },
    {
      id: 'done',
      title: '완료',
      kind: 'boolean',
      width: 190,
      sortable: false,
      headerRenderer: 'button',
      headerRendererParams: params,
    },
  ]);
  log({
    ko: '"완료" 머리글의 버튼을 눌러 보세요. 한 번 누르면 버튼이 비활성이 돼요.',
    en: 'Press the button in the Done header. After one press it becomes disabled.',
  });
}
export const options = { columns: [{ id: 'task' }], rows, rowKey: 'id' };
export const actions = {
  reset: (api) => {
    api.updateRows(api.getRows().map((row) => ({ ...row, done: false })));
    api.updateColumn('done', {
      headerRendererParams: api.getColumns().find((c) => c.id === 'done')?.headerRendererParams,
    });
  },
};

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="reset">Reset</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('reset').addEventListener('click', () => actions.reset(api));
example.js
const rows = [
  { id: 1, task: 'Fix login screen', owner: 'Liam Carter', done: false },
  { id: 2, task: 'Review payment flow', owner: 'Ethan Clark', done: true },
  { id: 3, task: 'New icon set', owner: 'Emma Stone', done: false },
  { id: 4, task: 'Deployment check', owner: 'Olivia Reed', done: false },
];
// headerRenderer: 'button' puts a button next to the header title. Pressing it triggers no sort, drag or menu; only onClick is called.
// For a column with sorting off, put focus on the header (focusHeader instead of Tab) and press it with Enter or Space too.
export function setup(api, log) {
  const params = {
    label: 'Mark all done',
    onClick: () => {
      api.updateRows(api.getRows().map((row) => ({ ...row, done: true })));
      log('All tasks are marked done.');
      api.setHeaderRendererParams('done', params); // Report the params again so the header is redrawn (disabled is evaluated again)
    },
    // Disabled once everything is done. The value is evaluated when the header is drawn.
    disabled: () => api.getRows().every((row) => row.done),
  };
  api.setColumns([
    { id: 'task', title: 'Task', width: 170, sortable: true },
    { id: 'owner', title: 'Owner', width: 100, sortable: true },
    {
      id: 'done',
      title: 'Done',
      kind: 'boolean',
      width: 190,
      sortable: false,
      headerRenderer: 'button',
      headerRendererParams: params,
    },
  ]);
  log('Press the button in the Done header. After one press it becomes disabled.');
}
export const options = { columns: [{ id: 'task' }], rows, rowKey: 'id' };
export const actions = {
  reset: (api) => {
    api.updateRows(api.getRows().map((row) => ({ ...row, done: false })));
    api.updateColumn('done', {
      headerRendererParams: api.getColumns().find((c) => c.id === 'done')?.headerRendererParams,
    });
  },
};