기본값이나 계산한 값으로 되돌리기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.
코드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 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());
}<!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 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 함수로 정해요.
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',
};
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());
}
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;
plan: string;
seats: number | null;
support: string | null;
}
const rows: Row[] = [
{ id: 1, plan: 'basic', seats: 5, support: '이메일' },
{ id: 2, plan: 'team', seats: 20, support: '채팅' },
{ id: 3, plan: 'basic', seats: 3, support: '이메일' },
];
const SEATS: Record<string, number> = { basic: 5, team: 20, business: 100 };
const SUPPORT: Record<string, string> = { basic: '이메일', team: '채팅', business: '전담 담당자' };
// reset: 'default'는 아래 컬럼의 defaultValue로, 함수는 (아래 컬럼 id, 새 행)에 대한 결과로 되돌려요.
// 여기서는 요금제를 바꾸면 좌석 수는 defaultValue 함수로, 지원 방식은 reset 함수로 정해요.
export const options: GridOptions<Row> = {
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 as Row).plan] : SEATS[(row as 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 as Row).plan] ?? '-'}`,
},
],
rows,
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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());
}버튼을 누르면 이 예제의 코드가 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;
plan: string;
seats: number | null;
support: string | null;
}
const rows: Row[] = [
{ id: 1, plan: 'basic', seats: 5, support: '이메일' },
{ id: 2, plan: 'team', seats: 20, support: '채팅' },
{ id: 3, plan: 'basic', seats: 3, support: '이메일' },
];
const SEATS: Record<string, number> = { basic: 5, team: 20, business: 100 };
const SUPPORT: Record<string, string> = { basic: '이메일', team: '채팅', business: '전담 담당자' };
// reset: 'default'는 아래 컬럼의 defaultValue로, 함수는 (아래 컬럼 id, 새 행)에 대한 결과로 되돌려요.
// 여기서는 요금제를 바꾸면 좌석 수는 defaultValue 함수로, 지원 방식은 reset 함수로 정해요.
export const options: GridOptions<Row> = {
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 as Row).plan] : SEATS[(row as 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 as Row).plan] ?? '-'}`,
},
],
rows,
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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());
}버튼을 누르면 이 예제의 코드가 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;
plan: string;
seats: number | null;
support: string | null;
}
const rows: Row[] = [
{ id: 1, plan: 'basic', seats: 5, support: '이메일' },
{ id: 2, plan: 'team', seats: 20, support: '채팅' },
{ id: 3, plan: 'basic', seats: 3, support: '이메일' },
];
const SEATS: Record<string, number> = { basic: 5, team: 20, business: 100 };
const SUPPORT: Record<string, string> = { basic: '이메일', team: '채팅', business: '전담 담당자' };
// reset: 'default'는 아래 컬럼의 defaultValue로, 함수는 (아래 컬럼 id, 새 행)에 대한 결과로 되돌려요.
// 여기서는 요금제를 바꾸면 좌석 수는 defaultValue 함수로, 지원 방식은 reset 함수로 정해요.
export const options: GridOptions<Row> = {
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 as Row).plan] : SEATS[(row as 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 as Row).plan] ?? '-'}`,
},
],
rows,
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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());
}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 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());
}<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Reset to defaults or computed values</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 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.
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',
};
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());
}
options.ariaLabel = "Reset to defaults or computed values";
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;
plan: string;
seats: number | null;
support: string | null;
}
const rows: Row[] = [
{ 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: Record<string, number> = { basic: 5, team: 20, business: 100 };
const SUPPORT: Record<string, string> = {
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: GridOptions<Row> = {
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 as Row).plan] : SEATS[(row as 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 as Row).plan] ?? '-'}`,
},
],
rows,
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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());
}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;
plan: string;
seats: number | null;
support: string | null;
}
const rows: Row[] = [
{ 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: Record<string, number> = { basic: 5, team: 20, business: 100 };
const SUPPORT: Record<string, string> = {
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: GridOptions<Row> = {
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 as Row).plan] : SEATS[(row as 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 as Row).plan] ?? '-'}`,
},
],
rows,
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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());
}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;
plan: string;
seats: number | null;
support: string | null;
}
const rows: Row[] = [
{ 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: Record<string, number> = { basic: 5, team: 20, business: 100 };
const SUPPORT: Record<string, string> = {
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: GridOptions<Row> = {
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 as Row).plan] : SEATS[(row as 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 as Row).plan] ?? '-'}`,
},
],
rows,
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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());
}