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

부모를 바꾸면 아래 컬럼 비우기Clear child columns when the parent changes

컬럼의 cascade는 그 컬럼의 값이 실제로 바뀐 편집 뒤에 같은 행의 아래 컬럼(children)을 되돌려요. 기본은 비우기이고 편집할 수 없는 칸도 비워져요. 부모와 아래 칸은 되돌리기 한 단계이고, 붙여넣기와 지우기 키에서도 같아요.A column's cascade resets the child columns of the same row after an edit that actually changes it. The default clears them, and non-editable cells are cleared too. The parent and children are one undo step, also for paste and the clear key.

활용Intermediate
직접 써 보기Live preview부모를 바꾸면 아래 컬럼 비우기Clear child columns when the parent changes

코드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 CITIES = {
  KR: [{ value: '서울' }, { value: '부산' }, { value: '대전' }],
  JP: [{ value: '도쿄' }, { value: '오사카' }],
  US: [{ value: '뉴욕' }, { value: '시애틀' }],
};
const rows = [
  { id: 1, country: 'KR', city: '서울', zip: '04524' },
  { id: 2, country: 'JP', city: '도쿄', zip: '100-0001' },
  { id: 3, country: 'US', city: '시애틀', zip: '98101' },
];
// cascade: 부모 컬럼의 값이 바뀌면 아래 컬럼(children)을 같은 변경·되돌리기 한 단계로 비워요(reset 기본 'clear').
// 편집할 수 없는 '우편번호'도 비워져요. 도시 목록은 editorOptions 함수가 행의 나라로 정해요.
export const options = {
  columns: [
    {
      id: 'country',
      title: '나라',
      width: 120,
      editable: true,
      cellEditor: 'select',
      editorOptions: [{ value: 'KR' }, { value: 'JP' }, { value: 'US' }],
      cascade: { children: ['city', 'zip'] },
    },
    {
      id: 'city',
      title: '도시',
      width: 150,
      editable: true,
      cellEditor: 'select',
      editorOptions: ({ row }) => CITIES[row.country] ?? [],
    },
    { id: 'zip', title: '우편번호(읽기 전용)', width: 170, editable: false },
  ],
  rows,
  rowKey: 'id',
};
export function setup(api, log) {
  log({
    ko: '"나라"를 바꾸면 도시와 우편번호가 비워져요. Ctrl+Z 한 번이면 셋이 함께 돌아와요.',
    en: 'Changing "나라" (country) clears the city and the zip code. One Ctrl+Z restores all three.',
  });
  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 CITIES = {
  KR: [{ value: 'Seoul' }, { value: 'Busan' }, { value: 'Daejeon' }],
  JP: [{ value: 'Tokyo' }, { value: 'Osaka' }],
  US: [{ value: 'New York' }, { value: 'Seattle' }],
};
const rows = [
  { id: 1, country: 'KR', city: 'Seoul', zip: '04524' },
  { id: 2, country: 'JP', city: 'Tokyo', zip: '100-0001' },
  { id: 3, country: 'US', city: 'Seattle', zip: '98101' },
];
// cascade: when the parent column value changes, the child columns are cleared in the same change and the same undo step (reset defaults to 'clear').
// The non-editable 'Zip code' is cleared too. The editorOptions function decides the city list from the row's country.
export const options = {
  columns: [
    {
      id: 'country',
      title: 'Country',
      width: 120,
      editable: true,
      cellEditor: 'select',
      editorOptions: [{ value: 'KR' }, { value: 'JP' }, { value: 'US' }],
      cascade: { children: ['city', 'zip'] },
    },
    {
      id: 'city',
      title: 'City',
      width: 150,
      editable: true,
      cellEditor: 'select',
      editorOptions: ({ row }) => CITIES[row.country] ?? [],
    },
    { id: 'zip', title: 'Zip code (read-only)', width: 170, editable: false },
  ],
  rows,
  rowKey: 'id',
};
export function setup(api, log) {
  log('Changing "Country" clears the city and the zip code. One Ctrl+Z restores all three.');
  const offs = [
    api.on('editCommitted', (e) => log(`Row ${e.rowKey}: ${e.columnId} = ${String(e.newValue)}`)),
  ];
  return () => offs.forEach((off) => off());
}