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

스위치로 켜고 끄기Turn things on and off with a switch

cellRenderer 'switch'는 셀 안에 누르는 스위치를 그려요. 눌러서 바꾼 값은 일반 편집처럼 검증, 되돌리기, 변경 추적을 거쳐요. 편집할 수 없는 행은 비활성이고, canToggle로 바꾸지 못하게 막을 수도 있어요.cellRenderer 'switch' draws a switch you can press in the cell. The value you change goes through validation, undo and change tracking like any edit. Rows that are not editable are disabled, and canToggle can refuse a change.

입문Beginner
직접 써 보기Live preview스위치로 켜고 끄기Turn things on and off with a switch

코드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="undo">되돌리기</button>
  </div>
  <div id="grid" style="height: 250px"></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('undo').addEventListener('click', () => actions.undo(api));
example.js
const rows = [
  { id: 1, name: '알림 메일', active: true, required: false, locked: false },
  { id: 2, name: '보안 로그', active: true, required: true, locked: false },
  { id: 3, name: '정기 백업', active: false, required: false, locked: false },
  { id: 4, name: '관리자 계정', active: true, required: false, locked: true },
];
// switch 렌더러는 값을 그대로(true/false) 쓰고, 누르면 일반 편집 확정 경로로 값이 바뀌어요.
// editable이 행 조건을 받으면 잠긴 행은 비활성으로 그려져요. canToggle이 false를 돌려주면 값은 그대로예요.
export const options = {
  columns: [
    { id: 'name', title: '기능', width: 150 },
    {
      id: 'active',
      title: '사용',
      kind: 'boolean',
      width: 110,
      editable: (row) => !row.locked,
      cellRenderer: 'switch',
      cellRendererParams: {
        label: '사용',
        canToggle: ({ row, next }) => !(row.required && next === false),
      },
    },
    { id: 'required', title: '필수', kind: 'boolean', width: 80 },
  ],
  rows,
  rowKey: 'id',
};
let say = () => undefined;
export function setup(api, log) {
  say = log;
  const off = api.on('editCommitted', (e) => log({
    ko: `행 ${e.rowKey}: ${e.columnId} = ${String(e.newValue)}`,
    en: `Row ${e.rowKey}: ${e.columnId} = ${String(e.newValue)}`,
  }));
  return off;
}
export const actions = {
  undo: (api) => {
    if (!api.canUndo())
      say({ ko: '되돌릴 것이 없어요.', en: 'Nothing to undo.' });
    else
      api.undo();
  },
};

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="undo">Undo</button>
  </div>
  <div id="grid" style="height: 250px"></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('undo').addEventListener('click', () => actions.undo(api));
example.js
const rows = [
  { id: 1, name: 'Notification mail', active: true, required: false, locked: false },
  { id: 2, name: 'Security log', active: true, required: true, locked: false },
  { id: 3, name: 'Scheduled backup', active: false, required: false, locked: false },
  { id: 4, name: 'Admin account', active: true, required: false, locked: true },
];
// The switch renderer uses the value as is (true/false), and pressing it changes the value through the normal edit-commit path.
// When editable takes a row condition, locked rows are drawn disabled. If canToggle returns false, the value stays.
export const options = {
  columns: [
    { id: 'name', title: 'Feature', width: 170 },
    {
      id: 'active',
      title: 'Enabled',
      kind: 'boolean',
      width: 110,
      editable: (row) => !row.locked,
      cellRenderer: 'switch',
      cellRendererParams: {
        label: 'Enabled',
        canToggle: ({ row, next }) => !(row.required && next === false),
      },
    },
    { id: 'required', title: 'Required', kind: 'boolean', width: 90 },
  ],
  rows,
  rowKey: 'id',
};
let say = () => undefined;
export function setup(api, log) {
  say = log;
  const off = api.on('editCommitted', (e) => log(`Row ${e.rowKey}: ${e.columnId} = ${String(e.newValue)}`));
  return off;
}
export const actions = {
  undo: (api) => {
    if (!api.canUndo())
      say('Nothing to undo.');
    else
      api.undo();
  },
};