페이지를 나눠도 같은 값은 칸이 합쳐져요Merged cells work inside pages too
대분류, 구분처럼 같은 값이 이어지면 칸을 합쳐 보여요. 페이지를 나눠도 그 페이지에 보이는 줄 안에서 합쳐지고, '구분'은 같은 대분류 안에서만 합치도록 기준을 정할 수 있어요.Equal neighbouring values merge into one cell, and merging works within each page. The Group column can be told to merge only inside the same major category.
코드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">
<select aria-label="칸 합치기" id="merge">
<option value="on" selected>켜기</option>
<option value="off">끄기</option>
</select>
<select aria-label="구분 합치는 기준" id="within">
<option value="major" selected>같은 대분류 안에서만</option>
<option value="value">값이 같으면</option>
</select>
</div>
<div id="grid" style="height: 420px"></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);
document.getElementById('merge').addEventListener('change', (event) => actions.merge(api, event.target.value));
document.getElementById('within').addEventListener('change', (event) => actions.within(api, event.target.value));// 식품 영양표: 대분류 → 구분 → 품목. 같은 값이 이어지면 칸을 합쳐 보여요.
// '구분'에는 대분류마다 똑같은 이름('기타')이 있어서, 값만 보면 서로 다른 대분류의 칸까지 합쳐질 수 있어요.
const DATA = [
['식물성식품', '곡류', '쌀밥', 310],
['식물성식품', '곡류', '현미밥', 330],
['식물성식품', '곡류', '보리밥', 300],
['식물성식품', '채소류', '시금치', 23],
['식물성식품', '채소류', '당근', 41],
['식물성식품', '기타', '두부', 84],
['식물성식품', '기타', '김', 35],
['동물성식품', '기타', '달걀', 143],
['동물성식품', '기타', '우유', 61],
['동물성식품', '육류', '닭가슴살', 165],
['동물성식품', '육류', '돼지목살', 240],
['동물성식품', '육류', '소등심', 250],
['동물성식품', '수산물', '고등어', 205],
['동물성식품', '수산물', '연어', 208],
['가공식품', '면류', '라면', 480],
['가공식품', '면류', '우동', 330],
['가공식품', '기타', '참치캔', 190],
['가공식품', '기타', '식빵', 265],
];
const rows = DATA.map(([major, group, item, kcal], i) => ({
id: i + 1,
major,
group,
item,
kcal,
}));
// mergeWithin을 쓰면 '구분'은 같은 대분류 안에서만 합쳐져요(예: 식물성식품의 기타와 동물성식품의 기타는 따로).
const columns = (merge, within) => [
{ id: 'major', title: '대분류', width: 120, merge },
{ id: 'group', title: '구분', width: 110, merge, mergeWithin: within ? 'major' : undefined },
{ id: 'item', title: '품목', width: 130 },
{ id: 'kcal', title: '열량(kcal)', kind: 'number', width: 110, align: 'end' },
];
export const options = {
columns: columns(true, true),
rows,
rowKey: 'id',
showRowNumbers: true,
pagination: { mode: 'client', pageSize: 9 },
};
let merge = true;
let within = true;
const apply = (api) => api.setColumns(columns(merge, within));
export function setup() {
merge = true;
within = true;
return () => { };
}
export const actions = {
merge: (api, value) => {
merge = value !== 'off';
apply(api);
},
within: (api, value) => {
within = value !== 'value';
apply(api);
},
};<!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">
<select aria-label="칸 합치기" id="merge">
<option value="on" selected>켜기</option>
<option value="off">끄기</option>
</select>
<select aria-label="구분 합치는 기준" id="within">
<option value="major" selected>같은 대분류 안에서만</option>
<option value="value">값이 같으면</option>
</select>
</div>
<div id="grid" style="height: 420px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
// 식품 영양표: 대분류 → 구분 → 품목. 같은 값이 이어지면 칸을 합쳐 보여요.
// '구분'에는 대분류마다 똑같은 이름('기타')이 있어서, 값만 보면 서로 다른 대분류의 칸까지 합쳐질 수 있어요.
const DATA = [
['식물성식품', '곡류', '쌀밥', 310],
['식물성식품', '곡류', '현미밥', 330],
['식물성식품', '곡류', '보리밥', 300],
['식물성식품', '채소류', '시금치', 23],
['식물성식품', '채소류', '당근', 41],
['식물성식품', '기타', '두부', 84],
['식물성식품', '기타', '김', 35],
['동물성식품', '기타', '달걀', 143],
['동물성식품', '기타', '우유', 61],
['동물성식품', '육류', '닭가슴살', 165],
['동물성식품', '육류', '돼지목살', 240],
['동물성식품', '육류', '소등심', 250],
['동물성식품', '수산물', '고등어', 205],
['동물성식품', '수산물', '연어', 208],
['가공식품', '면류', '라면', 480],
['가공식품', '면류', '우동', 330],
['가공식품', '기타', '참치캔', 190],
['가공식품', '기타', '식빵', 265],
];
const rows = DATA.map(([major, group, item, kcal], i) => ({
id: i + 1,
major,
group,
item,
kcal,
}));
// mergeWithin을 쓰면 '구분'은 같은 대분류 안에서만 합쳐져요(예: 식물성식품의 기타와 동물성식품의 기타는 따로).
const columns = (merge, within) => [
{ id: 'major', title: '대분류', width: 120, merge },
{ id: 'group', title: '구분', width: 110, merge, mergeWithin: within ? 'major' : undefined },
{ id: 'item', title: '품목', width: 130 },
{ id: 'kcal', title: '열량(kcal)', kind: 'number', width: 110, align: 'end' },
];
const options = {
columns: columns(true, true),
rows,
rowKey: 'id',
showRowNumbers: true,
pagination: { mode: 'client', pageSize: 9 },
};
let merge = true;
let within = true;
const apply = (api) => api.setColumns(columns(merge, within));
function setup() {
merge = true;
within = true;
return () => { };
}
const actions = {
merge: (api, value) => {
merge = value !== 'off';
apply(api);
},
within: (api, value) => {
within = value !== 'value';
apply(api);
},
};
options.ariaLabel = "페이지를 나눠도 같은 값은 칸이 합쳐져요";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api);
document.getElementById('merge').addEventListener('change', (event) => actions.merge(api, event.target.value));
document.getElementById('within').addEventListener('change', (event) => actions.within(api, event.target.value));
</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) : undefined), [api]);
return (
<>
<div className="toolbar">
<select
aria-label="칸 합치기"
disabled={!api}
onChange={(event) => api && actions.merge(api, event.target.value)}
>
<option value="on">켜기</option>
<option value="off">끄기</option>
</select>
<select defaultValue="on"
aria-label="구분 합치는 기준"
disabled={!api}
onChange={(event) => api && actions.within(api, event.target.value)}
>
<option value="major">같은 대분류 안에서만</option>
<option value="value">값이 같으면</option>
</select>
</div>
<Grid style={{ height: 420 }} ref={gridRef} options={options} />
</>
);
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
major: string;
group: string;
item: string;
kcal: number;
}
// 식품 영양표: 대분류 → 구분 → 품목. 같은 값이 이어지면 칸을 합쳐 보여요.
// '구분'에는 대분류마다 똑같은 이름('기타')이 있어서, 값만 보면 서로 다른 대분류의 칸까지 합쳐질 수 있어요.
const DATA: [string, string, string, number][] = [
['식물성식품', '곡류', '쌀밥', 310],
['식물성식품', '곡류', '현미밥', 330],
['식물성식품', '곡류', '보리밥', 300],
['식물성식품', '채소류', '시금치', 23],
['식물성식품', '채소류', '당근', 41],
['식물성식품', '기타', '두부', 84],
['식물성식품', '기타', '김', 35],
['동물성식품', '기타', '달걀', 143],
['동물성식품', '기타', '우유', 61],
['동물성식품', '육류', '닭가슴살', 165],
['동물성식품', '육류', '돼지목살', 240],
['동물성식품', '육류', '소등심', 250],
['동물성식품', '수산물', '고등어', 205],
['동물성식품', '수산물', '연어', 208],
['가공식품', '면류', '라면', 480],
['가공식품', '면류', '우동', 330],
['가공식품', '기타', '참치캔', 190],
['가공식품', '기타', '식빵', 265],
];
const rows: Row[] = DATA.map(([major, group, item, kcal], i) => ({
id: i + 1,
major,
group,
item,
kcal,
}));
// mergeWithin을 쓰면 '구분'은 같은 대분류 안에서만 합쳐져요(예: 식물성식품의 기타와 동물성식품의 기타는 따로).
const columns = (merge: boolean, within: boolean): ColumnDef<Row>[] => [
{ id: 'major', title: '대분류', width: 120, merge },
{ id: 'group', title: '구분', width: 110, merge, mergeWithin: within ? 'major' : undefined },
{ id: 'item', title: '품목', width: 130 },
{ id: 'kcal', title: '열량(kcal)', kind: 'number', width: 110, align: 'end' },
];
export const options: GridOptions<Row> = {
columns: columns(true, true),
rows,
rowKey: 'id',
showRowNumbers: true,
pagination: { mode: 'client', pageSize: 9 },
};
let merge = true;
let within = true;
const apply = (api: GridApi<Row>): void => api.setColumns(columns(merge, within));
export function setup() {
merge = true;
within = true;
return () => {};
}
export const actions = {
merge: (api: GridApi<Row>, value?: string) => {
merge = value !== 'off';
apply(api);
},
within: (api: GridApi<Row>, value?: string) => {
within = value !== 'value';
apply(api);
},
};버튼을 누르면 이 예제의 코드가 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);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<select
aria-label="칸 합치기"
:disabled="!api"
@change="api && actions.merge(api, ($event.target as HTMLSelectElement).value)"
>
<option value="on" selected>켜기</option>
<option value="off">끄기</option>
</select>
<select
aria-label="구분 합치는 기준"
:disabled="!api"
@change="api && actions.within(api, ($event.target as HTMLSelectElement).value)"
>
<option value="major" selected>같은 대분류 안에서만</option>
<option value="value">값이 같으면</option>
</select>
</div>
<Grid style="height: 420px" :options="options" @ready="onReady" />
</template>import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
major: string;
group: string;
item: string;
kcal: number;
}
// 식품 영양표: 대분류 → 구분 → 품목. 같은 값이 이어지면 칸을 합쳐 보여요.
// '구분'에는 대분류마다 똑같은 이름('기타')이 있어서, 값만 보면 서로 다른 대분류의 칸까지 합쳐질 수 있어요.
const DATA: [string, string, string, number][] = [
['식물성식품', '곡류', '쌀밥', 310],
['식물성식품', '곡류', '현미밥', 330],
['식물성식품', '곡류', '보리밥', 300],
['식물성식품', '채소류', '시금치', 23],
['식물성식품', '채소류', '당근', 41],
['식물성식품', '기타', '두부', 84],
['식물성식품', '기타', '김', 35],
['동물성식품', '기타', '달걀', 143],
['동물성식품', '기타', '우유', 61],
['동물성식품', '육류', '닭가슴살', 165],
['동물성식품', '육류', '돼지목살', 240],
['동물성식품', '육류', '소등심', 250],
['동물성식품', '수산물', '고등어', 205],
['동물성식품', '수산물', '연어', 208],
['가공식품', '면류', '라면', 480],
['가공식품', '면류', '우동', 330],
['가공식품', '기타', '참치캔', 190],
['가공식품', '기타', '식빵', 265],
];
const rows: Row[] = DATA.map(([major, group, item, kcal], i) => ({
id: i + 1,
major,
group,
item,
kcal,
}));
// mergeWithin을 쓰면 '구분'은 같은 대분류 안에서만 합쳐져요(예: 식물성식품의 기타와 동물성식품의 기타는 따로).
const columns = (merge: boolean, within: boolean): ColumnDef<Row>[] => [
{ id: 'major', title: '대분류', width: 120, merge },
{ id: 'group', title: '구분', width: 110, merge, mergeWithin: within ? 'major' : undefined },
{ id: 'item', title: '품목', width: 130 },
{ id: 'kcal', title: '열량(kcal)', kind: 'number', width: 110, align: 'end' },
];
export const options: GridOptions<Row> = {
columns: columns(true, true),
rows,
rowKey: 'id',
showRowNumbers: true,
pagination: { mode: 'client', pageSize: 9 },
};
let merge = true;
let within = true;
const apply = (api: GridApi<Row>): void => api.setColumns(columns(merge, within));
export function setup() {
merge = true;
within = true;
return () => {};
}
export const actions = {
merge: (api: GridApi<Row>, value?: string) => {
merge = value !== 'off';
apply(api);
},
within: (api: GridApi<Row>, value?: string) => {
within = value !== 'value';
apply(api);
},
};버튼을 누르면 이 예제의 코드가 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">
<select
aria-label="칸 합치기"
[disabled]="!api"
(change)="api && actions.merge(api, $any($event.target).value)"
>
<option value="on" selected>켜기</option>
<option value="off">끄기</option>
</select>
<select
aria-label="구분 합치는 기준"
[disabled]="!api"
(change)="api && actions.within(api, $any($event.target).value)"
>
<option value="major" selected>같은 대분류 안에서만</option>
<option value="value">값이 같으면</option>
</select>
</div>
<mx-grid style="height: 420px" [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);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/angular';
export interface Row {
id: number;
major: string;
group: string;
item: string;
kcal: number;
}
// 식품 영양표: 대분류 → 구분 → 품목. 같은 값이 이어지면 칸을 합쳐 보여요.
// '구분'에는 대분류마다 똑같은 이름('기타')이 있어서, 값만 보면 서로 다른 대분류의 칸까지 합쳐질 수 있어요.
const DATA: [string, string, string, number][] = [
['식물성식품', '곡류', '쌀밥', 310],
['식물성식품', '곡류', '현미밥', 330],
['식물성식품', '곡류', '보리밥', 300],
['식물성식품', '채소류', '시금치', 23],
['식물성식품', '채소류', '당근', 41],
['식물성식품', '기타', '두부', 84],
['식물성식품', '기타', '김', 35],
['동물성식품', '기타', '달걀', 143],
['동물성식품', '기타', '우유', 61],
['동물성식품', '육류', '닭가슴살', 165],
['동물성식품', '육류', '돼지목살', 240],
['동물성식품', '육류', '소등심', 250],
['동물성식품', '수산물', '고등어', 205],
['동물성식품', '수산물', '연어', 208],
['가공식품', '면류', '라면', 480],
['가공식품', '면류', '우동', 330],
['가공식품', '기타', '참치캔', 190],
['가공식품', '기타', '식빵', 265],
];
const rows: Row[] = DATA.map(([major, group, item, kcal], i) => ({
id: i + 1,
major,
group,
item,
kcal,
}));
// mergeWithin을 쓰면 '구분'은 같은 대분류 안에서만 합쳐져요(예: 식물성식품의 기타와 동물성식품의 기타는 따로).
const columns = (merge: boolean, within: boolean): ColumnDef<Row>[] => [
{ id: 'major', title: '대분류', width: 120, merge },
{ id: 'group', title: '구분', width: 110, merge, mergeWithin: within ? 'major' : undefined },
{ id: 'item', title: '품목', width: 130 },
{ id: 'kcal', title: '열량(kcal)', kind: 'number', width: 110, align: 'end' },
];
export const options: GridOptions<Row> = {
columns: columns(true, true),
rows,
rowKey: 'id',
showRowNumbers: true,
pagination: { mode: 'client', pageSize: 9 },
};
let merge = true;
let within = true;
const apply = (api: GridApi<Row>): void => api.setColumns(columns(merge, within));
export function setup() {
merge = true;
within = true;
return () => {};
}
export const actions = {
merge: (api: GridApi<Row>, value?: string) => {
merge = value !== 'off';
apply(api);
},
within: (api: GridApi<Row>, value?: string) => {
within = value !== 'value';
apply(api);
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<div id="app">
<div class="toolbar">
<select aria-label="Merge cells" id="merge">
<option value="on" selected>On</option>
<option value="off">Off</option>
</select>
<select aria-label="Group merge basis" id="within">
<option value="major" selected>Inside the same major category</option>
<option value="value">Same value</option>
</select>
</div>
<div id="grid" style="height: 420px"></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);
document.getElementById('merge').addEventListener('change', (event) => actions.merge(api, event.target.value));
document.getElementById('within').addEventListener('change', (event) => actions.within(api, event.target.value));// Food nutrition table: major category -> group -> item. Cells with the same value in a row are merged.
// "Group" has the same name ('Other') under every major category, so going by value alone could merge cells across different major categories.
const DATA = [
['Plant foods', 'Grains', 'White rice', 310],
['Plant foods', 'Grains', 'Brown rice', 330],
['Plant foods', 'Grains', 'Barley rice', 300],
['Plant foods', 'Vegetables', 'Spinach', 23],
['Plant foods', 'Vegetables', 'Carrot', 41],
['Plant foods', 'Other', 'Tofu', 84],
['Plant foods', 'Other', 'Seaweed', 35],
['Animal foods', 'Other', 'Egg', 143],
['Animal foods', 'Other', 'Milk', 61],
['Animal foods', 'Meat', 'Chicken breast', 165],
['Animal foods', 'Meat', 'Pork shoulder', 240],
['Animal foods', 'Meat', 'Beef sirloin', 250],
['Animal foods', 'Seafood', 'Mackerel', 205],
['Animal foods', 'Seafood', 'Salmon', 208],
['Processed foods', 'Noodles', 'Instant ramen', 480],
['Processed foods', 'Noodles', 'Udon', 330],
['Processed foods', 'Other', 'Canned tuna', 190],
['Processed foods', 'Other', 'Sandwich bread', 265],
];
const rows = DATA.map(([major, group, item, kcal], i) => ({
id: i + 1,
major,
group,
item,
kcal,
}));
// With mergeWithin, "Group" merges only inside the same major category (e.g. Other under Plant foods and Other under Animal foods stay separate).
const columns = (merge, within) => [
{ id: 'major', title: 'Major category', width: 170, merge },
{ id: 'group', title: 'Group', width: 110, merge, mergeWithin: within ? 'major' : undefined },
{ id: 'item', title: 'Item', width: 140 },
{ id: 'kcal', title: 'Energy (kcal)', kind: 'number', width: 160, align: 'end' },
];
export const options = {
columns: columns(true, true),
rows,
rowKey: 'id',
showRowNumbers: true,
pagination: { mode: 'client', pageSize: 9 },
};
let merge = true;
let within = true;
const apply = (api) => api.setColumns(columns(merge, within));
export function setup() {
merge = true;
within = true;
return () => { };
}
export const actions = {
merge: (api, value) => {
merge = value !== 'off';
apply(api);
},
within: (api, value) => {
within = value !== 'value';
apply(api);
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Merged cells work inside pages too</title>
</head>
<body>
<div class="toolbar">
<select aria-label="Merge cells" id="merge">
<option value="on" selected>On</option>
<option value="off">Off</option>
</select>
<select aria-label="Group merge basis" id="within">
<option value="major" selected>Inside the same major category</option>
<option value="value">Same value</option>
</select>
</div>
<div id="grid" style="height: 420px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
// Food nutrition table: major category -> group -> item. Cells with the same value in a row are merged.
// "Group" has the same name ('Other') under every major category, so going by value alone could merge cells across different major categories.
const DATA = [
['Plant foods', 'Grains', 'White rice', 310],
['Plant foods', 'Grains', 'Brown rice', 330],
['Plant foods', 'Grains', 'Barley rice', 300],
['Plant foods', 'Vegetables', 'Spinach', 23],
['Plant foods', 'Vegetables', 'Carrot', 41],
['Plant foods', 'Other', 'Tofu', 84],
['Plant foods', 'Other', 'Seaweed', 35],
['Animal foods', 'Other', 'Egg', 143],
['Animal foods', 'Other', 'Milk', 61],
['Animal foods', 'Meat', 'Chicken breast', 165],
['Animal foods', 'Meat', 'Pork shoulder', 240],
['Animal foods', 'Meat', 'Beef sirloin', 250],
['Animal foods', 'Seafood', 'Mackerel', 205],
['Animal foods', 'Seafood', 'Salmon', 208],
['Processed foods', 'Noodles', 'Instant ramen', 480],
['Processed foods', 'Noodles', 'Udon', 330],
['Processed foods', 'Other', 'Canned tuna', 190],
['Processed foods', 'Other', 'Sandwich bread', 265],
];
const rows = DATA.map(([major, group, item, kcal], i) => ({
id: i + 1,
major,
group,
item,
kcal,
}));
// With mergeWithin, "Group" merges only inside the same major category (e.g. Other under Plant foods and Other under Animal foods stay separate).
const columns = (merge, within) => [
{ id: 'major', title: 'Major category', width: 170, merge },
{ id: 'group', title: 'Group', width: 110, merge, mergeWithin: within ? 'major' : undefined },
{ id: 'item', title: 'Item', width: 140 },
{ id: 'kcal', title: 'Energy (kcal)', kind: 'number', width: 160, align: 'end' },
];
const options = {
columns: columns(true, true),
rows,
rowKey: 'id',
showRowNumbers: true,
pagination: { mode: 'client', pageSize: 9 },
};
let merge = true;
let within = true;
const apply = (api) => api.setColumns(columns(merge, within));
function setup() {
merge = true;
within = true;
return () => { };
}
const actions = {
merge: (api, value) => {
merge = value !== 'off';
apply(api);
},
within: (api, value) => {
within = value !== 'value';
apply(api);
},
};
options.ariaLabel = "Merged cells work inside pages too";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api);
document.getElementById('merge').addEventListener('change', (event) => actions.merge(api, event.target.value));
document.getElementById('within').addEventListener('change', (event) => actions.within(api, event.target.value));
</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) : undefined), [api]);
return (
<>
<div className="toolbar">
<select
aria-label="Merge cells"
disabled={!api}
onChange={(event) => api && actions.merge(api, event.target.value)}
>
<option value="on">On</option>
<option value="off">Off</option>
</select>
<select defaultValue="on"
aria-label="Group merge basis"
disabled={!api}
onChange={(event) => api && actions.within(api, event.target.value)}
>
<option value="major">Inside the same major category</option>
<option value="value">Same value</option>
</select>
</div>
<Grid style={{ height: 420 }} ref={gridRef} options={options} />
</>
);
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
major: string;
group: string;
item: string;
kcal: number;
}
// Food nutrition table: major category -> group -> item. Cells with the same value in a row are merged.
// "Group" has the same name ('Other') under every major category, so going by value alone could merge cells across different major categories.
const DATA: [string, string, string, number][] = [
['Plant foods', 'Grains', 'White rice', 310],
['Plant foods', 'Grains', 'Brown rice', 330],
['Plant foods', 'Grains', 'Barley rice', 300],
['Plant foods', 'Vegetables', 'Spinach', 23],
['Plant foods', 'Vegetables', 'Carrot', 41],
['Plant foods', 'Other', 'Tofu', 84],
['Plant foods', 'Other', 'Seaweed', 35],
['Animal foods', 'Other', 'Egg', 143],
['Animal foods', 'Other', 'Milk', 61],
['Animal foods', 'Meat', 'Chicken breast', 165],
['Animal foods', 'Meat', 'Pork shoulder', 240],
['Animal foods', 'Meat', 'Beef sirloin', 250],
['Animal foods', 'Seafood', 'Mackerel', 205],
['Animal foods', 'Seafood', 'Salmon', 208],
['Processed foods', 'Noodles', 'Instant ramen', 480],
['Processed foods', 'Noodles', 'Udon', 330],
['Processed foods', 'Other', 'Canned tuna', 190],
['Processed foods', 'Other', 'Sandwich bread', 265],
];
const rows: Row[] = DATA.map(([major, group, item, kcal], i) => ({
id: i + 1,
major,
group,
item,
kcal,
}));
// With mergeWithin, "Group" merges only inside the same major category (e.g. Other under Plant foods and Other under Animal foods stay separate).
const columns = (merge: boolean, within: boolean): ColumnDef<Row>[] => [
{ id: 'major', title: 'Major category', width: 170, merge },
{ id: 'group', title: 'Group', width: 110, merge, mergeWithin: within ? 'major' : undefined },
{ id: 'item', title: 'Item', width: 140 },
{ id: 'kcal', title: 'Energy (kcal)', kind: 'number', width: 160, align: 'end' },
];
export const options: GridOptions<Row> = {
columns: columns(true, true),
rows,
rowKey: 'id',
showRowNumbers: true,
pagination: { mode: 'client', pageSize: 9 },
};
let merge = true;
let within = true;
const apply = (api: GridApi<Row>): void => api.setColumns(columns(merge, within));
export function setup() {
merge = true;
within = true;
return () => {};
}
export const actions = {
merge: (api: GridApi<Row>, value?: string) => {
merge = value !== 'off';
apply(api);
},
within: (api: GridApi<Row>, value?: string) => {
within = value !== 'value';
apply(api);
},
};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);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<select
aria-label="Merge cells"
:disabled="!api"
@change="api && actions.merge(api, ($event.target as HTMLSelectElement).value)"
>
<option value="on" selected>On</option>
<option value="off">Off</option>
</select>
<select
aria-label="Group merge basis"
:disabled="!api"
@change="api && actions.within(api, ($event.target as HTMLSelectElement).value)"
>
<option value="major" selected>Inside the same major category</option>
<option value="value">Same value</option>
</select>
</div>
<Grid style="height: 420px" :options="options" @ready="onReady" />
</template>import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
major: string;
group: string;
item: string;
kcal: number;
}
// Food nutrition table: major category -> group -> item. Cells with the same value in a row are merged.
// "Group" has the same name ('Other') under every major category, so going by value alone could merge cells across different major categories.
const DATA: [string, string, string, number][] = [
['Plant foods', 'Grains', 'White rice', 310],
['Plant foods', 'Grains', 'Brown rice', 330],
['Plant foods', 'Grains', 'Barley rice', 300],
['Plant foods', 'Vegetables', 'Spinach', 23],
['Plant foods', 'Vegetables', 'Carrot', 41],
['Plant foods', 'Other', 'Tofu', 84],
['Plant foods', 'Other', 'Seaweed', 35],
['Animal foods', 'Other', 'Egg', 143],
['Animal foods', 'Other', 'Milk', 61],
['Animal foods', 'Meat', 'Chicken breast', 165],
['Animal foods', 'Meat', 'Pork shoulder', 240],
['Animal foods', 'Meat', 'Beef sirloin', 250],
['Animal foods', 'Seafood', 'Mackerel', 205],
['Animal foods', 'Seafood', 'Salmon', 208],
['Processed foods', 'Noodles', 'Instant ramen', 480],
['Processed foods', 'Noodles', 'Udon', 330],
['Processed foods', 'Other', 'Canned tuna', 190],
['Processed foods', 'Other', 'Sandwich bread', 265],
];
const rows: Row[] = DATA.map(([major, group, item, kcal], i) => ({
id: i + 1,
major,
group,
item,
kcal,
}));
// With mergeWithin, "Group" merges only inside the same major category (e.g. Other under Plant foods and Other under Animal foods stay separate).
const columns = (merge: boolean, within: boolean): ColumnDef<Row>[] => [
{ id: 'major', title: 'Major category', width: 170, merge },
{ id: 'group', title: 'Group', width: 110, merge, mergeWithin: within ? 'major' : undefined },
{ id: 'item', title: 'Item', width: 140 },
{ id: 'kcal', title: 'Energy (kcal)', kind: 'number', width: 160, align: 'end' },
];
export const options: GridOptions<Row> = {
columns: columns(true, true),
rows,
rowKey: 'id',
showRowNumbers: true,
pagination: { mode: 'client', pageSize: 9 },
};
let merge = true;
let within = true;
const apply = (api: GridApi<Row>): void => api.setColumns(columns(merge, within));
export function setup() {
merge = true;
within = true;
return () => {};
}
export const actions = {
merge: (api: GridApi<Row>, value?: string) => {
merge = value !== 'off';
apply(api);
},
within: (api: GridApi<Row>, value?: string) => {
within = value !== 'value';
apply(api);
},
};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">
<select
aria-label="Merge cells"
[disabled]="!api"
(change)="api && actions.merge(api, $any($event.target).value)"
>
<option value="on" selected>On</option>
<option value="off">Off</option>
</select>
<select
aria-label="Group merge basis"
[disabled]="!api"
(change)="api && actions.within(api, $any($event.target).value)"
>
<option value="major" selected>Inside the same major category</option>
<option value="value">Same value</option>
</select>
</div>
<mx-grid style="height: 420px" [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);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/angular';
export interface Row {
id: number;
major: string;
group: string;
item: string;
kcal: number;
}
// Food nutrition table: major category -> group -> item. Cells with the same value in a row are merged.
// "Group" has the same name ('Other') under every major category, so going by value alone could merge cells across different major categories.
const DATA: [string, string, string, number][] = [
['Plant foods', 'Grains', 'White rice', 310],
['Plant foods', 'Grains', 'Brown rice', 330],
['Plant foods', 'Grains', 'Barley rice', 300],
['Plant foods', 'Vegetables', 'Spinach', 23],
['Plant foods', 'Vegetables', 'Carrot', 41],
['Plant foods', 'Other', 'Tofu', 84],
['Plant foods', 'Other', 'Seaweed', 35],
['Animal foods', 'Other', 'Egg', 143],
['Animal foods', 'Other', 'Milk', 61],
['Animal foods', 'Meat', 'Chicken breast', 165],
['Animal foods', 'Meat', 'Pork shoulder', 240],
['Animal foods', 'Meat', 'Beef sirloin', 250],
['Animal foods', 'Seafood', 'Mackerel', 205],
['Animal foods', 'Seafood', 'Salmon', 208],
['Processed foods', 'Noodles', 'Instant ramen', 480],
['Processed foods', 'Noodles', 'Udon', 330],
['Processed foods', 'Other', 'Canned tuna', 190],
['Processed foods', 'Other', 'Sandwich bread', 265],
];
const rows: Row[] = DATA.map(([major, group, item, kcal], i) => ({
id: i + 1,
major,
group,
item,
kcal,
}));
// With mergeWithin, "Group" merges only inside the same major category (e.g. Other under Plant foods and Other under Animal foods stay separate).
const columns = (merge: boolean, within: boolean): ColumnDef<Row>[] => [
{ id: 'major', title: 'Major category', width: 170, merge },
{ id: 'group', title: 'Group', width: 110, merge, mergeWithin: within ? 'major' : undefined },
{ id: 'item', title: 'Item', width: 140 },
{ id: 'kcal', title: 'Energy (kcal)', kind: 'number', width: 160, align: 'end' },
];
export const options: GridOptions<Row> = {
columns: columns(true, true),
rows,
rowKey: 'id',
showRowNumbers: true,
pagination: { mode: 'client', pageSize: 9 },
};
let merge = true;
let within = true;
const apply = (api: GridApi<Row>): void => api.setColumns(columns(merge, within));
export function setup() {
merge = true;
within = true;
return () => {};
}
export const actions = {
merge: (api: GridApi<Row>, value?: string) => {
merge = value !== 'off';
apply(api);
},
within: (api: GridApi<Row>, value?: string) => {
within = value !== 'value';
apply(api);
},
};