같은 값은 세로로 합쳐 보여 주기Merge equal values down a column
위아래로 같은 값이 이어지면 한 칸으로 합쳐요. 합치지 않고 첫 줄에만 값을 보이게 할 수도 있고, 어느 쪽이든 값 위치를 첫 줄이나 가운데로 고를 수 있어요.Equal values stacked in a column become one cell, or keep the cells and show the value only on the first row. Either way, put the value on the first row or in the center.
코드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="mode">
<option value="merge">한 칸으로 합치기</option>
<option value="top">첫 줄에만 값 보이기</option>
<option value="off">합치지 않기</option>
</select>
<select aria-label="값 위치" id="label">
<option value="start">첫 줄</option>
<option value="center" selected>보이는 구간 가운데</option>
</select>
<select aria-label="정렬 기준" id="sort">
<option value="none" selected>정렬 안 함</option>
<option value="region">지역순</option>
<option value="category">분류순</option>
<option value="amount">출하량 큰 순</option>
</select>
</div>
<div id="grid" style="height: 400px"></div>
</div>
<script type="module" src="./main.js"></script>import { createGrid } from 'maxgrid';
import { actions, options } from './example';
const api = createGrid(document.getElementById('grid'), options);
document.getElementById('mode').addEventListener('change', (event) => actions.mode(api, event.target.value));
document.getElementById('label').addEventListener('change', (event) => actions.label(api, event.target.value));
document.getElementById('sort').addEventListener('change', (event) => actions.sort(api, event.target.value));// 고른 방식을 기억해 두었다가, 바뀔 때마다 같은 방식으로 컬럼을 다시 넣어요.
let mode = 'merge';
let label = 'center';
// merge: true → 위아래로 같은 값이면 한 칸으로 합쳐요.
// merge: 'top' → 칸은 그대로 두고 구간의 첫 줄에만 값을 보여 줘요.
// mergeLabel → 두 방식 모두에서 값 위치를 골라요. 'start'는 구간의 첫 줄, 'center'는 보이는 구간의 가운데예요.
const columns = () => {
const merge = mode === 'off' ? undefined : mode === 'top' ? 'top' : true;
const on = merge === undefined ? {} : { merge, mergeLabel: label };
return [
{ id: 'region', title: '지역', width: 110, ...on },
{ id: 'category', title: '분류', width: 110, ...on },
{ id: 'item', title: '품목', width: 130 },
{ id: 'amount', title: '출하량(톤)', kind: 'number', width: 120, align: 'end' },
];
};
export const options = {
columns: columns(),
headerGroups: [
{ id: 'where', title: '어디서', children: ['region', 'category'] },
{ id: 'what', title: '얼마나', children: ['item', 'amount'] },
],
rows: [
{ id: 1, region: '경기', category: '과일', item: '사과', amount: 120 },
{ id: 2, region: '경기', category: '과일', item: '배', amount: 90 },
{ id: 3, region: '경기', category: '곡물', item: '쌀', amount: 300 },
{ id: 4, region: '전남', category: '과일', item: '감귤', amount: 210 },
{ id: 5, region: '전남', category: '곡물', item: '쌀', amount: 280 },
{ id: 6, region: '전남', category: '곡물', item: '옥수수', amount: 130 },
{ id: 7, region: '강원', category: '채소', item: '배추', amount: 175 },
{ id: 8, region: '강원', category: '채소', item: '감자', amount: 240 },
{ id: 9, region: '제주', category: '과일', item: '한라봉', amount: 85 },
{ id: 10, region: '제주', category: '채소', item: '당근', amount: 140 },
],
rowKey: 'id',
};
export const actions = {
mode: (api, value) => {
mode = value === 'top' || value === 'off' ? value : 'merge';
api.setColumns(columns());
},
label: (api, value) => {
label = value === 'start' ? 'start' : 'center';
api.setColumns(columns());
},
// 합치는 건 "이웃한 줄끼리 같은 값"일 때만이에요. 정렬 기준을 바꾸면 이웃이 바뀌어서 합쳐지는 컬럼도 바뀌어요.
sort: (api, value) => api.setSortModel(value === 'region' || value === 'category'
? [{ columnId: value, direction: 'asc' }]
: value === 'amount'
? [{ columnId: 'amount', direction: 'desc' }]
: []),
};<!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="mode">
<option value="merge">한 칸으로 합치기</option>
<option value="top">첫 줄에만 값 보이기</option>
<option value="off">합치지 않기</option>
</select>
<select aria-label="값 위치" id="label">
<option value="start">첫 줄</option>
<option value="center" selected>보이는 구간 가운데</option>
</select>
<select aria-label="정렬 기준" id="sort">
<option value="none" selected>정렬 안 함</option>
<option value="region">지역순</option>
<option value="category">분류순</option>
<option value="amount">출하량 큰 순</option>
</select>
</div>
<div id="grid" style="height: 400px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
// 고른 방식을 기억해 두었다가, 바뀔 때마다 같은 방식으로 컬럼을 다시 넣어요.
let mode = 'merge';
let label = 'center';
// merge: true → 위아래로 같은 값이면 한 칸으로 합쳐요.
// merge: 'top' → 칸은 그대로 두고 구간의 첫 줄에만 값을 보여 줘요.
// mergeLabel → 두 방식 모두에서 값 위치를 골라요. 'start'는 구간의 첫 줄, 'center'는 보이는 구간의 가운데예요.
const columns = () => {
const merge = mode === 'off' ? undefined : mode === 'top' ? 'top' : true;
const on = merge === undefined ? {} : { merge, mergeLabel: label };
return [
{ id: 'region', title: '지역', width: 110, ...on },
{ id: 'category', title: '분류', width: 110, ...on },
{ id: 'item', title: '품목', width: 130 },
{ id: 'amount', title: '출하량(톤)', kind: 'number', width: 120, align: 'end' },
];
};
const options = {
columns: columns(),
headerGroups: [
{ id: 'where', title: '어디서', children: ['region', 'category'] },
{ id: 'what', title: '얼마나', children: ['item', 'amount'] },
],
rows: [
{ id: 1, region: '경기', category: '과일', item: '사과', amount: 120 },
{ id: 2, region: '경기', category: '과일', item: '배', amount: 90 },
{ id: 3, region: '경기', category: '곡물', item: '쌀', amount: 300 },
{ id: 4, region: '전남', category: '과일', item: '감귤', amount: 210 },
{ id: 5, region: '전남', category: '곡물', item: '쌀', amount: 280 },
{ id: 6, region: '전남', category: '곡물', item: '옥수수', amount: 130 },
{ id: 7, region: '강원', category: '채소', item: '배추', amount: 175 },
{ id: 8, region: '강원', category: '채소', item: '감자', amount: 240 },
{ id: 9, region: '제주', category: '과일', item: '한라봉', amount: 85 },
{ id: 10, region: '제주', category: '채소', item: '당근', amount: 140 },
],
rowKey: 'id',
};
const actions = {
mode: (api, value) => {
mode = value === 'top' || value === 'off' ? value : 'merge';
api.setColumns(columns());
},
label: (api, value) => {
label = value === 'start' ? 'start' : 'center';
api.setColumns(columns());
},
// 합치는 건 "이웃한 줄끼리 같은 값"일 때만이에요. 정렬 기준을 바꾸면 이웃이 바뀌어서 합쳐지는 컬럼도 바뀌어요.
sort: (api, value) => api.setSortModel(value === 'region' || value === 'category'
? [{ columnId: value, direction: 'asc' }]
: value === 'amount'
? [{ columnId: 'amount', direction: 'desc' }]
: []),
};
options.ariaLabel = "같은 값은 세로로 합쳐 보여 주기";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
document.getElementById('mode').addEventListener('change', (event) => actions.mode(api, event.target.value));
document.getElementById('label').addEventListener('change', (event) => actions.label(api, event.target.value));
document.getElementById('sort').addEventListener('change', (event) => actions.sort(api, event.target.value));
</script>
</body>
</html>버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
return (
<>
<div className="toolbar">
<select
aria-label="방식"
disabled={!api}
onChange={(event) => api && actions.mode(api, event.target.value)}
>
<option value="merge">한 칸으로 합치기</option>
<option value="top">첫 줄에만 값 보이기</option>
<option value="off">합치지 않기</option>
</select>
<select
aria-label="값 위치"
disabled={!api}
onChange={(event) => api && actions.label(api, event.target.value)}
>
<option value="start">첫 줄</option>
<option value="center">보이는 구간 가운데</option>
</select>
<select defaultValue="center"
aria-label="정렬 기준"
disabled={!api}
onChange={(event) => api && actions.sort(api, event.target.value)}
>
<option value="none">정렬 안 함</option>
<option value="region">지역순</option>
<option value="category">분류순</option>
<option value="amount">출하량 큰 순</option>
</select>
</div>
<Grid style={{ height: 400 }} ref={gridRef} options={options} />
</>
);
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
region: string;
category: string;
item: string;
amount: number;
}
type Mode = 'merge' | 'top' | 'off';
type Label = 'start' | 'center';
// 고른 방식을 기억해 두었다가, 바뀔 때마다 같은 방식으로 컬럼을 다시 넣어요.
let mode: Mode = 'merge';
let label: Label = 'center';
// merge: true → 위아래로 같은 값이면 한 칸으로 합쳐요.
// merge: 'top' → 칸은 그대로 두고 구간의 첫 줄에만 값을 보여 줘요.
// mergeLabel → 두 방식 모두에서 값 위치를 골라요. 'start'는 구간의 첫 줄, 'center'는 보이는 구간의 가운데예요.
const columns = (): ColumnDef<Row>[] => {
const merge = mode === 'off' ? undefined : mode === 'top' ? ('top' as const) : true;
const on = merge === undefined ? {} : { merge, mergeLabel: label };
return [
{ id: 'region', title: '지역', width: 110, ...on },
{ id: 'category', title: '분류', width: 110, ...on },
{ id: 'item', title: '품목', width: 130 },
{ id: 'amount', title: '출하량(톤)', kind: 'number', width: 120, align: 'end' },
];
};
export const options: GridOptions<Row> = {
columns: columns(),
headerGroups: [
{ id: 'where', title: '어디서', children: ['region', 'category'] },
{ id: 'what', title: '얼마나', children: ['item', 'amount'] },
],
rows: [
{ id: 1, region: '경기', category: '과일', item: '사과', amount: 120 },
{ id: 2, region: '경기', category: '과일', item: '배', amount: 90 },
{ id: 3, region: '경기', category: '곡물', item: '쌀', amount: 300 },
{ id: 4, region: '전남', category: '과일', item: '감귤', amount: 210 },
{ id: 5, region: '전남', category: '곡물', item: '쌀', amount: 280 },
{ id: 6, region: '전남', category: '곡물', item: '옥수수', amount: 130 },
{ id: 7, region: '강원', category: '채소', item: '배추', amount: 175 },
{ id: 8, region: '강원', category: '채소', item: '감자', amount: 240 },
{ id: 9, region: '제주', category: '과일', item: '한라봉', amount: 85 },
{ id: 10, region: '제주', category: '채소', item: '당근', amount: 140 },
],
rowKey: 'id',
};
export const actions = {
mode: (api: GridApi<Row>, value?: string) => {
mode = value === 'top' || value === 'off' ? value : 'merge';
api.setColumns(columns());
},
label: (api: GridApi<Row>, value?: string) => {
label = value === 'start' ? 'start' : 'center';
api.setColumns(columns());
},
// 합치는 건 "이웃한 줄끼리 같은 값"일 때만이에요. 정렬 기준을 바꾸면 이웃이 바뀌어서 합쳐지는 컬럼도 바뀌어요.
sort: (api: GridApi<Row>, value?: string) =>
api.setSortModel(
value === 'region' || value === 'category'
? [{ columnId: value, direction: 'asc' }]
: value === 'amount'
? [{ columnId: 'amount', direction: 'desc' }]
: [],
),
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<script setup lang="ts">
import { shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
}
</script>
<template>
<div class="toolbar">
<select
aria-label="방식"
:disabled="!api"
@change="api && actions.mode(api, ($event.target as HTMLSelectElement).value)"
>
<option value="merge">한 칸으로 합치기</option>
<option value="top">첫 줄에만 값 보이기</option>
<option value="off">합치지 않기</option>
</select>
<select
aria-label="값 위치"
:disabled="!api"
@change="api && actions.label(api, ($event.target as HTMLSelectElement).value)"
>
<option value="start">첫 줄</option>
<option value="center" selected>보이는 구간 가운데</option>
</select>
<select
aria-label="정렬 기준"
:disabled="!api"
@change="api && actions.sort(api, ($event.target as HTMLSelectElement).value)"
>
<option value="none" selected>정렬 안 함</option>
<option value="region">지역순</option>
<option value="category">분류순</option>
<option value="amount">출하량 큰 순</option>
</select>
</div>
<Grid style="height: 400px" :options="options" @ready="onReady" />
</template>import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
region: string;
category: string;
item: string;
amount: number;
}
type Mode = 'merge' | 'top' | 'off';
type Label = 'start' | 'center';
// 고른 방식을 기억해 두었다가, 바뀔 때마다 같은 방식으로 컬럼을 다시 넣어요.
let mode: Mode = 'merge';
let label: Label = 'center';
// merge: true → 위아래로 같은 값이면 한 칸으로 합쳐요.
// merge: 'top' → 칸은 그대로 두고 구간의 첫 줄에만 값을 보여 줘요.
// mergeLabel → 두 방식 모두에서 값 위치를 골라요. 'start'는 구간의 첫 줄, 'center'는 보이는 구간의 가운데예요.
const columns = (): ColumnDef<Row>[] => {
const merge = mode === 'off' ? undefined : mode === 'top' ? ('top' as const) : true;
const on = merge === undefined ? {} : { merge, mergeLabel: label };
return [
{ id: 'region', title: '지역', width: 110, ...on },
{ id: 'category', title: '분류', width: 110, ...on },
{ id: 'item', title: '품목', width: 130 },
{ id: 'amount', title: '출하량(톤)', kind: 'number', width: 120, align: 'end' },
];
};
export const options: GridOptions<Row> = {
columns: columns(),
headerGroups: [
{ id: 'where', title: '어디서', children: ['region', 'category'] },
{ id: 'what', title: '얼마나', children: ['item', 'amount'] },
],
rows: [
{ id: 1, region: '경기', category: '과일', item: '사과', amount: 120 },
{ id: 2, region: '경기', category: '과일', item: '배', amount: 90 },
{ id: 3, region: '경기', category: '곡물', item: '쌀', amount: 300 },
{ id: 4, region: '전남', category: '과일', item: '감귤', amount: 210 },
{ id: 5, region: '전남', category: '곡물', item: '쌀', amount: 280 },
{ id: 6, region: '전남', category: '곡물', item: '옥수수', amount: 130 },
{ id: 7, region: '강원', category: '채소', item: '배추', amount: 175 },
{ id: 8, region: '강원', category: '채소', item: '감자', amount: 240 },
{ id: 9, region: '제주', category: '과일', item: '한라봉', amount: 85 },
{ id: 10, region: '제주', category: '채소', item: '당근', amount: 140 },
],
rowKey: 'id',
};
export const actions = {
mode: (api: GridApi<Row>, value?: string) => {
mode = value === 'top' || value === 'off' ? value : 'merge';
api.setColumns(columns());
},
label: (api: GridApi<Row>, value?: string) => {
label = value === 'start' ? 'start' : 'center';
api.setColumns(columns());
},
// 합치는 건 "이웃한 줄끼리 같은 값"일 때만이에요. 정렬 기준을 바꾸면 이웃이 바뀌어서 합쳐지는 컬럼도 바뀌어요.
sort: (api: GridApi<Row>, value?: string) =>
api.setSortModel(
value === 'region' || value === 'category'
? [{ columnId: value, direction: 'asc' }]
: value === 'amount'
? [{ columnId: 'amount', direction: 'desc' }]
: [],
),
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Component } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, 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.mode(api, $any($event.target).value)"
>
<option value="merge">한 칸으로 합치기</option>
<option value="top">첫 줄에만 값 보이기</option>
<option value="off">합치지 않기</option>
</select>
<select
aria-label="값 위치"
[disabled]="!api"
(change)="api && actions.label(api, $any($event.target).value)"
>
<option value="start">첫 줄</option>
<option value="center" selected>보이는 구간 가운데</option>
</select>
<select
aria-label="정렬 기준"
[disabled]="!api"
(change)="api && actions.sort(api, $any($event.target).value)"
>
<option value="none" selected>정렬 안 함</option>
<option value="region">지역순</option>
<option value="category">분류순</option>
<option value="amount">출하량 큰 순</option>
</select>
</div>
<mx-grid style="height: 400px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
}
}
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/angular';
export interface Row {
id: number;
region: string;
category: string;
item: string;
amount: number;
}
type Mode = 'merge' | 'top' | 'off';
type Label = 'start' | 'center';
// 고른 방식을 기억해 두었다가, 바뀔 때마다 같은 방식으로 컬럼을 다시 넣어요.
let mode: Mode = 'merge';
let label: Label = 'center';
// merge: true → 위아래로 같은 값이면 한 칸으로 합쳐요.
// merge: 'top' → 칸은 그대로 두고 구간의 첫 줄에만 값을 보여 줘요.
// mergeLabel → 두 방식 모두에서 값 위치를 골라요. 'start'는 구간의 첫 줄, 'center'는 보이는 구간의 가운데예요.
const columns = (): ColumnDef<Row>[] => {
const merge = mode === 'off' ? undefined : mode === 'top' ? ('top' as const) : true;
const on = merge === undefined ? {} : { merge, mergeLabel: label };
return [
{ id: 'region', title: '지역', width: 110, ...on },
{ id: 'category', title: '분류', width: 110, ...on },
{ id: 'item', title: '품목', width: 130 },
{ id: 'amount', title: '출하량(톤)', kind: 'number', width: 120, align: 'end' },
];
};
export const options: GridOptions<Row> = {
columns: columns(),
headerGroups: [
{ id: 'where', title: '어디서', children: ['region', 'category'] },
{ id: 'what', title: '얼마나', children: ['item', 'amount'] },
],
rows: [
{ id: 1, region: '경기', category: '과일', item: '사과', amount: 120 },
{ id: 2, region: '경기', category: '과일', item: '배', amount: 90 },
{ id: 3, region: '경기', category: '곡물', item: '쌀', amount: 300 },
{ id: 4, region: '전남', category: '과일', item: '감귤', amount: 210 },
{ id: 5, region: '전남', category: '곡물', item: '쌀', amount: 280 },
{ id: 6, region: '전남', category: '곡물', item: '옥수수', amount: 130 },
{ id: 7, region: '강원', category: '채소', item: '배추', amount: 175 },
{ id: 8, region: '강원', category: '채소', item: '감자', amount: 240 },
{ id: 9, region: '제주', category: '과일', item: '한라봉', amount: 85 },
{ id: 10, region: '제주', category: '채소', item: '당근', amount: 140 },
],
rowKey: 'id',
};
export const actions = {
mode: (api: GridApi<Row>, value?: string) => {
mode = value === 'top' || value === 'off' ? value : 'merge';
api.setColumns(columns());
},
label: (api: GridApi<Row>, value?: string) => {
label = value === 'start' ? 'start' : 'center';
api.setColumns(columns());
},
// 합치는 건 "이웃한 줄끼리 같은 값"일 때만이에요. 정렬 기준을 바꾸면 이웃이 바뀌어서 합쳐지는 컬럼도 바뀌어요.
sort: (api: GridApi<Row>, value?: string) =>
api.setSortModel(
value === 'region' || value === 'category'
? [{ columnId: value, direction: 'asc' }]
: value === 'amount'
? [{ columnId: 'amount', direction: 'desc' }]
: [],
),
};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="Mode" id="mode">
<option value="merge">Merge into one cell</option>
<option value="top">Show on first row only</option>
<option value="off">No merging</option>
</select>
<select aria-label="Label position" id="label">
<option value="start">First row</option>
<option value="center" selected>Center of the visible span</option>
</select>
<select aria-label="Sort by" id="sort">
<option value="none" selected>No sorting</option>
<option value="region">Region</option>
<option value="category">Category</option>
<option value="amount">Amount, high to low</option>
</select>
</div>
<div id="grid" style="height: 400px"></div>
</div>
<script type="module" src="./main.js"></script>import { createGrid } from 'maxgrid';
import { actions, options } from './example';
const api = createGrid(document.getElementById('grid'), options);
document.getElementById('mode').addEventListener('change', (event) => actions.mode(api, event.target.value));
document.getElementById('label').addEventListener('change', (event) => actions.label(api, event.target.value));
document.getElementById('sort').addEventListener('change', (event) => actions.sort(api, event.target.value));// Remember the chosen mode and set the columns again the same way whenever it changes.
let mode = 'merge';
let label = 'center';
// merge: true -> equal values in consecutive rows merge into one cell.
// merge: 'top' -> cells stay as they are and only the first row of a run shows the value.
// mergeLabel -> picks where the value goes in both modes. 'start' is the first row of the run, 'center' is the middle of the visible run.
const columns = () => {
const merge = mode === 'off' ? undefined : mode === 'top' ? 'top' : true;
const on = merge === undefined ? {} : { merge, mergeLabel: label };
return [
{ id: 'region', title: 'Region', width: 110, ...on },
{ id: 'category', title: 'Category', width: 110, ...on },
{ id: 'item', title: 'Item', width: 130 },
{ id: 'amount', title: 'Shipped (tons)', kind: 'number', width: 130, align: 'end' },
];
};
export const options = {
columns: columns(),
headerGroups: [
{ id: 'where', title: 'Where', children: ['region', 'category'] },
{ id: 'what', title: 'How much', children: ['item', 'amount'] },
],
rows: [
{ id: 1, region: 'Texas', category: 'Fruit', item: 'Apple', amount: 120 },
{ id: 2, region: 'Texas', category: 'Fruit', item: 'Pear', amount: 90 },
{ id: 3, region: 'Texas', category: 'Grain', item: 'Rice', amount: 300 },
{ id: 4, region: 'Florida', category: 'Fruit', item: 'Orange', amount: 210 },
{ id: 5, region: 'Florida', category: 'Grain', item: 'Rice', amount: 280 },
{ id: 6, region: 'Florida', category: 'Grain', item: 'Corn', amount: 130 },
{ id: 7, region: 'Oregon', category: 'Vegetable', item: 'Cabbage', amount: 175 },
{ id: 8, region: 'Oregon', category: 'Vegetable', item: 'Potato', amount: 240 },
{ id: 9, region: 'Hawaii', category: 'Fruit', item: 'Pineapple', amount: 85 },
{ id: 10, region: 'Hawaii', category: 'Vegetable', item: 'Carrot', amount: 140 },
],
rowKey: 'id',
};
export const actions = {
mode: (api, value) => {
mode = value === 'top' || value === 'off' ? value : 'merge';
api.setColumns(columns());
},
label: (api, value) => {
label = value === 'start' ? 'start' : 'center';
api.setColumns(columns());
},
// Cells merge only when "neighboring rows have the same value". Changing the sort changes the neighbors, so which columns merge changes too.
sort: (api, value) => api.setSortModel(value === 'region' || value === 'category'
? [{ columnId: value, direction: 'asc' }]
: value === 'amount'
? [{ columnId: 'amount', direction: 'desc' }]
: []),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Merge equal values down a column</title>
</head>
<body>
<div class="toolbar">
<select aria-label="Mode" id="mode">
<option value="merge">Merge into one cell</option>
<option value="top">Show on first row only</option>
<option value="off">No merging</option>
</select>
<select aria-label="Label position" id="label">
<option value="start">First row</option>
<option value="center" selected>Center of the visible span</option>
</select>
<select aria-label="Sort by" id="sort">
<option value="none" selected>No sorting</option>
<option value="region">Region</option>
<option value="category">Category</option>
<option value="amount">Amount, high to low</option>
</select>
</div>
<div id="grid" style="height: 400px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
// Remember the chosen mode and set the columns again the same way whenever it changes.
let mode = 'merge';
let label = 'center';
// merge: true -> equal values in consecutive rows merge into one cell.
// merge: 'top' -> cells stay as they are and only the first row of a run shows the value.
// mergeLabel -> picks where the value goes in both modes. 'start' is the first row of the run, 'center' is the middle of the visible run.
const columns = () => {
const merge = mode === 'off' ? undefined : mode === 'top' ? 'top' : true;
const on = merge === undefined ? {} : { merge, mergeLabel: label };
return [
{ id: 'region', title: 'Region', width: 110, ...on },
{ id: 'category', title: 'Category', width: 110, ...on },
{ id: 'item', title: 'Item', width: 130 },
{ id: 'amount', title: 'Shipped (tons)', kind: 'number', width: 130, align: 'end' },
];
};
const options = {
columns: columns(),
headerGroups: [
{ id: 'where', title: 'Where', children: ['region', 'category'] },
{ id: 'what', title: 'How much', children: ['item', 'amount'] },
],
rows: [
{ id: 1, region: 'Texas', category: 'Fruit', item: 'Apple', amount: 120 },
{ id: 2, region: 'Texas', category: 'Fruit', item: 'Pear', amount: 90 },
{ id: 3, region: 'Texas', category: 'Grain', item: 'Rice', amount: 300 },
{ id: 4, region: 'Florida', category: 'Fruit', item: 'Orange', amount: 210 },
{ id: 5, region: 'Florida', category: 'Grain', item: 'Rice', amount: 280 },
{ id: 6, region: 'Florida', category: 'Grain', item: 'Corn', amount: 130 },
{ id: 7, region: 'Oregon', category: 'Vegetable', item: 'Cabbage', amount: 175 },
{ id: 8, region: 'Oregon', category: 'Vegetable', item: 'Potato', amount: 240 },
{ id: 9, region: 'Hawaii', category: 'Fruit', item: 'Pineapple', amount: 85 },
{ id: 10, region: 'Hawaii', category: 'Vegetable', item: 'Carrot', amount: 140 },
],
rowKey: 'id',
};
const actions = {
mode: (api, value) => {
mode = value === 'top' || value === 'off' ? value : 'merge';
api.setColumns(columns());
},
label: (api, value) => {
label = value === 'start' ? 'start' : 'center';
api.setColumns(columns());
},
// Cells merge only when "neighboring rows have the same value". Changing the sort changes the neighbors, so which columns merge changes too.
sort: (api, value) => api.setSortModel(value === 'region' || value === 'category'
? [{ columnId: value, direction: 'asc' }]
: value === 'amount'
? [{ columnId: 'amount', direction: 'desc' }]
: []),
};
options.ariaLabel = "Merge equal values down a column";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
document.getElementById('mode').addEventListener('change', (event) => actions.mode(api, event.target.value));
document.getElementById('label').addEventListener('change', (event) => actions.label(api, event.target.value));
document.getElementById('sort').addEventListener('change', (event) => actions.sort(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 { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
return (
<>
<div className="toolbar">
<select
aria-label="Mode"
disabled={!api}
onChange={(event) => api && actions.mode(api, event.target.value)}
>
<option value="merge">Merge into one cell</option>
<option value="top">Show on first row only</option>
<option value="off">No merging</option>
</select>
<select
aria-label="Label position"
disabled={!api}
onChange={(event) => api && actions.label(api, event.target.value)}
>
<option value="start">First row</option>
<option value="center">Center of the visible span</option>
</select>
<select defaultValue="center"
aria-label="Sort by"
disabled={!api}
onChange={(event) => api && actions.sort(api, event.target.value)}
>
<option value="none">No sorting</option>
<option value="region">Region</option>
<option value="category">Category</option>
<option value="amount">Amount, high to low</option>
</select>
</div>
<Grid style={{ height: 400 }} ref={gridRef} options={options} />
</>
);
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
region: string;
category: string;
item: string;
amount: number;
}
type Mode = 'merge' | 'top' | 'off';
type Label = 'start' | 'center';
// Remember the chosen mode and set the columns again the same way whenever it changes.
let mode: Mode = 'merge';
let label: Label = 'center';
// merge: true -> equal values in consecutive rows merge into one cell.
// merge: 'top' -> cells stay as they are and only the first row of a run shows the value.
// mergeLabel -> picks where the value goes in both modes. 'start' is the first row of the run, 'center' is the middle of the visible run.
const columns = (): ColumnDef<Row>[] => {
const merge = mode === 'off' ? undefined : mode === 'top' ? ('top' as const) : true;
const on = merge === undefined ? {} : { merge, mergeLabel: label };
return [
{ id: 'region', title: 'Region', width: 110, ...on },
{ id: 'category', title: 'Category', width: 110, ...on },
{ id: 'item', title: 'Item', width: 130 },
{ id: 'amount', title: 'Shipped (tons)', kind: 'number', width: 130, align: 'end' },
];
};
export const options: GridOptions<Row> = {
columns: columns(),
headerGroups: [
{ id: 'where', title: 'Where', children: ['region', 'category'] },
{ id: 'what', title: 'How much', children: ['item', 'amount'] },
],
rows: [
{ id: 1, region: 'Texas', category: 'Fruit', item: 'Apple', amount: 120 },
{ id: 2, region: 'Texas', category: 'Fruit', item: 'Pear', amount: 90 },
{ id: 3, region: 'Texas', category: 'Grain', item: 'Rice', amount: 300 },
{ id: 4, region: 'Florida', category: 'Fruit', item: 'Orange', amount: 210 },
{ id: 5, region: 'Florida', category: 'Grain', item: 'Rice', amount: 280 },
{ id: 6, region: 'Florida', category: 'Grain', item: 'Corn', amount: 130 },
{ id: 7, region: 'Oregon', category: 'Vegetable', item: 'Cabbage', amount: 175 },
{ id: 8, region: 'Oregon', category: 'Vegetable', item: 'Potato', amount: 240 },
{ id: 9, region: 'Hawaii', category: 'Fruit', item: 'Pineapple', amount: 85 },
{ id: 10, region: 'Hawaii', category: 'Vegetable', item: 'Carrot', amount: 140 },
],
rowKey: 'id',
};
export const actions = {
mode: (api: GridApi<Row>, value?: string) => {
mode = value === 'top' || value === 'off' ? value : 'merge';
api.setColumns(columns());
},
label: (api: GridApi<Row>, value?: string) => {
label = value === 'start' ? 'start' : 'center';
api.setColumns(columns());
},
// Cells merge only when "neighboring rows have the same value". Changing the sort changes the neighbors, so which columns merge changes too.
sort: (api: GridApi<Row>, value?: string) =>
api.setSortModel(
value === 'region' || value === 'category'
? [{ columnId: value, direction: 'asc' }]
: value === 'amount'
? [{ columnId: 'amount', direction: 'desc' }]
: [],
),
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<script setup lang="ts">
import { shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
}
</script>
<template>
<div class="toolbar">
<select
aria-label="Mode"
:disabled="!api"
@change="api && actions.mode(api, ($event.target as HTMLSelectElement).value)"
>
<option value="merge">Merge into one cell</option>
<option value="top">Show on first row only</option>
<option value="off">No merging</option>
</select>
<select
aria-label="Label position"
:disabled="!api"
@change="api && actions.label(api, ($event.target as HTMLSelectElement).value)"
>
<option value="start">First row</option>
<option value="center" selected>Center of the visible span</option>
</select>
<select
aria-label="Sort by"
:disabled="!api"
@change="api && actions.sort(api, ($event.target as HTMLSelectElement).value)"
>
<option value="none" selected>No sorting</option>
<option value="region">Region</option>
<option value="category">Category</option>
<option value="amount">Amount, high to low</option>
</select>
</div>
<Grid style="height: 400px" :options="options" @ready="onReady" />
</template>import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
region: string;
category: string;
item: string;
amount: number;
}
type Mode = 'merge' | 'top' | 'off';
type Label = 'start' | 'center';
// Remember the chosen mode and set the columns again the same way whenever it changes.
let mode: Mode = 'merge';
let label: Label = 'center';
// merge: true -> equal values in consecutive rows merge into one cell.
// merge: 'top' -> cells stay as they are and only the first row of a run shows the value.
// mergeLabel -> picks where the value goes in both modes. 'start' is the first row of the run, 'center' is the middle of the visible run.
const columns = (): ColumnDef<Row>[] => {
const merge = mode === 'off' ? undefined : mode === 'top' ? ('top' as const) : true;
const on = merge === undefined ? {} : { merge, mergeLabel: label };
return [
{ id: 'region', title: 'Region', width: 110, ...on },
{ id: 'category', title: 'Category', width: 110, ...on },
{ id: 'item', title: 'Item', width: 130 },
{ id: 'amount', title: 'Shipped (tons)', kind: 'number', width: 130, align: 'end' },
];
};
export const options: GridOptions<Row> = {
columns: columns(),
headerGroups: [
{ id: 'where', title: 'Where', children: ['region', 'category'] },
{ id: 'what', title: 'How much', children: ['item', 'amount'] },
],
rows: [
{ id: 1, region: 'Texas', category: 'Fruit', item: 'Apple', amount: 120 },
{ id: 2, region: 'Texas', category: 'Fruit', item: 'Pear', amount: 90 },
{ id: 3, region: 'Texas', category: 'Grain', item: 'Rice', amount: 300 },
{ id: 4, region: 'Florida', category: 'Fruit', item: 'Orange', amount: 210 },
{ id: 5, region: 'Florida', category: 'Grain', item: 'Rice', amount: 280 },
{ id: 6, region: 'Florida', category: 'Grain', item: 'Corn', amount: 130 },
{ id: 7, region: 'Oregon', category: 'Vegetable', item: 'Cabbage', amount: 175 },
{ id: 8, region: 'Oregon', category: 'Vegetable', item: 'Potato', amount: 240 },
{ id: 9, region: 'Hawaii', category: 'Fruit', item: 'Pineapple', amount: 85 },
{ id: 10, region: 'Hawaii', category: 'Vegetable', item: 'Carrot', amount: 140 },
],
rowKey: 'id',
};
export const actions = {
mode: (api: GridApi<Row>, value?: string) => {
mode = value === 'top' || value === 'off' ? value : 'merge';
api.setColumns(columns());
},
label: (api: GridApi<Row>, value?: string) => {
label = value === 'start' ? 'start' : 'center';
api.setColumns(columns());
},
// Cells merge only when "neighboring rows have the same value". Changing the sort changes the neighbors, so which columns merge changes too.
sort: (api: GridApi<Row>, value?: string) =>
api.setSortModel(
value === 'region' || value === 'category'
? [{ columnId: value, direction: 'asc' }]
: value === 'amount'
? [{ columnId: 'amount', direction: 'desc' }]
: [],
),
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Component } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<select
aria-label="Mode"
[disabled]="!api"
(change)="api && actions.mode(api, $any($event.target).value)"
>
<option value="merge">Merge into one cell</option>
<option value="top">Show on first row only</option>
<option value="off">No merging</option>
</select>
<select
aria-label="Label position"
[disabled]="!api"
(change)="api && actions.label(api, $any($event.target).value)"
>
<option value="start">First row</option>
<option value="center" selected>Center of the visible span</option>
</select>
<select
aria-label="Sort by"
[disabled]="!api"
(change)="api && actions.sort(api, $any($event.target).value)"
>
<option value="none" selected>No sorting</option>
<option value="region">Region</option>
<option value="category">Category</option>
<option value="amount">Amount, high to low</option>
</select>
</div>
<mx-grid style="height: 400px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
}
}
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/angular';
export interface Row {
id: number;
region: string;
category: string;
item: string;
amount: number;
}
type Mode = 'merge' | 'top' | 'off';
type Label = 'start' | 'center';
// Remember the chosen mode and set the columns again the same way whenever it changes.
let mode: Mode = 'merge';
let label: Label = 'center';
// merge: true -> equal values in consecutive rows merge into one cell.
// merge: 'top' -> cells stay as they are and only the first row of a run shows the value.
// mergeLabel -> picks where the value goes in both modes. 'start' is the first row of the run, 'center' is the middle of the visible run.
const columns = (): ColumnDef<Row>[] => {
const merge = mode === 'off' ? undefined : mode === 'top' ? ('top' as const) : true;
const on = merge === undefined ? {} : { merge, mergeLabel: label };
return [
{ id: 'region', title: 'Region', width: 110, ...on },
{ id: 'category', title: 'Category', width: 110, ...on },
{ id: 'item', title: 'Item', width: 130 },
{ id: 'amount', title: 'Shipped (tons)', kind: 'number', width: 130, align: 'end' },
];
};
export const options: GridOptions<Row> = {
columns: columns(),
headerGroups: [
{ id: 'where', title: 'Where', children: ['region', 'category'] },
{ id: 'what', title: 'How much', children: ['item', 'amount'] },
],
rows: [
{ id: 1, region: 'Texas', category: 'Fruit', item: 'Apple', amount: 120 },
{ id: 2, region: 'Texas', category: 'Fruit', item: 'Pear', amount: 90 },
{ id: 3, region: 'Texas', category: 'Grain', item: 'Rice', amount: 300 },
{ id: 4, region: 'Florida', category: 'Fruit', item: 'Orange', amount: 210 },
{ id: 5, region: 'Florida', category: 'Grain', item: 'Rice', amount: 280 },
{ id: 6, region: 'Florida', category: 'Grain', item: 'Corn', amount: 130 },
{ id: 7, region: 'Oregon', category: 'Vegetable', item: 'Cabbage', amount: 175 },
{ id: 8, region: 'Oregon', category: 'Vegetable', item: 'Potato', amount: 240 },
{ id: 9, region: 'Hawaii', category: 'Fruit', item: 'Pineapple', amount: 85 },
{ id: 10, region: 'Hawaii', category: 'Vegetable', item: 'Carrot', amount: 140 },
],
rowKey: 'id',
};
export const actions = {
mode: (api: GridApi<Row>, value?: string) => {
mode = value === 'top' || value === 'off' ? value : 'merge';
api.setColumns(columns());
},
label: (api: GridApi<Row>, value?: string) => {
label = value === 'start' ? 'start' : 'center';
api.setColumns(columns());
},
// Cells merge only when "neighboring rows have the same value". Changing the sort changes the neighbors, so which columns merge changes too.
sort: (api: GridApi<Row>, value?: string) =>
api.setSortModel(
value === 'region' || value === 'category'
? [{ columnId: value, direction: 'asc' }]
: value === 'amount'
? [{ columnId: 'amount', direction: 'desc' }]
: [],
),
};