부모를 바꾸면 아래 컬럼 비우기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.
코드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)
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<div id="app">
<div id="grid" style="height: 200px"></div>
</div>
<script type="module" src="./main.js"></script>import { createGrid } from 'maxgrid';
import { options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);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());
}<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>부모를 바꾸면 아래 컬럼 비우기</title>
</head>
<body>
<div id="grid" style="height: 200px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
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 함수가 행의 나라로 정해요.
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',
};
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());
}
options.ariaLabel = "부모를 바꾸면 아래 컬럼 비우기";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
</script>
</body>
</html>버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<Grid style={{ height: 200 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
country: string;
city: string | null;
zip: string | null;
}
const CITIES: Record<string, { value: string }[]> = {
KR: [{ value: '서울' }, { value: '부산' }, { value: '대전' }],
JP: [{ value: '도쿄' }, { value: '오사카' }],
US: [{ value: '뉴욕' }, { value: '시애틀' }],
};
const rows: Row[] = [
{ 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: GridOptions<Row> = {
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 as Row).country] ?? [],
},
{ id: 'zip', title: '우편번호(읽기 전용)', width: 170, editable: false },
],
rows,
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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());
}버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<Grid style="height: 200px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
country: string;
city: string | null;
zip: string | null;
}
const CITIES: Record<string, { value: string }[]> = {
KR: [{ value: '서울' }, { value: '부산' }, { value: '대전' }],
JP: [{ value: '도쿄' }, { value: '오사카' }],
US: [{ value: '뉴욕' }, { value: '시애틀' }],
};
const rows: Row[] = [
{ 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: GridOptions<Row> = {
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 as Row).country] ?? [],
},
{ id: 'zip', title: '우편번호(읽기 전용)', width: 170, editable: false },
],
rows,
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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());
}버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<mx-grid style="height: 200px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
country: string;
city: string | null;
zip: string | null;
}
const CITIES: Record<string, { value: string }[]> = {
KR: [{ value: '서울' }, { value: '부산' }, { value: '대전' }],
JP: [{ value: '도쿄' }, { value: '오사카' }],
US: [{ value: '뉴욕' }, { value: '시애틀' }],
};
const rows: Row[] = [
{ 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: GridOptions<Row> = {
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 as Row).country] ?? [],
},
{ id: 'zip', title: '우편번호(읽기 전용)', width: 170, editable: false },
],
rows,
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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());
}When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<div id="app">
<div id="grid" style="height: 200px"></div>
</div>
<script type="module" src="./main.js"></script>import { createGrid } from 'maxgrid';
import { options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);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());
}<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Clear child columns when the parent changes</title>
</head>
<body>
<div id="grid" style="height: 200px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
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.
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',
};
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());
}
options.ariaLabel = "Clear child columns when the parent changes";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
</script>
</body>
</html>When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<Grid style={{ height: 200 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
country: string;
city: string | null;
zip: string | null;
}
const CITIES: Record<string, { value: string }[]> = {
KR: [{ value: 'Seoul' }, { value: 'Busan' }, { value: 'Daejeon' }],
JP: [{ value: 'Tokyo' }, { value: 'Osaka' }],
US: [{ value: 'New York' }, { value: 'Seattle' }],
};
const rows: Row[] = [
{ 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: GridOptions<Row> = {
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 as Row).country] ?? [],
},
{ id: 'zip', title: 'Zip code (read-only)', width: 170, editable: false },
],
rows,
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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());
}When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<Grid style="height: 200px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
country: string;
city: string | null;
zip: string | null;
}
const CITIES: Record<string, { value: string }[]> = {
KR: [{ value: 'Seoul' }, { value: 'Busan' }, { value: 'Daejeon' }],
JP: [{ value: 'Tokyo' }, { value: 'Osaka' }],
US: [{ value: 'New York' }, { value: 'Seattle' }],
};
const rows: Row[] = [
{ 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: GridOptions<Row> = {
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 as Row).country] ?? [],
},
{ id: 'zip', title: 'Zip code (read-only)', width: 170, editable: false },
],
rows,
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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());
}When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<mx-grid style="height: 200px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
country: string;
city: string | null;
zip: string | null;
}
const CITIES: Record<string, { value: string }[]> = {
KR: [{ value: 'Seoul' }, { value: 'Busan' }, { value: 'Daejeon' }],
JP: [{ value: 'Tokyo' }, { value: 'Osaka' }],
US: [{ value: 'New York' }, { value: 'Seattle' }],
};
const rows: Row[] = [
{ 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: GridOptions<Row> = {
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 as Row).country] ?? [],
},
{ id: 'zip', title: 'Zip code (read-only)', width: 170, editable: false },
],
rows,
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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());
}