합친 구간을 코드로 묻기Ask for merged spans in code
getMergeSpan(rowKey, columnId)는 칸이 속한 병합 구간 { rows, cols, start }를 돌려주고 합치지 않은 칸은 null이에요. 구간의 모든 칸이 같은 start를 돌려줘요. isMerged는 병합 여부만 돌려주고요. 구간은 보이는 행 전체에서 계산하고 정렬·필터가 바뀌면 다시 계산해요.getMergeSpan(rowKey, columnId) returns the merged span { rows, cols, start } of a cell, or null for an unmerged cell. Every cell of a span returns the same start, and isMerged returns only whether the cell is merged. Spans are computed over all visible rows and recalculated when the sort or filter changes.
코드Code
탭을 골라 코드를 복사하세요. 위 화면은 이 코드를 그대로 실행한 결과예요.Pick a tab and copy the code. The preview above runs exactly this code.
이 화면은 새 모양 옵션을 켠 모양이에요: density: 'spacious', iconStyle: 'soft', motion: true (코드에 없으면 기본 모양으로 보여요)This preview has the new-look options on: density: 'spacious', iconStyle: 'soft', motion: true (without them in your code you get the default look)
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<div id="app">
<div class="toolbar">
<button type="button" id="dept">2행 부서 구간</button>
<button type="button" id="team">3행 팀 구간</button>
<button type="button" id="single">4행 부서(한 칸)</button>
<button type="button" id="sort">이름 내림차순 정렬</button>
<button type="button" id="reset">정렬 해제</button>
</div>
<div id="grid" style="height: 330px"></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('dept').addEventListener('click', () => actions.dept(api));
document.getElementById('team').addEventListener('click', () => actions.team(api));
document.getElementById('single').addEventListener('click', () => actions.single(api));
document.getElementById('sort').addEventListener('click', () => actions.sort(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));const rows = [
{ id: 1, dept: '개발', team: '웹', name: '김하늘' },
{ id: 2, dept: '개발', team: '웹', name: '이도윤' },
{ id: 3, dept: '개발', team: '앱', name: '박서연' },
{ id: 4, dept: '운영', team: '인프라', name: '최지우' },
{ id: 5, dept: '디자인', team: '브랜드', name: '정민준' },
{ id: 6, dept: '디자인', team: '브랜드', name: '강서윤' },
];
// getMergeSpan(rowKey, columnId)는 칸이 속한 병합 구간 { rows, cols, start }를, isMerged는 병합 여부를 돌려줘요.
// 구간은 화면에 그려진 줄이 아니라 보이는 행 전체에서 계산하고 정렬·필터를 바꾸면 다시 계산돼요.
export const options = {
columns: [
{ id: 'dept', title: '부서', width: 90, merge: true },
{ id: 'team', title: '팀', width: 90, merge: true, mergeWithin: 'dept' },
{ id: 'name', title: '이름', width: 90, merge: true },
],
rows,
rowKey: 'id',
};
let say = () => { };
export function setup(_api, log) {
say = log;
log({
ko: '버튼으로 구간을 물어 보세요. 정렬을 바꾸면 구간도 달라져요.',
en: 'Ask for spans with the buttons. A different sort changes the spans.',
});
}
function show(api, rowKey, columnId) {
const span = api.getMergeSpan(rowKey, columnId);
say(span === null
? {
ko: `${rowKey}행 ${columnId}: 병합 아님(isMerged ${api.isMerged(rowKey, columnId)})`,
en: `Row ${rowKey} ${columnId}: not merged (isMerged ${api.isMerged(rowKey, columnId)})`,
}
: {
ko: `${rowKey}행 ${columnId}: ${span.rows}줄, 시작 ${span.start.rowKey}행`,
en: `Row ${rowKey} ${columnId}: ${span.rows} rows, starts at row ${span.start.rowKey}`,
});
}
export const actions = {
dept: (api) => show(api, 2, 'dept'),
team: (api) => show(api, 3, 'team'),
single: (api) => show(api, 4, 'dept'),
sort: (api) => {
api.setSortModel([{ columnId: 'name', direction: 'desc' }]);
show(api, 2, 'dept');
},
reset: (api) => {
api.setSortModel([]);
show(api, 2, 'dept');
},
};<!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="dept">2행 부서 구간</button>
<button type="button" id="team">3행 팀 구간</button>
<button type="button" id="single">4행 부서(한 칸)</button>
<button type="button" id="sort">이름 내림차순 정렬</button>
<button type="button" id="reset">정렬 해제</button>
</div>
<div id="grid" style="height: 330px"></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, dept: '개발', team: '웹', name: '김하늘' },
{ id: 2, dept: '개발', team: '웹', name: '이도윤' },
{ id: 3, dept: '개발', team: '앱', name: '박서연' },
{ id: 4, dept: '운영', team: '인프라', name: '최지우' },
{ id: 5, dept: '디자인', team: '브랜드', name: '정민준' },
{ id: 6, dept: '디자인', team: '브랜드', name: '강서윤' },
];
// getMergeSpan(rowKey, columnId)는 칸이 속한 병합 구간 { rows, cols, start }를, isMerged는 병합 여부를 돌려줘요.
// 구간은 화면에 그려진 줄이 아니라 보이는 행 전체에서 계산하고 정렬·필터를 바꾸면 다시 계산돼요.
const options = {
columns: [
{ id: 'dept', title: '부서', width: 90, merge: true },
{ id: 'team', title: '팀', width: 90, merge: true, mergeWithin: 'dept' },
{ id: 'name', title: '이름', width: 90, merge: true },
],
rows,
rowKey: 'id',
};
let say = () => { };
function setup(_api, log) {
say = log;
log({
ko: '버튼으로 구간을 물어 보세요. 정렬을 바꾸면 구간도 달라져요.',
en: 'Ask for spans with the buttons. A different sort changes the spans.',
});
}
function show(api, rowKey, columnId) {
const span = api.getMergeSpan(rowKey, columnId);
say(span === null
? {
ko: `${rowKey}행 ${columnId}: 병합 아님(isMerged ${api.isMerged(rowKey, columnId)})`,
en: `Row ${rowKey} ${columnId}: not merged (isMerged ${api.isMerged(rowKey, columnId)})`,
}
: {
ko: `${rowKey}행 ${columnId}: ${span.rows}줄, 시작 ${span.start.rowKey}행`,
en: `Row ${rowKey} ${columnId}: ${span.rows} rows, starts at row ${span.start.rowKey}`,
});
}
const actions = {
dept: (api) => show(api, 2, 'dept'),
team: (api) => show(api, 3, 'team'),
single: (api) => show(api, 4, 'dept'),
sort: (api) => {
api.setSortModel([{ columnId: 'name', direction: 'desc' }]);
show(api, 2, 'dept');
},
reset: (api) => {
api.setSortModel([]);
show(api, 2, 'dept');
},
};
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('dept').addEventListener('click', () => actions.dept(api));
document.getElementById('team').addEventListener('click', () => actions.team(api));
document.getElementById('single').addEventListener('click', () => actions.single(api));
document.getElementById('sort').addEventListener('click', () => actions.sort(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(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.dept(api)}
>2행 부서 구간</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.team(api)}
>3행 팀 구간</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.single(api)}
>4행 부서(한 칸)</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.sort(api)}
>이름 내림차순 정렬</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.reset(api)}
>정렬 해제</button>
</div>
<Grid style={{ height: 330 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
dept: string;
team: string;
name: string;
}
const rows: Row[] = [
{ id: 1, dept: '개발', team: '웹', name: '김하늘' },
{ id: 2, dept: '개발', team: '웹', name: '이도윤' },
{ id: 3, dept: '개발', team: '앱', name: '박서연' },
{ id: 4, dept: '운영', team: '인프라', name: '최지우' },
{ id: 5, dept: '디자인', team: '브랜드', name: '정민준' },
{ id: 6, dept: '디자인', team: '브랜드', name: '강서윤' },
];
// getMergeSpan(rowKey, columnId)는 칸이 속한 병합 구간 { rows, cols, start }를, isMerged는 병합 여부를 돌려줘요.
// 구간은 화면에 그려진 줄이 아니라 보이는 행 전체에서 계산하고 정렬·필터를 바꾸면 다시 계산돼요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'dept', title: '부서', width: 90, merge: true },
{ id: 'team', title: '팀', width: 90, merge: true, mergeWithin: 'dept' },
{ id: 'name', title: '이름', width: 90, merge: true },
],
rows,
rowKey: 'id',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
log({
ko: '버튼으로 구간을 물어 보세요. 정렬을 바꾸면 구간도 달라져요.',
en: 'Ask for spans with the buttons. A different sort changes the spans.',
});
}
function show(api: GridApi<Row>, rowKey: number, columnId: string): void {
const span = api.getMergeSpan(rowKey, columnId);
say(
span === null
? {
ko: `${rowKey}행 ${columnId}: 병합 아님(isMerged ${api.isMerged(rowKey, columnId)})`,
en: `Row ${rowKey} ${columnId}: not merged (isMerged ${api.isMerged(rowKey, columnId)})`,
}
: {
ko: `${rowKey}행 ${columnId}: ${span.rows}줄, 시작 ${span.start.rowKey}행`,
en: `Row ${rowKey} ${columnId}: ${span.rows} rows, starts at row ${span.start.rowKey}`,
},
);
}
export const actions = {
dept: (api: GridApi<Row>) => show(api, 2, 'dept'),
team: (api: GridApi<Row>) => show(api, 3, 'team'),
single: (api: GridApi<Row>) => show(api, 4, 'dept'),
sort: (api: GridApi<Row>) => {
api.setSortModel([{ columnId: 'name', direction: 'desc' }]);
show(api, 2, 'dept');
},
reset: (api: GridApi<Row>) => {
api.setSortModel([]);
show(api, 2, 'dept');
},
};버튼을 누르면 이 예제의 코드가 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.dept(api)">2행 부서 구간</button>
<button type="button" :disabled="!api" @click="api && actions.team(api)">3행 팀 구간</button>
<button type="button" :disabled="!api" @click="api && actions.single(api)">4행 부서(한 칸)</button>
<button type="button" :disabled="!api" @click="api && actions.sort(api)">이름 내림차순 정렬</button>
<button type="button" :disabled="!api" @click="api && actions.reset(api)">정렬 해제</button>
</div>
<Grid style="height: 330px" :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;
dept: string;
team: string;
name: string;
}
const rows: Row[] = [
{ id: 1, dept: '개발', team: '웹', name: '김하늘' },
{ id: 2, dept: '개발', team: '웹', name: '이도윤' },
{ id: 3, dept: '개발', team: '앱', name: '박서연' },
{ id: 4, dept: '운영', team: '인프라', name: '최지우' },
{ id: 5, dept: '디자인', team: '브랜드', name: '정민준' },
{ id: 6, dept: '디자인', team: '브랜드', name: '강서윤' },
];
// getMergeSpan(rowKey, columnId)는 칸이 속한 병합 구간 { rows, cols, start }를, isMerged는 병합 여부를 돌려줘요.
// 구간은 화면에 그려진 줄이 아니라 보이는 행 전체에서 계산하고 정렬·필터를 바꾸면 다시 계산돼요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'dept', title: '부서', width: 90, merge: true },
{ id: 'team', title: '팀', width: 90, merge: true, mergeWithin: 'dept' },
{ id: 'name', title: '이름', width: 90, merge: true },
],
rows,
rowKey: 'id',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
log({
ko: '버튼으로 구간을 물어 보세요. 정렬을 바꾸면 구간도 달라져요.',
en: 'Ask for spans with the buttons. A different sort changes the spans.',
});
}
function show(api: GridApi<Row>, rowKey: number, columnId: string): void {
const span = api.getMergeSpan(rowKey, columnId);
say(
span === null
? {
ko: `${rowKey}행 ${columnId}: 병합 아님(isMerged ${api.isMerged(rowKey, columnId)})`,
en: `Row ${rowKey} ${columnId}: not merged (isMerged ${api.isMerged(rowKey, columnId)})`,
}
: {
ko: `${rowKey}행 ${columnId}: ${span.rows}줄, 시작 ${span.start.rowKey}행`,
en: `Row ${rowKey} ${columnId}: ${span.rows} rows, starts at row ${span.start.rowKey}`,
},
);
}
export const actions = {
dept: (api: GridApi<Row>) => show(api, 2, 'dept'),
team: (api: GridApi<Row>) => show(api, 3, 'team'),
single: (api: GridApi<Row>) => show(api, 4, 'dept'),
sort: (api: GridApi<Row>) => {
api.setSortModel([{ columnId: 'name', direction: 'desc' }]);
show(api, 2, 'dept');
},
reset: (api: GridApi<Row>) => {
api.setSortModel([]);
show(api, 2, 'dept');
},
};버튼을 누르면 이 예제의 코드가 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.dept(api)"
>2행 부서 구간</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.team(api)"
>3행 팀 구간</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.single(api)"
>4행 부서(한 칸)</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.sort(api)"
>이름 내림차순 정렬</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.reset(api)"
>정렬 해제</button>
</div>
<mx-grid style="height: 330px" [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 { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
dept: string;
team: string;
name: string;
}
const rows: Row[] = [
{ id: 1, dept: '개발', team: '웹', name: '김하늘' },
{ id: 2, dept: '개발', team: '웹', name: '이도윤' },
{ id: 3, dept: '개발', team: '앱', name: '박서연' },
{ id: 4, dept: '운영', team: '인프라', name: '최지우' },
{ id: 5, dept: '디자인', team: '브랜드', name: '정민준' },
{ id: 6, dept: '디자인', team: '브랜드', name: '강서윤' },
];
// getMergeSpan(rowKey, columnId)는 칸이 속한 병합 구간 { rows, cols, start }를, isMerged는 병합 여부를 돌려줘요.
// 구간은 화면에 그려진 줄이 아니라 보이는 행 전체에서 계산하고 정렬·필터를 바꾸면 다시 계산돼요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'dept', title: '부서', width: 90, merge: true },
{ id: 'team', title: '팀', width: 90, merge: true, mergeWithin: 'dept' },
{ id: 'name', title: '이름', width: 90, merge: true },
],
rows,
rowKey: 'id',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
log({
ko: '버튼으로 구간을 물어 보세요. 정렬을 바꾸면 구간도 달라져요.',
en: 'Ask for spans with the buttons. A different sort changes the spans.',
});
}
function show(api: GridApi<Row>, rowKey: number, columnId: string): void {
const span = api.getMergeSpan(rowKey, columnId);
say(
span === null
? {
ko: `${rowKey}행 ${columnId}: 병합 아님(isMerged ${api.isMerged(rowKey, columnId)})`,
en: `Row ${rowKey} ${columnId}: not merged (isMerged ${api.isMerged(rowKey, columnId)})`,
}
: {
ko: `${rowKey}행 ${columnId}: ${span.rows}줄, 시작 ${span.start.rowKey}행`,
en: `Row ${rowKey} ${columnId}: ${span.rows} rows, starts at row ${span.start.rowKey}`,
},
);
}
export const actions = {
dept: (api: GridApi<Row>) => show(api, 2, 'dept'),
team: (api: GridApi<Row>) => show(api, 3, 'team'),
single: (api: GridApi<Row>) => show(api, 4, 'dept'),
sort: (api: GridApi<Row>) => {
api.setSortModel([{ columnId: 'name', direction: 'desc' }]);
show(api, 2, 'dept');
},
reset: (api: GridApi<Row>) => {
api.setSortModel([]);
show(api, 2, 'dept');
},
};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="dept">Row 2 Dept span</button>
<button type="button" id="team">Row 3 Team span</button>
<button type="button" id="single">Row 4 Dept (single)</button>
<button type="button" id="sort">Sort by name desc</button>
<button type="button" id="reset">Clear sort</button>
</div>
<div id="grid" style="height: 330px"></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('dept').addEventListener('click', () => actions.dept(api));
document.getElementById('team').addEventListener('click', () => actions.team(api));
document.getElementById('single').addEventListener('click', () => actions.single(api));
document.getElementById('sort').addEventListener('click', () => actions.sort(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));const rows = [
{ id: 1, dept: 'Dev', team: 'Web', name: 'Chloe Adams' },
{ id: 2, dept: 'Dev', team: 'Web', name: 'Emma Stone' },
{ id: 3, dept: 'Dev', team: 'App', name: 'Daniel Parker' },
{ id: 4, dept: 'Ops', team: 'Infra', name: 'Olivia Reed' },
{ id: 5, dept: 'Design', team: 'Brand', name: 'Liam Turner' },
{ id: 6, dept: 'Design', team: 'Brand', name: 'Ava Brooks' },
];
// getMergeSpan(rowKey, columnId) returns the merge span { rows, cols, start } the cell belongs to, and isMerged tells whether it is merged.
// Spans are computed over all visible rows, not only the rows drawn on screen, and are recomputed when the sort or filter changes.
export const options = {
columns: [
{ id: 'dept', title: 'Dept', width: 90, merge: true },
{ id: 'team', title: 'Team', width: 90, merge: true, mergeWithin: 'dept' },
{ id: 'name', title: 'Name', width: 110, merge: true },
],
rows,
rowKey: 'id',
};
let say = () => { };
export function setup(_api, log) {
say = log;
log('Ask for spans with the buttons. A different sort changes the spans.');
}
function show(api, rowKey, columnId) {
const span = api.getMergeSpan(rowKey, columnId);
say(span === null
? `Row ${rowKey} ${columnId}: not merged (isMerged ${api.isMerged(rowKey, columnId)})`
: `Row ${rowKey} ${columnId}: ${span.rows} rows, starts at row ${span.start.rowKey}`);
}
export const actions = {
dept: (api) => show(api, 2, 'dept'),
team: (api) => show(api, 3, 'team'),
single: (api) => show(api, 4, 'dept'),
sort: (api) => {
api.setSortModel([{ columnId: 'name', direction: 'desc' }]);
show(api, 2, 'dept');
},
reset: (api) => {
api.setSortModel([]);
show(api, 2, 'dept');
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Ask for merged spans in code</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="dept">Row 2 Dept span</button>
<button type="button" id="team">Row 3 Team span</button>
<button type="button" id="single">Row 4 Dept (single)</button>
<button type="button" id="sort">Sort by name desc</button>
<button type="button" id="reset">Clear sort</button>
</div>
<div id="grid" style="height: 330px"></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, dept: 'Dev', team: 'Web', name: 'Chloe Adams' },
{ id: 2, dept: 'Dev', team: 'Web', name: 'Emma Stone' },
{ id: 3, dept: 'Dev', team: 'App', name: 'Daniel Parker' },
{ id: 4, dept: 'Ops', team: 'Infra', name: 'Olivia Reed' },
{ id: 5, dept: 'Design', team: 'Brand', name: 'Liam Turner' },
{ id: 6, dept: 'Design', team: 'Brand', name: 'Ava Brooks' },
];
// getMergeSpan(rowKey, columnId) returns the merge span { rows, cols, start } the cell belongs to, and isMerged tells whether it is merged.
// Spans are computed over all visible rows, not only the rows drawn on screen, and are recomputed when the sort or filter changes.
const options = {
columns: [
{ id: 'dept', title: 'Dept', width: 90, merge: true },
{ id: 'team', title: 'Team', width: 90, merge: true, mergeWithin: 'dept' },
{ id: 'name', title: 'Name', width: 110, merge: true },
],
rows,
rowKey: 'id',
};
let say = () => { };
function setup(_api, log) {
say = log;
log('Ask for spans with the buttons. A different sort changes the spans.');
}
function show(api, rowKey, columnId) {
const span = api.getMergeSpan(rowKey, columnId);
say(span === null
? `Row ${rowKey} ${columnId}: not merged (isMerged ${api.isMerged(rowKey, columnId)})`
: `Row ${rowKey} ${columnId}: ${span.rows} rows, starts at row ${span.start.rowKey}`);
}
const actions = {
dept: (api) => show(api, 2, 'dept'),
team: (api) => show(api, 3, 'team'),
single: (api) => show(api, 4, 'dept'),
sort: (api) => {
api.setSortModel([{ columnId: 'name', direction: 'desc' }]);
show(api, 2, 'dept');
},
reset: (api) => {
api.setSortModel([]);
show(api, 2, 'dept');
},
};
options.ariaLabel = "Ask for merged spans in code";
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('dept').addEventListener('click', () => actions.dept(api));
document.getElementById('team').addEventListener('click', () => actions.team(api));
document.getElementById('single').addEventListener('click', () => actions.single(api));
document.getElementById('sort').addEventListener('click', () => actions.sort(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(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.dept(api)}
>Row 2 Dept span</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.team(api)}
>Row 3 Team span</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.single(api)}
>Row 4 Dept (single)</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.sort(api)}
>Sort by name desc</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.reset(api)}
>Clear sort</button>
</div>
<Grid style={{ height: 330 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
dept: string;
team: string;
name: string;
}
const rows: Row[] = [
{ id: 1, dept: 'Dev', team: 'Web', name: 'Chloe Adams' },
{ id: 2, dept: 'Dev', team: 'Web', name: 'Emma Stone' },
{ id: 3, dept: 'Dev', team: 'App', name: 'Daniel Parker' },
{ id: 4, dept: 'Ops', team: 'Infra', name: 'Olivia Reed' },
{ id: 5, dept: 'Design', team: 'Brand', name: 'Liam Turner' },
{ id: 6, dept: 'Design', team: 'Brand', name: 'Ava Brooks' },
];
// getMergeSpan(rowKey, columnId) returns the merge span { rows, cols, start } the cell belongs to, and isMerged tells whether it is merged.
// Spans are computed over all visible rows, not only the rows drawn on screen, and are recomputed when the sort or filter changes.
export const options: GridOptions<Row> = {
columns: [
{ id: 'dept', title: 'Dept', width: 90, merge: true },
{ id: 'team', title: 'Team', width: 90, merge: true, mergeWithin: 'dept' },
{ id: 'name', title: 'Name', width: 110, merge: true },
],
rows,
rowKey: 'id',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
log('Ask for spans with the buttons. A different sort changes the spans.');
}
function show(api: GridApi<Row>, rowKey: number, columnId: string): void {
const span = api.getMergeSpan(rowKey, columnId);
say(
span === null
? `Row ${rowKey} ${columnId}: not merged (isMerged ${api.isMerged(rowKey, columnId)})`
: `Row ${rowKey} ${columnId}: ${span.rows} rows, starts at row ${span.start.rowKey}`,
);
}
export const actions = {
dept: (api: GridApi<Row>) => show(api, 2, 'dept'),
team: (api: GridApi<Row>) => show(api, 3, 'team'),
single: (api: GridApi<Row>) => show(api, 4, 'dept'),
sort: (api: GridApi<Row>) => {
api.setSortModel([{ columnId: 'name', direction: 'desc' }]);
show(api, 2, 'dept');
},
reset: (api: GridApi<Row>) => {
api.setSortModel([]);
show(api, 2, 'dept');
},
};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.dept(api)">Row 2 Dept span</button>
<button type="button" :disabled="!api" @click="api && actions.team(api)">Row 3 Team span</button>
<button type="button" :disabled="!api" @click="api && actions.single(api)">Row 4 Dept (single)</button>
<button type="button" :disabled="!api" @click="api && actions.sort(api)">Sort by name desc</button>
<button type="button" :disabled="!api" @click="api && actions.reset(api)">Clear sort</button>
</div>
<Grid style="height: 330px" :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;
dept: string;
team: string;
name: string;
}
const rows: Row[] = [
{ id: 1, dept: 'Dev', team: 'Web', name: 'Chloe Adams' },
{ id: 2, dept: 'Dev', team: 'Web', name: 'Emma Stone' },
{ id: 3, dept: 'Dev', team: 'App', name: 'Daniel Parker' },
{ id: 4, dept: 'Ops', team: 'Infra', name: 'Olivia Reed' },
{ id: 5, dept: 'Design', team: 'Brand', name: 'Liam Turner' },
{ id: 6, dept: 'Design', team: 'Brand', name: 'Ava Brooks' },
];
// getMergeSpan(rowKey, columnId) returns the merge span { rows, cols, start } the cell belongs to, and isMerged tells whether it is merged.
// Spans are computed over all visible rows, not only the rows drawn on screen, and are recomputed when the sort or filter changes.
export const options: GridOptions<Row> = {
columns: [
{ id: 'dept', title: 'Dept', width: 90, merge: true },
{ id: 'team', title: 'Team', width: 90, merge: true, mergeWithin: 'dept' },
{ id: 'name', title: 'Name', width: 110, merge: true },
],
rows,
rowKey: 'id',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
log('Ask for spans with the buttons. A different sort changes the spans.');
}
function show(api: GridApi<Row>, rowKey: number, columnId: string): void {
const span = api.getMergeSpan(rowKey, columnId);
say(
span === null
? `Row ${rowKey} ${columnId}: not merged (isMerged ${api.isMerged(rowKey, columnId)})`
: `Row ${rowKey} ${columnId}: ${span.rows} rows, starts at row ${span.start.rowKey}`,
);
}
export const actions = {
dept: (api: GridApi<Row>) => show(api, 2, 'dept'),
team: (api: GridApi<Row>) => show(api, 3, 'team'),
single: (api: GridApi<Row>) => show(api, 4, 'dept'),
sort: (api: GridApi<Row>) => {
api.setSortModel([{ columnId: 'name', direction: 'desc' }]);
show(api, 2, 'dept');
},
reset: (api: GridApi<Row>) => {
api.setSortModel([]);
show(api, 2, 'dept');
},
};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.dept(api)"
>Row 2 Dept span</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.team(api)"
>Row 3 Team span</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.single(api)"
>Row 4 Dept (single)</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.sort(api)"
>Sort by name desc</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.reset(api)"
>Clear sort</button>
</div>
<mx-grid style="height: 330px" [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 { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
dept: string;
team: string;
name: string;
}
const rows: Row[] = [
{ id: 1, dept: 'Dev', team: 'Web', name: 'Chloe Adams' },
{ id: 2, dept: 'Dev', team: 'Web', name: 'Emma Stone' },
{ id: 3, dept: 'Dev', team: 'App', name: 'Daniel Parker' },
{ id: 4, dept: 'Ops', team: 'Infra', name: 'Olivia Reed' },
{ id: 5, dept: 'Design', team: 'Brand', name: 'Liam Turner' },
{ id: 6, dept: 'Design', team: 'Brand', name: 'Ava Brooks' },
];
// getMergeSpan(rowKey, columnId) returns the merge span { rows, cols, start } the cell belongs to, and isMerged tells whether it is merged.
// Spans are computed over all visible rows, not only the rows drawn on screen, and are recomputed when the sort or filter changes.
export const options: GridOptions<Row> = {
columns: [
{ id: 'dept', title: 'Dept', width: 90, merge: true },
{ id: 'team', title: 'Team', width: 90, merge: true, mergeWithin: 'dept' },
{ id: 'name', title: 'Name', width: 110, merge: true },
],
rows,
rowKey: 'id',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
log('Ask for spans with the buttons. A different sort changes the spans.');
}
function show(api: GridApi<Row>, rowKey: number, columnId: string): void {
const span = api.getMergeSpan(rowKey, columnId);
say(
span === null
? `Row ${rowKey} ${columnId}: not merged (isMerged ${api.isMerged(rowKey, columnId)})`
: `Row ${rowKey} ${columnId}: ${span.rows} rows, starts at row ${span.start.rowKey}`,
);
}
export const actions = {
dept: (api: GridApi<Row>) => show(api, 2, 'dept'),
team: (api: GridApi<Row>) => show(api, 3, 'team'),
single: (api: GridApi<Row>) => show(api, 4, 'dept'),
sort: (api: GridApi<Row>) => {
api.setSortModel([{ columnId: 'name', direction: 'desc' }]);
show(api, 2, 'dept');
},
reset: (api: GridApi<Row>) => {
api.setSortModel([]);
show(api, 2, 'dept');
},
};