옆 칸이 같으면 가로로 합치기Merge equal neighbours across a row
근무표처럼 같은 줄에서 이웃한 값이 같으면 한 칸으로 합쳐요. 합칠 줄을 조건으로 정할 수도 있어서, 휴가 중인 줄은 그대로 둘 수 있어요.In a shift table, equal neighbours on a row become one cell. A condition decides which rows merge, so vacation rows can stay as they are.
코드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">켜기</option>
<option value="off">끄기</option>
</select>
<select aria-label="휴가 중인 줄" id="vacation">
<option value="merge">똑같이 합치기</option>
<option value="skip">합치지 않기</option>
</select>
</div>
<div id="grid" style="height: 250px"></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('merge').addEventListener('change', (event) => actions.merge(api, event.target.value));
document.getElementById('vacation').addEventListener('change', (event) => actions.vacation(api, event.target.value));// 시작 컬럼(월)에 mergeCols: 5를 주면, 같은 줄에서 이웃한 같은 값이 한 칸으로 합쳐져요.
// mergeColsWhen은 줄마다 합칠지 정해요. 휴가 중인 줄만 합치지 않게 해 볼 수 있어요.
let merged = true;
let skipVacation = false;
const columns = () => [
{ id: 'name', title: '이름', width: 110 },
{
id: 'mon',
title: '월',
width: 90,
align: 'center',
...(merged
? { mergeCols: 5, ...(skipVacation ? { mergeColsWhen: (row) => !row.vacation } : {}) }
: {}),
},
{ id: 'tue', title: '화', width: 90, align: 'center' },
{ id: 'wed', title: '수', width: 90, align: 'center' },
{ id: 'thu', title: '목', width: 90, align: 'center' },
{ id: 'fri', title: '금', width: 90, align: 'center' },
];
const row = (id, name, vacation, days) => ({
id,
name,
vacation,
mon: days[0],
tue: days[1],
wed: days[2],
thu: days[3],
fri: days[4],
});
export const options = {
columns: columns(),
rows: [
row(1, '김서연', false, ['주간', '주간', '야간', '야간', '야간']),
row(2, '이준호', true, ['휴무', '휴무', '휴무', '휴무', '휴무']),
row(3, '박민지', false, ['주간', '야간', '주간', '야간', '주간']),
row(4, '최도윤', false, ['야간', '야간', '야간', '주간', '주간']),
row(5, '정하은', true, ['주간', '주간', '휴무', '휴무', '휴무']),
row(6, '강지후', false, ['주간', '주간', '주간', '주간', '주간']),
],
rowKey: 'id',
};
export const actions = {
merge: (api, value) => {
merged = value !== 'off';
api.setColumns(columns());
},
vacation: (api, value) => {
skipVacation = value === 'skip';
api.setColumns(columns());
},
};<!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">켜기</option>
<option value="off">끄기</option>
</select>
<select aria-label="휴가 중인 줄" id="vacation">
<option value="merge">똑같이 합치기</option>
<option value="skip">합치지 않기</option>
</select>
</div>
<div id="grid" style="height: 250px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
// 시작 컬럼(월)에 mergeCols: 5를 주면, 같은 줄에서 이웃한 같은 값이 한 칸으로 합쳐져요.
// mergeColsWhen은 줄마다 합칠지 정해요. 휴가 중인 줄만 합치지 않게 해 볼 수 있어요.
let merged = true;
let skipVacation = false;
const columns = () => [
{ id: 'name', title: '이름', width: 110 },
{
id: 'mon',
title: '월',
width: 90,
align: 'center',
...(merged
? { mergeCols: 5, ...(skipVacation ? { mergeColsWhen: (row) => !row.vacation } : {}) }
: {}),
},
{ id: 'tue', title: '화', width: 90, align: 'center' },
{ id: 'wed', title: '수', width: 90, align: 'center' },
{ id: 'thu', title: '목', width: 90, align: 'center' },
{ id: 'fri', title: '금', width: 90, align: 'center' },
];
const row = (id, name, vacation, days) => ({
id,
name,
vacation,
mon: days[0],
tue: days[1],
wed: days[2],
thu: days[3],
fri: days[4],
});
const options = {
columns: columns(),
rows: [
row(1, '김서연', false, ['주간', '주간', '야간', '야간', '야간']),
row(2, '이준호', true, ['휴무', '휴무', '휴무', '휴무', '휴무']),
row(3, '박민지', false, ['주간', '야간', '주간', '야간', '주간']),
row(4, '최도윤', false, ['야간', '야간', '야간', '주간', '주간']),
row(5, '정하은', true, ['주간', '주간', '휴무', '휴무', '휴무']),
row(6, '강지후', false, ['주간', '주간', '주간', '주간', '주간']),
],
rowKey: 'id',
};
const actions = {
merge: (api, value) => {
merged = value !== 'off';
api.setColumns(columns());
},
vacation: (api, value) => {
skipVacation = value === 'skip';
api.setColumns(columns());
},
};
options.ariaLabel = "옆 칸이 같으면 가로로 합치기";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
document.getElementById('merge').addEventListener('change', (event) => actions.merge(api, event.target.value));
document.getElementById('vacation').addEventListener('change', (event) => actions.vacation(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.merge(api, event.target.value)}
>
<option value="on">켜기</option>
<option value="off">끄기</option>
</select>
<select
aria-label="휴가 중인 줄"
disabled={!api}
onChange={(event) => api && actions.vacation(api, event.target.value)}
>
<option value="merge">똑같이 합치기</option>
<option value="skip">합치지 않기</option>
</select>
</div>
<Grid style={{ height: 250 }} ref={gridRef} options={options} />
</>
);
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
name: string;
vacation: boolean;
mon: string;
tue: string;
wed: string;
thu: string;
fri: string;
}
// 시작 컬럼(월)에 mergeCols: 5를 주면, 같은 줄에서 이웃한 같은 값이 한 칸으로 합쳐져요.
// mergeColsWhen은 줄마다 합칠지 정해요. 휴가 중인 줄만 합치지 않게 해 볼 수 있어요.
let merged = true;
let skipVacation = false;
const columns = (): ColumnDef<Row>[] => [
{ id: 'name', title: '이름', width: 110 },
{
id: 'mon',
title: '월',
width: 90,
align: 'center',
...(merged
? { mergeCols: 5, ...(skipVacation ? { mergeColsWhen: (row: Row) => !row.vacation } : {}) }
: {}),
},
{ id: 'tue', title: '화', width: 90, align: 'center' },
{ id: 'wed', title: '수', width: 90, align: 'center' },
{ id: 'thu', title: '목', width: 90, align: 'center' },
{ id: 'fri', title: '금', width: 90, align: 'center' },
];
const row = (id: number, name: string, vacation: boolean, days: string[]): Row => ({
id,
name,
vacation,
mon: days[0],
tue: days[1],
wed: days[2],
thu: days[3],
fri: days[4],
});
export const options: GridOptions<Row> = {
columns: columns(),
rows: [
row(1, '김서연', false, ['주간', '주간', '야간', '야간', '야간']),
row(2, '이준호', true, ['휴무', '휴무', '휴무', '휴무', '휴무']),
row(3, '박민지', false, ['주간', '야간', '주간', '야간', '주간']),
row(4, '최도윤', false, ['야간', '야간', '야간', '주간', '주간']),
row(5, '정하은', true, ['주간', '주간', '휴무', '휴무', '휴무']),
row(6, '강지후', false, ['주간', '주간', '주간', '주간', '주간']),
],
rowKey: 'id',
};
export const actions = {
merge: (api: GridApi<Row>, value?: string) => {
merged = value !== 'off';
api.setColumns(columns());
},
vacation: (api: GridApi<Row>, value?: string) => {
skipVacation = value === 'skip';
api.setColumns(columns());
},
};버튼을 누르면 이 예제의 코드가 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.merge(api, ($event.target as HTMLSelectElement).value)"
>
<option value="on">켜기</option>
<option value="off">끄기</option>
</select>
<select
aria-label="휴가 중인 줄"
:disabled="!api"
@change="api && actions.vacation(api, ($event.target as HTMLSelectElement).value)"
>
<option value="merge">똑같이 합치기</option>
<option value="skip">합치지 않기</option>
</select>
</div>
<Grid style="height: 250px" :options="options" @ready="onReady" />
</template>import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
name: string;
vacation: boolean;
mon: string;
tue: string;
wed: string;
thu: string;
fri: string;
}
// 시작 컬럼(월)에 mergeCols: 5를 주면, 같은 줄에서 이웃한 같은 값이 한 칸으로 합쳐져요.
// mergeColsWhen은 줄마다 합칠지 정해요. 휴가 중인 줄만 합치지 않게 해 볼 수 있어요.
let merged = true;
let skipVacation = false;
const columns = (): ColumnDef<Row>[] => [
{ id: 'name', title: '이름', width: 110 },
{
id: 'mon',
title: '월',
width: 90,
align: 'center',
...(merged
? { mergeCols: 5, ...(skipVacation ? { mergeColsWhen: (row: Row) => !row.vacation } : {}) }
: {}),
},
{ id: 'tue', title: '화', width: 90, align: 'center' },
{ id: 'wed', title: '수', width: 90, align: 'center' },
{ id: 'thu', title: '목', width: 90, align: 'center' },
{ id: 'fri', title: '금', width: 90, align: 'center' },
];
const row = (id: number, name: string, vacation: boolean, days: string[]): Row => ({
id,
name,
vacation,
mon: days[0],
tue: days[1],
wed: days[2],
thu: days[3],
fri: days[4],
});
export const options: GridOptions<Row> = {
columns: columns(),
rows: [
row(1, '김서연', false, ['주간', '주간', '야간', '야간', '야간']),
row(2, '이준호', true, ['휴무', '휴무', '휴무', '휴무', '휴무']),
row(3, '박민지', false, ['주간', '야간', '주간', '야간', '주간']),
row(4, '최도윤', false, ['야간', '야간', '야간', '주간', '주간']),
row(5, '정하은', true, ['주간', '주간', '휴무', '휴무', '휴무']),
row(6, '강지후', false, ['주간', '주간', '주간', '주간', '주간']),
],
rowKey: 'id',
};
export const actions = {
merge: (api: GridApi<Row>, value?: string) => {
merged = value !== 'off';
api.setColumns(columns());
},
vacation: (api: GridApi<Row>, value?: string) => {
skipVacation = value === 'skip';
api.setColumns(columns());
},
};버튼을 누르면 이 예제의 코드가 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.merge(api, $any($event.target).value)"
>
<option value="on">켜기</option>
<option value="off">끄기</option>
</select>
<select
aria-label="휴가 중인 줄"
[disabled]="!api"
(change)="api && actions.vacation(api, $any($event.target).value)"
>
<option value="merge">똑같이 합치기</option>
<option value="skip">합치지 않기</option>
</select>
</div>
<mx-grid style="height: 250px" [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;
name: string;
vacation: boolean;
mon: string;
tue: string;
wed: string;
thu: string;
fri: string;
}
// 시작 컬럼(월)에 mergeCols: 5를 주면, 같은 줄에서 이웃한 같은 값이 한 칸으로 합쳐져요.
// mergeColsWhen은 줄마다 합칠지 정해요. 휴가 중인 줄만 합치지 않게 해 볼 수 있어요.
let merged = true;
let skipVacation = false;
const columns = (): ColumnDef<Row>[] => [
{ id: 'name', title: '이름', width: 110 },
{
id: 'mon',
title: '월',
width: 90,
align: 'center',
...(merged
? { mergeCols: 5, ...(skipVacation ? { mergeColsWhen: (row: Row) => !row.vacation } : {}) }
: {}),
},
{ id: 'tue', title: '화', width: 90, align: 'center' },
{ id: 'wed', title: '수', width: 90, align: 'center' },
{ id: 'thu', title: '목', width: 90, align: 'center' },
{ id: 'fri', title: '금', width: 90, align: 'center' },
];
const row = (id: number, name: string, vacation: boolean, days: string[]): Row => ({
id,
name,
vacation,
mon: days[0],
tue: days[1],
wed: days[2],
thu: days[3],
fri: days[4],
});
export const options: GridOptions<Row> = {
columns: columns(),
rows: [
row(1, '김서연', false, ['주간', '주간', '야간', '야간', '야간']),
row(2, '이준호', true, ['휴무', '휴무', '휴무', '휴무', '휴무']),
row(3, '박민지', false, ['주간', '야간', '주간', '야간', '주간']),
row(4, '최도윤', false, ['야간', '야간', '야간', '주간', '주간']),
row(5, '정하은', true, ['주간', '주간', '휴무', '휴무', '휴무']),
row(6, '강지후', false, ['주간', '주간', '주간', '주간', '주간']),
],
rowKey: 'id',
};
export const actions = {
merge: (api: GridApi<Row>, value?: string) => {
merged = value !== 'off';
api.setColumns(columns());
},
vacation: (api: GridApi<Row>, value?: string) => {
skipVacation = value === 'skip';
api.setColumns(columns());
},
};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 across" id="merge">
<option value="on">On</option>
<option value="off">Off</option>
</select>
<select aria-label="Vacation rows" id="vacation">
<option value="merge">Merge as usual</option>
<option value="skip">Do not merge</option>
</select>
</div>
<div id="grid" style="height: 250px"></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('merge').addEventListener('change', (event) => actions.merge(api, event.target.value));
document.getElementById('vacation').addEventListener('change', (event) => actions.vacation(api, event.target.value));// With mergeCols: 5 on the start column (Mon), equal neighboring values in the same row merge into one cell.
// mergeColsWhen decides per row whether to merge. You can skip merging for rows on vacation.
let merged = true;
let skipVacation = false;
const columns = () => [
{ id: 'name', title: 'Name', width: 130 },
{
id: 'mon',
title: 'Mon',
width: 90,
align: 'center',
...(merged
? { mergeCols: 5, ...(skipVacation ? { mergeColsWhen: (row) => !row.vacation } : {}) }
: {}),
},
{ id: 'tue', title: 'Tue', width: 90, align: 'center' },
{ id: 'wed', title: 'Wed', width: 90, align: 'center' },
{ id: 'thu', title: 'Thu', width: 90, align: 'center' },
{ id: 'fri', title: 'Fri', width: 90, align: 'center' },
];
const row = (id, name, vacation, days) => ({
id,
name,
vacation,
mon: days[0],
tue: days[1],
wed: days[2],
thu: days[3],
fri: days[4],
});
export const options = {
columns: columns(),
rows: [
row(1, 'Emma Stone', false, ['Day', 'Day', 'Night', 'Night', 'Night']),
row(2, 'Liam Carter', true, ['Off', 'Off', 'Off', 'Off', 'Off']),
row(3, 'Olivia Reed', false, ['Day', 'Night', 'Day', 'Night', 'Day']),
row(4, 'Noah Parker', false, ['Night', 'Night', 'Night', 'Day', 'Day']),
row(5, 'Sophia Hayes', true, ['Day', 'Day', 'Off', 'Off', 'Off']),
row(6, 'Ethan Brooks', false, ['Day', 'Day', 'Day', 'Day', 'Day']),
],
rowKey: 'id',
};
export const actions = {
merge: (api, value) => {
merged = value !== 'off';
api.setColumns(columns());
},
vacation: (api, value) => {
skipVacation = value === 'skip';
api.setColumns(columns());
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Merge equal neighbours across a row</title>
</head>
<body>
<div class="toolbar">
<select aria-label="Merge across" id="merge">
<option value="on">On</option>
<option value="off">Off</option>
</select>
<select aria-label="Vacation rows" id="vacation">
<option value="merge">Merge as usual</option>
<option value="skip">Do not merge</option>
</select>
</div>
<div id="grid" style="height: 250px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
// With mergeCols: 5 on the start column (Mon), equal neighboring values in the same row merge into one cell.
// mergeColsWhen decides per row whether to merge. You can skip merging for rows on vacation.
let merged = true;
let skipVacation = false;
const columns = () => [
{ id: 'name', title: 'Name', width: 130 },
{
id: 'mon',
title: 'Mon',
width: 90,
align: 'center',
...(merged
? { mergeCols: 5, ...(skipVacation ? { mergeColsWhen: (row) => !row.vacation } : {}) }
: {}),
},
{ id: 'tue', title: 'Tue', width: 90, align: 'center' },
{ id: 'wed', title: 'Wed', width: 90, align: 'center' },
{ id: 'thu', title: 'Thu', width: 90, align: 'center' },
{ id: 'fri', title: 'Fri', width: 90, align: 'center' },
];
const row = (id, name, vacation, days) => ({
id,
name,
vacation,
mon: days[0],
tue: days[1],
wed: days[2],
thu: days[3],
fri: days[4],
});
const options = {
columns: columns(),
rows: [
row(1, 'Emma Stone', false, ['Day', 'Day', 'Night', 'Night', 'Night']),
row(2, 'Liam Carter', true, ['Off', 'Off', 'Off', 'Off', 'Off']),
row(3, 'Olivia Reed', false, ['Day', 'Night', 'Day', 'Night', 'Day']),
row(4, 'Noah Parker', false, ['Night', 'Night', 'Night', 'Day', 'Day']),
row(5, 'Sophia Hayes', true, ['Day', 'Day', 'Off', 'Off', 'Off']),
row(6, 'Ethan Brooks', false, ['Day', 'Day', 'Day', 'Day', 'Day']),
],
rowKey: 'id',
};
const actions = {
merge: (api, value) => {
merged = value !== 'off';
api.setColumns(columns());
},
vacation: (api, value) => {
skipVacation = value === 'skip';
api.setColumns(columns());
},
};
options.ariaLabel = "Merge equal neighbours across a row";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
document.getElementById('merge').addEventListener('change', (event) => actions.merge(api, event.target.value));
document.getElementById('vacation').addEventListener('change', (event) => actions.vacation(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="Merge across"
disabled={!api}
onChange={(event) => api && actions.merge(api, event.target.value)}
>
<option value="on">On</option>
<option value="off">Off</option>
</select>
<select
aria-label="Vacation rows"
disabled={!api}
onChange={(event) => api && actions.vacation(api, event.target.value)}
>
<option value="merge">Merge as usual</option>
<option value="skip">Do not merge</option>
</select>
</div>
<Grid style={{ height: 250 }} ref={gridRef} options={options} />
</>
);
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
name: string;
vacation: boolean;
mon: string;
tue: string;
wed: string;
thu: string;
fri: string;
}
// With mergeCols: 5 on the start column (Mon), equal neighboring values in the same row merge into one cell.
// mergeColsWhen decides per row whether to merge. You can skip merging for rows on vacation.
let merged = true;
let skipVacation = false;
const columns = (): ColumnDef<Row>[] => [
{ id: 'name', title: 'Name', width: 130 },
{
id: 'mon',
title: 'Mon',
width: 90,
align: 'center',
...(merged
? { mergeCols: 5, ...(skipVacation ? { mergeColsWhen: (row: Row) => !row.vacation } : {}) }
: {}),
},
{ id: 'tue', title: 'Tue', width: 90, align: 'center' },
{ id: 'wed', title: 'Wed', width: 90, align: 'center' },
{ id: 'thu', title: 'Thu', width: 90, align: 'center' },
{ id: 'fri', title: 'Fri', width: 90, align: 'center' },
];
const row = (id: number, name: string, vacation: boolean, days: string[]): Row => ({
id,
name,
vacation,
mon: days[0],
tue: days[1],
wed: days[2],
thu: days[3],
fri: days[4],
});
export const options: GridOptions<Row> = {
columns: columns(),
rows: [
row(1, 'Emma Stone', false, ['Day', 'Day', 'Night', 'Night', 'Night']),
row(2, 'Liam Carter', true, ['Off', 'Off', 'Off', 'Off', 'Off']),
row(3, 'Olivia Reed', false, ['Day', 'Night', 'Day', 'Night', 'Day']),
row(4, 'Noah Parker', false, ['Night', 'Night', 'Night', 'Day', 'Day']),
row(5, 'Sophia Hayes', true, ['Day', 'Day', 'Off', 'Off', 'Off']),
row(6, 'Ethan Brooks', false, ['Day', 'Day', 'Day', 'Day', 'Day']),
],
rowKey: 'id',
};
export const actions = {
merge: (api: GridApi<Row>, value?: string) => {
merged = value !== 'off';
api.setColumns(columns());
},
vacation: (api: GridApi<Row>, value?: string) => {
skipVacation = value === 'skip';
api.setColumns(columns());
},
};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="Merge across"
:disabled="!api"
@change="api && actions.merge(api, ($event.target as HTMLSelectElement).value)"
>
<option value="on">On</option>
<option value="off">Off</option>
</select>
<select
aria-label="Vacation rows"
:disabled="!api"
@change="api && actions.vacation(api, ($event.target as HTMLSelectElement).value)"
>
<option value="merge">Merge as usual</option>
<option value="skip">Do not merge</option>
</select>
</div>
<Grid style="height: 250px" :options="options" @ready="onReady" />
</template>import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
name: string;
vacation: boolean;
mon: string;
tue: string;
wed: string;
thu: string;
fri: string;
}
// With mergeCols: 5 on the start column (Mon), equal neighboring values in the same row merge into one cell.
// mergeColsWhen decides per row whether to merge. You can skip merging for rows on vacation.
let merged = true;
let skipVacation = false;
const columns = (): ColumnDef<Row>[] => [
{ id: 'name', title: 'Name', width: 130 },
{
id: 'mon',
title: 'Mon',
width: 90,
align: 'center',
...(merged
? { mergeCols: 5, ...(skipVacation ? { mergeColsWhen: (row: Row) => !row.vacation } : {}) }
: {}),
},
{ id: 'tue', title: 'Tue', width: 90, align: 'center' },
{ id: 'wed', title: 'Wed', width: 90, align: 'center' },
{ id: 'thu', title: 'Thu', width: 90, align: 'center' },
{ id: 'fri', title: 'Fri', width: 90, align: 'center' },
];
const row = (id: number, name: string, vacation: boolean, days: string[]): Row => ({
id,
name,
vacation,
mon: days[0],
tue: days[1],
wed: days[2],
thu: days[3],
fri: days[4],
});
export const options: GridOptions<Row> = {
columns: columns(),
rows: [
row(1, 'Emma Stone', false, ['Day', 'Day', 'Night', 'Night', 'Night']),
row(2, 'Liam Carter', true, ['Off', 'Off', 'Off', 'Off', 'Off']),
row(3, 'Olivia Reed', false, ['Day', 'Night', 'Day', 'Night', 'Day']),
row(4, 'Noah Parker', false, ['Night', 'Night', 'Night', 'Day', 'Day']),
row(5, 'Sophia Hayes', true, ['Day', 'Day', 'Off', 'Off', 'Off']),
row(6, 'Ethan Brooks', false, ['Day', 'Day', 'Day', 'Day', 'Day']),
],
rowKey: 'id',
};
export const actions = {
merge: (api: GridApi<Row>, value?: string) => {
merged = value !== 'off';
api.setColumns(columns());
},
vacation: (api: GridApi<Row>, value?: string) => {
skipVacation = value === 'skip';
api.setColumns(columns());
},
};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="Merge across"
[disabled]="!api"
(change)="api && actions.merge(api, $any($event.target).value)"
>
<option value="on">On</option>
<option value="off">Off</option>
</select>
<select
aria-label="Vacation rows"
[disabled]="!api"
(change)="api && actions.vacation(api, $any($event.target).value)"
>
<option value="merge">Merge as usual</option>
<option value="skip">Do not merge</option>
</select>
</div>
<mx-grid style="height: 250px" [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;
name: string;
vacation: boolean;
mon: string;
tue: string;
wed: string;
thu: string;
fri: string;
}
// With mergeCols: 5 on the start column (Mon), equal neighboring values in the same row merge into one cell.
// mergeColsWhen decides per row whether to merge. You can skip merging for rows on vacation.
let merged = true;
let skipVacation = false;
const columns = (): ColumnDef<Row>[] => [
{ id: 'name', title: 'Name', width: 130 },
{
id: 'mon',
title: 'Mon',
width: 90,
align: 'center',
...(merged
? { mergeCols: 5, ...(skipVacation ? { mergeColsWhen: (row: Row) => !row.vacation } : {}) }
: {}),
},
{ id: 'tue', title: 'Tue', width: 90, align: 'center' },
{ id: 'wed', title: 'Wed', width: 90, align: 'center' },
{ id: 'thu', title: 'Thu', width: 90, align: 'center' },
{ id: 'fri', title: 'Fri', width: 90, align: 'center' },
];
const row = (id: number, name: string, vacation: boolean, days: string[]): Row => ({
id,
name,
vacation,
mon: days[0],
tue: days[1],
wed: days[2],
thu: days[3],
fri: days[4],
});
export const options: GridOptions<Row> = {
columns: columns(),
rows: [
row(1, 'Emma Stone', false, ['Day', 'Day', 'Night', 'Night', 'Night']),
row(2, 'Liam Carter', true, ['Off', 'Off', 'Off', 'Off', 'Off']),
row(3, 'Olivia Reed', false, ['Day', 'Night', 'Day', 'Night', 'Day']),
row(4, 'Noah Parker', false, ['Night', 'Night', 'Night', 'Day', 'Day']),
row(5, 'Sophia Hayes', true, ['Day', 'Day', 'Off', 'Off', 'Off']),
row(6, 'Ethan Brooks', false, ['Day', 'Day', 'Day', 'Day', 'Day']),
],
rowKey: 'id',
};
export const actions = {
merge: (api: GridApi<Row>, value?: string) => {
merged = value !== 'off';
api.setColumns(columns());
},
vacation: (api: GridApi<Row>, value?: string) => {
skipVacation = value === 'skip';
api.setColumns(columns());
},
};