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

머리글 체크박스로 전체 바꾸기Change everything with a header checkbox

headerRenderer: 'checkbox'는 머리글에 체크박스를 넣어요. checked(불리언 또는 함수, 'indeterminate' 가능), onChange, canCheck, position을 headerRendererParams로 줘요. 앱 상태가 바뀌면 api.setHeaderRendererParams로 알려 다시 그려요.headerRenderer: 'checkbox' puts a checkbox in the header. checked (a boolean or a function, 'indeterminate' allowed), onChange, canCheck and position go in headerRendererParams. When your state changes, api.setHeaderRendererParams makes the grid redraw it.

활용Intermediate
직접 써 보기Live preview머리글 체크박스로 전체 바꾸기Change everything with a header checkbox

코드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 id="grid" style="height: 200px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
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: 'checkbox': checked는 그릴 때 평가해요(true, false, 'indeterminate'). 앱 상태가 바뀌면 setHeaderRendererParams로 알려요.
function allDone(api) {
  const done = api.getRows().filter((row) => row.done).length;
  return done === 0 ? false : done === api.getRowCount() ? true : 'indeterminate';
}
export function setup(api, log) {
  const params = {
    checked: () => allDone(api),
    onChange: (checked) => {
      api.updateRows(api.getRows().map((row) => ({ ...row, done: checked })));
      log({ ko: `전체 ${checked ? '완료' : '해제'}`, en: `All ${checked ? 'done' : 'cleared'}` });
      api.setHeaderRendererParams('done', params); // 다시 그려 상태(체크·일부)를 맞춰요
    },
    // 일부만 완료일 때 머리글 체크를 켜는 건 허용하지 않아요(예시): 먼저 모두 해제하거나 개별로 맞춰요.
    canCheck: () => true,
  };
  api.setColumns([
    { id: 'task', title: '작업', width: 170 },
    { id: 'owner', title: '담당', width: 100 },
    {
      id: 'done',
      title: '완료',
      kind: 'boolean',
      width: 110,
      headerRenderer: 'checkbox',
      headerRendererParams: params,
      editable: true,
    },
  ]);
  log({
    ko: '머리글 체크박스는 전체 상태를 보여요. 행의 완료 칸을 바꾸면 "일부" 모양이 돼요.',
    en: 'The header checkbox shows the overall state. Change a Done cell in a row to see the partial look.',
  });
  return api.on('editCommitted', () => api.setHeaderRendererParams('done', params));
}
export const options = { columns: [{ id: 'task' }], rows, rowKey: 'id' };

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 id="grid" style="height: 200px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
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: 'checkbox': checked is evaluated when drawing (true, false, 'indeterminate'). Report app state changes with setHeaderRendererParams.
function allDone(api) {
  const done = api.getRows().filter((row) => row.done).length;
  return done === 0 ? false : done === api.getRowCount() ? true : 'indeterminate';
}
export function setup(api, log) {
  const params = {
    checked: () => allDone(api),
    onChange: (checked) => {
      api.updateRows(api.getRows().map((row) => ({ ...row, done: checked })));
      log(`All ${checked ? 'done' : 'cleared'}`);
      api.setHeaderRendererParams('done', params); // Redraw to sync the state (checked / partial)
    },
    // Example: turning on the header check while only some rows are done could be refused: first clear all or set rows one by one.
    canCheck: () => true,
  };
  api.setColumns([
    { id: 'task', title: 'Task', width: 170 },
    { id: 'owner', title: 'Owner', width: 100 },
    {
      id: 'done',
      title: 'Done',
      kind: 'boolean',
      width: 110,
      headerRenderer: 'checkbox',
      headerRendererParams: params,
      editable: true,
    },
  ]);
  log('The header checkbox shows the overall state. Change a Done cell in a row to see the partial look.');
  return api.on('editCommitted', () => api.setHeaderRendererParams('done', params));
}
export const options = { columns: [{ id: 'task' }], rows, rowKey: 'id' };