그룹을 접고 펼 때 컬럼 보이기Show columns when a group is open or closed
showWhen으로 헤더 그룹을 펼쳤을 때만(groupOpen), 접었을 때만(groupClosed) 보이는 컬럼을 정해요. 급여 그룹을 접으면 세부 컬럼이 사라지고 합계 하나만 남아요.showWhen makes a column visible only while its header group is open (groupOpen) or closed (groupClosed). Closing the Pay group hides the detail columns and leaves one total.
코드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 class="toolbar">
<button type="button" id="close">급여 접기</button>
<button type="button" id="open">급여 펼치기</button>
</div>
<div id="grid" style="height: 240px"></div>
</div>
<script type="module" src="./main.js"></script>import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('close').addEventListener('click', () => actions.close(api));
document.getElementById('open').addEventListener('click', () => actions.open(api));const ROWS = [
{ id: 1001, name: '김민준', team: '개발팀', city: '서울', salary: 6200, bonus: 620 },
{ id: 1002, name: '이서연', team: '디자인팀', city: '부산', salary: 4800, bonus: 480 },
{ id: 1003, name: '박지호', team: '영업팀', city: '대구', salary: 7100, bonus: 710 },
{ id: 1004, name: '최유나', team: '인사팀', city: '서울', salary: 3900, bonus: 390 },
];
let say = () => { };
export function setup(_api, log) {
say = log;
return () => { };
}
const COLUMNS = [
{ id: 'name', title: '이름', width: 120 },
{ id: 'team', title: '팀', width: 110 },
{
id: 'salary',
title: '기본급',
kind: 'number',
width: 110,
align: 'end',
showWhen: 'groupOpen',
},
{ id: 'bonus', title: '보너스', kind: 'number', width: 110, align: 'end', showWhen: 'groupOpen' },
{
id: 'total',
title: '합계',
kind: 'number',
width: 110,
align: 'end',
showWhen: 'groupClosed',
getValue: (row) => row.salary + row.bonus,
},
];
export const options = {
columns: COLUMNS,
rows: ROWS,
rowKey: 'id',
headerGroups: [
{ id: 'pay', title: '급여', collapsible: true, children: ['salary', 'bonus', 'total'] },
],
};
export const actions = {
close: (api) => {
api.setHeaderGroupCollapsed('pay', true);
say({ ko: '급여를 접었어요: 합계만 보여요', en: 'Closed Pay: only Total shows' });
},
open: (api) => {
api.setHeaderGroupCollapsed('pay', false);
say({ ko: '급여를 펼쳤어요: 기본급·보너스가 보여요', en: 'Opened Pay: Base and Bonus show' });
},
};<!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 class="toolbar">
<button type="button" id="close">급여 접기</button>
<button type="button" id="open">급여 펼치기</button>
</div>
<div id="grid" style="height: 240px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const ROWS = [
{ id: 1001, name: '김민준', team: '개발팀', city: '서울', salary: 6200, bonus: 620 },
{ id: 1002, name: '이서연', team: '디자인팀', city: '부산', salary: 4800, bonus: 480 },
{ id: 1003, name: '박지호', team: '영업팀', city: '대구', salary: 7100, bonus: 710 },
{ id: 1004, name: '최유나', team: '인사팀', city: '서울', salary: 3900, bonus: 390 },
];
let say = () => { };
function setup(_api, log) {
say = log;
return () => { };
}
const COLUMNS = [
{ id: 'name', title: '이름', width: 120 },
{ id: 'team', title: '팀', width: 110 },
{
id: 'salary',
title: '기본급',
kind: 'number',
width: 110,
align: 'end',
showWhen: 'groupOpen',
},
{ id: 'bonus', title: '보너스', kind: 'number', width: 110, align: 'end', showWhen: 'groupOpen' },
{
id: 'total',
title: '합계',
kind: 'number',
width: 110,
align: 'end',
showWhen: 'groupClosed',
getValue: (row) => row.salary + row.bonus,
},
];
const options = {
columns: COLUMNS,
rows: ROWS,
rowKey: 'id',
headerGroups: [
{ id: 'pay', title: '급여', collapsible: true, children: ['salary', 'bonus', 'total'] },
],
};
const actions = {
close: (api) => {
api.setHeaderGroupCollapsed('pay', true);
say({ ko: '급여를 접었어요: 합계만 보여요', en: 'Closed Pay: only Total shows' });
},
open: (api) => {
api.setHeaderGroupCollapsed('pay', false);
say({ ko: '급여를 펼쳤어요: 기본급·보너스가 보여요', en: 'Opened Pay: Base and Bonus show' });
},
};
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);
});
document.getElementById('close').addEventListener('click', () => actions.close(api));
document.getElementById('open').addEventListener('click', () => actions.open(api));
</script>
</body>
</html>버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<button
type="button"
disabled={!api}
onClick={() => api && actions.close(api)}
>급여 접기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.open(api)}
>급여 펼치기</button>
</div>
<Grid style={{ height: 240 }} ref={gridRef} options={options} />
</>
);
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
team: string;
city: string;
salary: number;
bonus: number;
}
const ROWS: Row[] = [
{ id: 1001, name: '김민준', team: '개발팀', city: '서울', salary: 6200, bonus: 620 },
{ id: 1002, name: '이서연', team: '디자인팀', city: '부산', salary: 4800, bonus: 480 },
{ id: 1003, name: '박지호', team: '영업팀', city: '대구', salary: 7100, bonus: 710 },
{ id: 1004, name: '최유나', team: '인사팀', city: '서울', salary: 3900, bonus: 390 },
];
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {};
}
const COLUMNS: ColumnDef<Row>[] = [
{ id: 'name', title: '이름', width: 120 },
{ id: 'team', title: '팀', width: 110 },
{
id: 'salary',
title: '기본급',
kind: 'number',
width: 110,
align: 'end',
showWhen: 'groupOpen',
},
{ id: 'bonus', title: '보너스', kind: 'number', width: 110, align: 'end', showWhen: 'groupOpen' },
{
id: 'total',
title: '합계',
kind: 'number',
width: 110,
align: 'end',
showWhen: 'groupClosed',
getValue: (row) => row.salary + row.bonus,
},
];
export const options: GridOptions<Row> = {
columns: COLUMNS,
rows: ROWS,
rowKey: 'id',
headerGroups: [
{ id: 'pay', title: '급여', collapsible: true, children: ['salary', 'bonus', 'total'] },
],
};
export const actions = {
close: (api: GridApi<Row>) => {
api.setHeaderGroupCollapsed('pay', true);
say({ ko: '급여를 접었어요: 합계만 보여요', en: 'Closed Pay: only Total shows' });
},
open: (api: GridApi<Row>) => {
api.setHeaderGroupCollapsed('pay', false);
say({ ko: '급여를 펼쳤어요: 기본급·보너스가 보여요', en: 'Opened Pay: Base and Bonus show' });
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, 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>
<div class="toolbar">
<button type="button" :disabled="!api" @click="api && actions.close(api)">급여 접기</button>
<button type="button" :disabled="!api" @click="api && actions.open(api)">급여 펼치기</button>
</div>
<Grid style="height: 240px" :options="options" @ready="onReady" />
</template>import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
team: string;
city: string;
salary: number;
bonus: number;
}
const ROWS: Row[] = [
{ id: 1001, name: '김민준', team: '개발팀', city: '서울', salary: 6200, bonus: 620 },
{ id: 1002, name: '이서연', team: '디자인팀', city: '부산', salary: 4800, bonus: 480 },
{ id: 1003, name: '박지호', team: '영업팀', city: '대구', salary: 7100, bonus: 710 },
{ id: 1004, name: '최유나', team: '인사팀', city: '서울', salary: 3900, bonus: 390 },
];
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {};
}
const COLUMNS: ColumnDef<Row>[] = [
{ id: 'name', title: '이름', width: 120 },
{ id: 'team', title: '팀', width: 110 },
{
id: 'salary',
title: '기본급',
kind: 'number',
width: 110,
align: 'end',
showWhen: 'groupOpen',
},
{ id: 'bonus', title: '보너스', kind: 'number', width: 110, align: 'end', showWhen: 'groupOpen' },
{
id: 'total',
title: '합계',
kind: 'number',
width: 110,
align: 'end',
showWhen: 'groupClosed',
getValue: (row) => row.salary + row.bonus,
},
];
export const options: GridOptions<Row> = {
columns: COLUMNS,
rows: ROWS,
rowKey: 'id',
headerGroups: [
{ id: 'pay', title: '급여', collapsible: true, children: ['salary', 'bonus', 'total'] },
],
};
export const actions = {
close: (api: GridApi<Row>) => {
api.setHeaderGroupCollapsed('pay', true);
say({ ko: '급여를 접었어요: 합계만 보여요', en: 'Closed Pay: only Total shows' });
},
open: (api: GridApi<Row>) => {
api.setHeaderGroupCollapsed('pay', false);
say({ ko: '급여를 펼쳤어요: 기본급·보너스가 보여요', en: 'Opened Pay: Base and Bonus show' });
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<button
type="button"
[disabled]="!api"
(click)="api && actions.close(api)"
>급여 접기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.open(api)"
>급여 펼치기</button>
</div>
<mx-grid style="height: 240px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
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 { ColumnDef, GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
team: string;
city: string;
salary: number;
bonus: number;
}
const ROWS: Row[] = [
{ id: 1001, name: '김민준', team: '개발팀', city: '서울', salary: 6200, bonus: 620 },
{ id: 1002, name: '이서연', team: '디자인팀', city: '부산', salary: 4800, bonus: 480 },
{ id: 1003, name: '박지호', team: '영업팀', city: '대구', salary: 7100, bonus: 710 },
{ id: 1004, name: '최유나', team: '인사팀', city: '서울', salary: 3900, bonus: 390 },
];
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {};
}
const COLUMNS: ColumnDef<Row>[] = [
{ id: 'name', title: '이름', width: 120 },
{ id: 'team', title: '팀', width: 110 },
{
id: 'salary',
title: '기본급',
kind: 'number',
width: 110,
align: 'end',
showWhen: 'groupOpen',
},
{ id: 'bonus', title: '보너스', kind: 'number', width: 110, align: 'end', showWhen: 'groupOpen' },
{
id: 'total',
title: '합계',
kind: 'number',
width: 110,
align: 'end',
showWhen: 'groupClosed',
getValue: (row) => row.salary + row.bonus,
},
];
export const options: GridOptions<Row> = {
columns: COLUMNS,
rows: ROWS,
rowKey: 'id',
headerGroups: [
{ id: 'pay', title: '급여', collapsible: true, children: ['salary', 'bonus', 'total'] },
],
};
export const actions = {
close: (api: GridApi<Row>) => {
api.setHeaderGroupCollapsed('pay', true);
say({ ko: '급여를 접었어요: 합계만 보여요', en: 'Closed Pay: only Total shows' });
},
open: (api: GridApi<Row>) => {
api.setHeaderGroupCollapsed('pay', false);
say({ ko: '급여를 펼쳤어요: 기본급·보너스가 보여요', en: 'Opened Pay: Base and Bonus show' });
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<div id="app">
<div class="toolbar">
<button type="button" id="close">Close Pay</button>
<button type="button" id="open">Open Pay</button>
</div>
<div id="grid" style="height: 240px"></div>
</div>
<script type="module" src="./main.js"></script>import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('close').addEventListener('click', () => actions.close(api));
document.getElementById('open').addEventListener('click', () => actions.open(api));const ROWS = [
{ id: 1001, name: 'Liam Carter', team: 'Dev', city: 'Seattle', salary: 62000, bonus: 6200 },
{ id: 1002, name: 'Emma Stone', team: 'Design', city: 'Boston', salary: 48000, bonus: 4800 },
{ id: 1003, name: 'Noah Hayes', team: 'Sales', city: 'Denver', salary: 71000, bonus: 7100 },
{ id: 1004, name: 'Olivia Reed', team: 'HR', city: 'Seattle', salary: 39000, bonus: 3900 },
];
let say = () => { };
export function setup(_api, log) {
say = log;
return () => { };
}
const COLUMNS = [
{ id: 'name', title: 'Name', width: 120 },
{ id: 'team', title: 'Team', width: 110 },
{
id: 'salary',
title: 'Base',
kind: 'number',
width: 110,
align: 'end',
showWhen: 'groupOpen',
},
{ id: 'bonus', title: 'Bonus', kind: 'number', width: 110, align: 'end', showWhen: 'groupOpen' },
{
id: 'total',
title: 'Total',
kind: 'number',
width: 110,
align: 'end',
showWhen: 'groupClosed',
getValue: (row) => row.salary + row.bonus,
},
];
export const options = {
columns: COLUMNS,
rows: ROWS,
rowKey: 'id',
headerGroups: [
{ id: 'pay', title: 'Pay', collapsible: true, children: ['salary', 'bonus', 'total'] },
],
};
export const actions = {
close: (api) => {
api.setHeaderGroupCollapsed('pay', true);
say('Closed Pay: only Total shows');
},
open: (api) => {
api.setHeaderGroupCollapsed('pay', false);
say('Opened Pay: Base and Bonus show');
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Show columns when a group is open or closed</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="close">Close Pay</button>
<button type="button" id="open">Open Pay</button>
</div>
<div id="grid" style="height: 240px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const ROWS = [
{ id: 1001, name: 'Liam Carter', team: 'Dev', city: 'Seattle', salary: 62000, bonus: 6200 },
{ id: 1002, name: 'Emma Stone', team: 'Design', city: 'Boston', salary: 48000, bonus: 4800 },
{ id: 1003, name: 'Noah Hayes', team: 'Sales', city: 'Denver', salary: 71000, bonus: 7100 },
{ id: 1004, name: 'Olivia Reed', team: 'HR', city: 'Seattle', salary: 39000, bonus: 3900 },
];
let say = () => { };
function setup(_api, log) {
say = log;
return () => { };
}
const COLUMNS = [
{ id: 'name', title: 'Name', width: 120 },
{ id: 'team', title: 'Team', width: 110 },
{
id: 'salary',
title: 'Base',
kind: 'number',
width: 110,
align: 'end',
showWhen: 'groupOpen',
},
{ id: 'bonus', title: 'Bonus', kind: 'number', width: 110, align: 'end', showWhen: 'groupOpen' },
{
id: 'total',
title: 'Total',
kind: 'number',
width: 110,
align: 'end',
showWhen: 'groupClosed',
getValue: (row) => row.salary + row.bonus,
},
];
const options = {
columns: COLUMNS,
rows: ROWS,
rowKey: 'id',
headerGroups: [
{ id: 'pay', title: 'Pay', collapsible: true, children: ['salary', 'bonus', 'total'] },
],
};
const actions = {
close: (api) => {
api.setHeaderGroupCollapsed('pay', true);
say('Closed Pay: only Total shows');
},
open: (api) => {
api.setHeaderGroupCollapsed('pay', false);
say('Opened Pay: Base and Bonus show');
},
};
options.ariaLabel = "Show columns when a group is open or closed";
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);
});
document.getElementById('close').addEventListener('click', () => actions.close(api));
document.getElementById('open').addEventListener('click', () => actions.open(api));
</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 { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<button
type="button"
disabled={!api}
onClick={() => api && actions.close(api)}
>Close Pay</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.open(api)}
>Open Pay</button>
</div>
<Grid style={{ height: 240 }} ref={gridRef} options={options} />
</>
);
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
team: string;
city: string;
salary: number;
bonus: number;
}
const ROWS: Row[] = [
{ id: 1001, name: 'Liam Carter', team: 'Dev', city: 'Seattle', salary: 62000, bonus: 6200 },
{ id: 1002, name: 'Emma Stone', team: 'Design', city: 'Boston', salary: 48000, bonus: 4800 },
{ id: 1003, name: 'Noah Hayes', team: 'Sales', city: 'Denver', salary: 71000, bonus: 7100 },
{ id: 1004, name: 'Olivia Reed', team: 'HR', city: 'Seattle', salary: 39000, bonus: 3900 },
];
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {};
}
const COLUMNS: ColumnDef<Row>[] = [
{ id: 'name', title: 'Name', width: 120 },
{ id: 'team', title: 'Team', width: 110 },
{
id: 'salary',
title: 'Base',
kind: 'number',
width: 110,
align: 'end',
showWhen: 'groupOpen',
},
{ id: 'bonus', title: 'Bonus', kind: 'number', width: 110, align: 'end', showWhen: 'groupOpen' },
{
id: 'total',
title: 'Total',
kind: 'number',
width: 110,
align: 'end',
showWhen: 'groupClosed',
getValue: (row) => row.salary + row.bonus,
},
];
export const options: GridOptions<Row> = {
columns: COLUMNS,
rows: ROWS,
rowKey: 'id',
headerGroups: [
{ id: 'pay', title: 'Pay', collapsible: true, children: ['salary', 'bonus', 'total'] },
],
};
export const actions = {
close: (api: GridApi<Row>) => {
api.setHeaderGroupCollapsed('pay', true);
say('Closed Pay: only Total shows');
},
open: (api: GridApi<Row>) => {
api.setHeaderGroupCollapsed('pay', false);
say('Opened Pay: Base and Bonus show');
},
};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 { actions, 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>
<div class="toolbar">
<button type="button" :disabled="!api" @click="api && actions.close(api)">Close Pay</button>
<button type="button" :disabled="!api" @click="api && actions.open(api)">Open Pay</button>
</div>
<Grid style="height: 240px" :options="options" @ready="onReady" />
</template>import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
team: string;
city: string;
salary: number;
bonus: number;
}
const ROWS: Row[] = [
{ id: 1001, name: 'Liam Carter', team: 'Dev', city: 'Seattle', salary: 62000, bonus: 6200 },
{ id: 1002, name: 'Emma Stone', team: 'Design', city: 'Boston', salary: 48000, bonus: 4800 },
{ id: 1003, name: 'Noah Hayes', team: 'Sales', city: 'Denver', salary: 71000, bonus: 7100 },
{ id: 1004, name: 'Olivia Reed', team: 'HR', city: 'Seattle', salary: 39000, bonus: 3900 },
];
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {};
}
const COLUMNS: ColumnDef<Row>[] = [
{ id: 'name', title: 'Name', width: 120 },
{ id: 'team', title: 'Team', width: 110 },
{
id: 'salary',
title: 'Base',
kind: 'number',
width: 110,
align: 'end',
showWhen: 'groupOpen',
},
{ id: 'bonus', title: 'Bonus', kind: 'number', width: 110, align: 'end', showWhen: 'groupOpen' },
{
id: 'total',
title: 'Total',
kind: 'number',
width: 110,
align: 'end',
showWhen: 'groupClosed',
getValue: (row) => row.salary + row.bonus,
},
];
export const options: GridOptions<Row> = {
columns: COLUMNS,
rows: ROWS,
rowKey: 'id',
headerGroups: [
{ id: 'pay', title: 'Pay', collapsible: true, children: ['salary', 'bonus', 'total'] },
],
};
export const actions = {
close: (api: GridApi<Row>) => {
api.setHeaderGroupCollapsed('pay', true);
say('Closed Pay: only Total shows');
},
open: (api: GridApi<Row>) => {
api.setHeaderGroupCollapsed('pay', false);
say('Opened Pay: Base and Bonus show');
},
};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 { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<button
type="button"
[disabled]="!api"
(click)="api && actions.close(api)"
>Close Pay</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.open(api)"
>Open Pay</button>
</div>
<mx-grid style="height: 240px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
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 { ColumnDef, GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
team: string;
city: string;
salary: number;
bonus: number;
}
const ROWS: Row[] = [
{ id: 1001, name: 'Liam Carter', team: 'Dev', city: 'Seattle', salary: 62000, bonus: 6200 },
{ id: 1002, name: 'Emma Stone', team: 'Design', city: 'Boston', salary: 48000, bonus: 4800 },
{ id: 1003, name: 'Noah Hayes', team: 'Sales', city: 'Denver', salary: 71000, bonus: 7100 },
{ id: 1004, name: 'Olivia Reed', team: 'HR', city: 'Seattle', salary: 39000, bonus: 3900 },
];
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {};
}
const COLUMNS: ColumnDef<Row>[] = [
{ id: 'name', title: 'Name', width: 120 },
{ id: 'team', title: 'Team', width: 110 },
{
id: 'salary',
title: 'Base',
kind: 'number',
width: 110,
align: 'end',
showWhen: 'groupOpen',
},
{ id: 'bonus', title: 'Bonus', kind: 'number', width: 110, align: 'end', showWhen: 'groupOpen' },
{
id: 'total',
title: 'Total',
kind: 'number',
width: 110,
align: 'end',
showWhen: 'groupClosed',
getValue: (row) => row.salary + row.bonus,
},
];
export const options: GridOptions<Row> = {
columns: COLUMNS,
rows: ROWS,
rowKey: 'id',
headerGroups: [
{ id: 'pay', title: 'Pay', collapsible: true, children: ['salary', 'bonus', 'total'] },
],
};
export const actions = {
close: (api: GridApi<Row>) => {
api.setHeaderGroupCollapsed('pay', true);
say('Closed Pay: only Total shows');
},
open: (api: GridApi<Row>) => {
api.setHeaderGroupCollapsed('pay', false);
say('Opened Pay: Base and Bonus show');
},
};