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

기본값이나 계산한 값으로 되돌리기Reset to defaults or computed values

cascade의 reset은 'clear'(비우기), 'default'(아래 컬럼의 defaultValue), 또는 (아래 컬럼 id, 새 행)을 받는 함수예요. 함수는 부모가 바뀐 새 행을 보고 컬럼마다 다른 값을 돌려줄 수 있어요. 함수가 던지면 그 칸만 건너뛰고 error 이벤트로 알려요.The reset of cascade is 'clear', 'default' (the child's defaultValue) or a function taking the child column id and the new row. The function sees the changed row and can return a different value per column. If it throws, only that cell is skipped and an error event is reported.

활용Intermediate
직접 써 보기Live preview기본값이나 계산한 값으로 되돌리기Reset to defaults or computed values

코드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, plan: 'basic', seats: 5, support: '이메일' },
  { id: 2, plan: 'team', seats: 20, support: '채팅' },
  { id: 3, plan: 'basic', seats: 3, support: '이메일' },
];
const SEATS = { basic: 5, team: 20, business: 100 };
const SUPPORT = { basic: '이메일', team: '채팅', business: '전담 담당자' };
// reset: 'default'는 아래 컬럼의 defaultValue로, 함수는 (아래 컬럼 id, 새 행)에 대한 결과로 되돌려요.
// 여기서는 요금제를 바꾸면 좌석 수는 defaultValue 함수로, 지원 방식은 reset 함수로 정해요.
export const options = {
  columns: [
    {
      id: 'plan',
      title: '요금제',
      width: 130,
      editable: true,
      cellEditor: 'select',
      editorOptions: [{ value: 'basic' }, { value: 'team' }, { value: 'business' }],
      cascade: {
        children: ['seats', 'support'],
        reset: (childId, row) => childId === 'support' ? SUPPORT[row.plan] : SEATS[row.plan],
      },
    },
    { id: 'seats', title: '좌석 수', kind: 'number', width: 110, editable: true },
    { id: 'support', title: '지원 방식', width: 150, editable: true },
    {
      id: 'note',
      title: '기본값 시연(default)',
      width: 200,
      getValue: (row) => `기본 좌석 ${SEATS[row.plan] ?? '-'}`,
    },
  ],
  rows,
  rowKey: 'id',
};
export function setup(api, log) {
  log({
    ko: '"요금제"를 바꾸면 좌석 수와 지원 방식이 그 요금제의 기본값으로 바뀌어요. 같은 요금제를 고르면 그대로예요.',
    en: 'Changing "요금제" (plan) resets seats and support to the plan defaults. Choosing the same plan changes nothing.',
  });
  const offs = [
    api.on('editCommitted', (e) => log({
      ko: `행 ${e.rowKey}: ${e.columnId} = ${String(e.newValue)}`,
      en: `Row ${e.rowKey}: ${e.columnId} = ${String(e.newValue)}`,
    })),
  ];
  return () => offs.forEach((off) => off());
}

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, plan: 'basic', seats: 5, support: 'Email' },
  { id: 2, plan: 'team', seats: 20, support: 'Chat' },
  { id: 3, plan: 'basic', seats: 3, support: 'Email' },
];
const SEATS = { basic: 5, team: 20, business: 100 };
const SUPPORT = {
  basic: 'Email',
  team: 'Chat',
  business: 'Dedicated manager',
};
// reset: 'default' restores the child column's defaultValue; a function restores the result for (child column id, new row).
// Here, when the plan changes, the seat count is decided by the defaultValue function and the support type by the reset function.
export const options = {
  columns: [
    {
      id: 'plan',
      title: 'Plan',
      width: 130,
      editable: true,
      cellEditor: 'select',
      editorOptions: [{ value: 'basic' }, { value: 'team' }, { value: 'business' }],
      cascade: {
        children: ['seats', 'support'],
        reset: (childId, row) => childId === 'support' ? SUPPORT[row.plan] : SEATS[row.plan],
      },
    },
    { id: 'seats', title: 'Seats', kind: 'number', width: 110, editable: true },
    { id: 'support', title: 'Support', width: 150, editable: true },
    {
      id: 'note',
      title: 'Default demo (default)',
      width: 200,
      getValue: (row) => `Default seats ${SEATS[row.plan] ?? '-'}`,
    },
  ],
  rows,
  rowKey: 'id',
};
export function setup(api, log) {
  log('Changing "Plan" resets seats and support to the plan defaults. Choosing the same plan changes nothing.');
  const offs = [
    api.on('editCommitted', (e) => log(`Row ${e.rowKey}: ${e.columnId} = ${String(e.newValue)}`)),
  ];
  return () => offs.forEach((off) => off());
}