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

목록에서 고르고 체크박스로 켜고 끄기Pick from a list, toggle with a checkbox

값을 목록에서 고르게 하거나 체크박스로 켜고 끌 수 있어요. 목록 칸은 한 번 누르면 목록이 열리고 체크박스 칸은 한 번 누르면 바로 바뀌어요. 보이는 모양(배지)과 고칠 때 모양(목록)도 따로 정할 수 있어요.Let people pick a value from a list or toggle a checkbox. One click opens the list or toggles the checkbox, and what you see (a badge) and what you edit with (a list) are set separately.

입문Beginner
직접 써 보기Live preview목록에서 고르고 체크박스로 켜고 끄기Pick from a list, toggle with a 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 class="toolbar">
    <select aria-label="보이는 모양" id="look">
      <option value="badge" selected>배지</option>
      <option value="text">글자</option>
    </select>
    <select aria-label="한 번 누르면" id="click">
      <option value="single" selected>바로 반응 (기본)</option>
      <option value="double">안 함 (더블클릭·키)</option>
    </select>
  </div>
  <div id="grid" style="height: 330px"></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('look').addEventListener('change', (event) => actions.look(api, event.target.value));
document.getElementById('click').addEventListener('change', (event) => actions.click(api, event.target.value));
example.js
const tasks = [
  '로그인 화면 만들기',
  '결제 연동',
  '검색 속도 개선',
  '알림 설정',
  '관리자 권한',
  '배포 점검',
];
const statuses = ['todo', 'doing', 'review', 'done'];
const priorities = ['낮음', '보통', '높음'];
const rows = tasks.map((task, i) => ({
  id: i + 1,
  task,
  status: statuses[i % statuses.length],
  priority: priorities[(i * 2) % priorities.length],
  done: i % 4 === 3,
}));
let look = 'badge';
// 목록 편집: 컬럼을 kind: 'select'로 두고 editorOptions에 고를 값을 줘요. label은 화면에 보이는 이름이에요.
// 보이는 모양과 고치는 모양은 따로예요: cellRenderer 'badge'로 배지를 그리고, 편집할 때는 같은 목록이 열려요.
const statusOptions = [
  { value: 'todo', label: '할 일' },
  { value: 'doing', label: '진행 중' },
  { value: 'review', label: '검토' },
  { value: 'done', label: '완료' },
];
const columns = () => [
  { id: 'task', title: '작업', width: 170 },
  {
    id: 'status',
    title: '상태',
    kind: 'select',
    width: 120,
    editable: true,
    editorOptions: statusOptions,
    // 글자 모드에서는 값(todo) 대신 목록의 이름(할 일)을 보여 줘요.
    ...(look === 'text'
      ? {
        formatValue: (value) => statusOptions.find((o) => o.value === value)?.label ?? String(value),
      }
      : {}),
    ...(look === 'badge'
      ? {
        cellRenderer: 'badge',
        cellRendererParams: {
          map: {
            todo: { label: '할 일', tone: 'neutral' },
            doing: { label: '진행 중', tone: 'info' },
            review: { label: '검토', tone: 'warning' },
            done: { label: '완료', tone: 'success' },
          },
        },
      }
      : {}),
  },
  {
    id: 'priority',
    title: '우선순위',
    kind: 'select',
    width: 110,
    editable: true,
    editorOptions: priorities.map((value) => ({ value })),
  },
  {
    id: 'done',
    title: '완료',
    kind: 'boolean',
    width: 80,
    align: 'center',
    editable: true,
    cellRenderer: 'checkbox',
  },
];
export const options = {
  columns: columns(),
  rows,
  rowKey: 'id',
  // singleClickEdit(기본 true): 목록(select) 칸은 한 번 누르면 목록이 열리고, 체크박스 칸은 한 번 누르면 바로 켜지고 꺼져요.
  // false로 두면 다른 칸처럼 더블클릭이나 키(F2·Enter·Space)로 편집을 시작해요. 글자·숫자 칸은 이 옵션과 상관없이 더블클릭이에요.
  singleClickEdit: true,
};
export function setup(api, log) {
  const nameOf = (id, value) => id === 'status'
    ? (statusOptions.find((o) => o.value === value)?.label ?? String(value))
    : String(value);
  const title = (id) => api.getColumns().find((c) => c.id === id)?.title ?? id;
  log({
    ko: '"상태"나 "우선순위" 칸을 한 번 누르면 목록이 열리고, "완료" 칸은 한 번 누르면 바로 켜지고 꺼져요. "한 번 누르면"을 바꿔서 비교해 보세요.',
    en: 'Click a "상태" (status) or "우선순위" (priority) cell once to open the list; a "완료" (done) cell toggles with one click. Change "Click to edit" and compare.',
  });
  return api.on('editCommitted', (e) => log({
    ko: `${e.rowKey}번 줄 ${title(e.columnId)}: ${nameOf(e.columnId, e.oldValue)} → ${nameOf(e.columnId, e.newValue)}`,
    en: `Row ${e.rowKey} ${title(e.columnId)}: ${nameOf(e.columnId, e.oldValue)} → ${nameOf(e.columnId, e.newValue)}`,
  }));
}
export const actions = {
  // 런타임에 setOptions로 바꿀 수 있어요.
  click: (api, value) => api.setOptions({ singleClickEdit: value !== 'double' }),
  look: (api, value) => {
    look = value === 'text' ? 'text' : 'badge';
    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">
    <select aria-label="Look" id="look">
      <option value="badge" selected>Badge</option>
      <option value="text">Text</option>
    </select>
    <select aria-label="Single click" id="click">
      <option value="single" selected>React at once (default)</option>
      <option value="double">Off (double-click or keys)</option>
    </select>
  </div>
  <div id="grid" style="height: 330px"></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('look').addEventListener('change', (event) => actions.look(api, event.target.value));
document.getElementById('click').addEventListener('change', (event) => actions.click(api, event.target.value));
example.js
const tasks = [
  'Build login screen',
  'Payment integration',
  'Speed up search',
  'Notification settings',
  'Admin permissions',
  'Deployment check',
];
const statuses = ['todo', 'doing', 'review', 'done'];
const priorities = ['Low', 'Medium', 'High'];
const rows = tasks.map((task, i) => ({
  id: i + 1,
  task,
  status: statuses[i % statuses.length],
  priority: priorities[(i * 2) % priorities.length],
  done: i % 4 === 3,
}));
let look = 'badge';
// List editing: set the column to kind: 'select' and give the values to pick in editorOptions. label is the name shown on screen.
// What you see and what you edit with are separate: cellRenderer 'badge' draws a badge, and the same list opens when editing.
const statusOptions = [
  { value: 'todo', label: 'To do' },
  { value: 'doing', label: 'In progress' },
  { value: 'review', label: 'Review' },
  { value: 'done', label: 'Done' },
];
const columns = () => [
  { id: 'task', title: 'Task', width: 190 },
  {
    id: 'status',
    title: 'Status',
    kind: 'select',
    width: 130,
    editable: true,
    editorOptions: statusOptions,
    // In text mode the list name (To do) is shown instead of the value (todo).
    ...(look === 'text'
      ? {
        formatValue: (value) => statusOptions.find((o) => o.value === value)?.label ?? String(value),
      }
      : {}),
    ...(look === 'badge'
      ? {
        cellRenderer: 'badge',
        cellRendererParams: {
          map: {
            todo: { label: 'To do', tone: 'neutral' },
            doing: { label: 'In progress', tone: 'info' },
            review: { label: 'Review', tone: 'warning' },
            done: { label: 'Done', tone: 'success' },
          },
        },
      }
      : {}),
  },
  {
    id: 'priority',
    title: 'Priority',
    kind: 'select',
    width: 110,
    editable: true,
    editorOptions: priorities.map((value) => ({ value })),
  },
  {
    id: 'done',
    title: 'Done',
    kind: 'boolean',
    width: 80,
    align: 'center',
    editable: true,
    cellRenderer: 'checkbox',
  },
];
export const options = {
  columns: columns(),
  rows,
  rowKey: 'id',
  // singleClickEdit (default true): one click opens the list on a select cell, and one click toggles a checkbox cell right away.
  // With false, editing starts with a double-click or a key (F2, Enter, Space) like other cells. Text and number cells use a double-click regardless of this option.
  singleClickEdit: true,
};
export function setup(api, log) {
  const nameOf = (id, value) => id === 'status'
    ? (statusOptions.find((o) => o.value === value)?.label ?? String(value))
    : String(value);
  const title = (id) => api.getColumns().find((c) => c.id === id)?.title ?? id;
  log('Click a "Status" or "Priority" cell once to open the list; a "Done" cell toggles with one click. Change "Single click" and compare.');
  return api.on('editCommitted', (e) => log(`Row ${e.rowKey} ${title(e.columnId)}: ${nameOf(e.columnId, e.oldValue)} → ${nameOf(e.columnId, e.newValue)}`));
}
export const actions = {
  // Can be changed at runtime with setOptions.
  click: (api, value) => api.setOptions({ singleClickEdit: value !== 'double' }),
  look: (api, value) => {
    look = value === 'text' ? 'text' : 'badge';
    api.setColumns(columns());
  },
};