위쪽 줄과 왼쪽 컬럼을 같이 붙이기Pin rows and columns together
합계 줄은 위에, 구분·품목 컬럼은 왼쪽에 붙여요. 가로세로로 어느 쪽으로 스크롤해도 기준이 되는 줄과 컬럼이 항상 눈앞에 남아요.Totals pinned on top, group and item columns pinned on the left. Scroll either way and your reference rows and columns stay in view.
코드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="rowCount">
<option value="3">3줄</option>
<option value="1">1줄</option>
<option value="0">고정 안 함</option>
</select>
<select aria-label="고정 컬럼 수" id="colCount">
<option value="2">2개</option>
<option value="1">1개</option>
<option value="0">고정 안 함</option>
</select>
</div>
<div id="grid" style="height: 360px"></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('rowCount').addEventListener('change', (event) => actions.rowCount(api, event.target.value));
document.getElementById('colCount').addEventListener('change', (event) => actions.colCount(api, event.target.value));// 위쪽 줄은 pinnedRows로, 왼쪽 컬럼은 컬럼의 pinned로 붙여요. 둘을 같이 쓰면
// 가로로도 세로로도 스크롤해도 "합계 줄"과 "구분·품목" 컬럼이 항상 보여요.
const years = ['y2020', 'y2021', 'y2022', 'y2023', 'y2024', 'y2025'];
const groups = ['곡류', '채소류', '과일류'];
const items = Array.from({ length: 36 }, (_, i) => {
const base = 40 + ((i * 37) % 260);
return {
id: i + 4,
group: groups[Math.floor(i / 12)],
item: `품목 ${String(i + 1).padStart(2, '0')}`,
y2020: base,
y2021: base + 3,
y2022: base + 5,
y2023: base + 6,
y2024: base + 9,
y2025: base + 12,
};
});
// 맨 위 세 줄: 전체 합계, 평균, 가장 큰 값. 아래 품목에서 계산해요.
const pick = (fn) => Object.fromEntries(years.map((y) => [y, fn(items.map((row) => row[y]))]));
const total = (values) => values.reduce((a, b) => a + b, 0);
const rows = [
{ id: 1, group: '요약', item: '전체 합계', ...pick(total) },
{ id: 2, group: '요약', item: '평균', ...pick((v) => Math.round(total(v) / v.length)) },
{ id: 3, group: '요약', item: '최대', ...pick((v) => Math.max(...v)) },
...items,
];
export const options = {
columns: [
{ id: 'group', title: '구분', width: 110, pinned: 'start', merge: true },
{ id: 'item', title: '품목', width: 140, pinned: 'start' },
...years.map((id) => ({
id,
title: id.slice(1),
kind: 'number',
width: 150,
align: 'end',
})),
],
headerGroups: [
{ id: 'early', title: '2020~2022', children: ['y2020', 'y2021', 'y2022'] },
{ id: 'late', title: '2023~2025', children: ['y2023', 'y2024', 'y2025'] },
],
rows,
rowKey: 'id',
// 화면이 480px보다 좁으면 고정이 풀려서 모두 같이 스크롤되고, 넓어지면 다시 붙어요.
pinMinWidth: 480,
pinnedRows: 3,
};
export const actions = {
rowCount: (api, value) => api.setOptions({ pinnedRows: Number(value ?? 0) }),
colCount: (api, value) => {
const count = Number(value ?? 0);
const ids = api.getColumns().map((c) => c.id);
// 뒤에서부터 풀어야 컬럼 순서가 그대로예요.
[...ids].reverse().forEach((id) => api.pinColumn(id, null));
ids.slice(0, count).forEach((id) => api.pinColumn(id, 'start'));
},
};<!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="rowCount">
<option value="3">3줄</option>
<option value="1">1줄</option>
<option value="0">고정 안 함</option>
</select>
<select aria-label="고정 컬럼 수" id="colCount">
<option value="2">2개</option>
<option value="1">1개</option>
<option value="0">고정 안 함</option>
</select>
</div>
<div id="grid" style="height: 360px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
// 위쪽 줄은 pinnedRows로, 왼쪽 컬럼은 컬럼의 pinned로 붙여요. 둘을 같이 쓰면
// 가로로도 세로로도 스크롤해도 "합계 줄"과 "구분·품목" 컬럼이 항상 보여요.
const years = ['y2020', 'y2021', 'y2022', 'y2023', 'y2024', 'y2025'];
const groups = ['곡류', '채소류', '과일류'];
const items = Array.from({ length: 36 }, (_, i) => {
const base = 40 + ((i * 37) % 260);
return {
id: i + 4,
group: groups[Math.floor(i / 12)],
item: `품목 ${String(i + 1).padStart(2, '0')}`,
y2020: base,
y2021: base + 3,
y2022: base + 5,
y2023: base + 6,
y2024: base + 9,
y2025: base + 12,
};
});
// 맨 위 세 줄: 전체 합계, 평균, 가장 큰 값. 아래 품목에서 계산해요.
const pick = (fn) => Object.fromEntries(years.map((y) => [y, fn(items.map((row) => row[y]))]));
const total = (values) => values.reduce((a, b) => a + b, 0);
const rows = [
{ id: 1, group: '요약', item: '전체 합계', ...pick(total) },
{ id: 2, group: '요약', item: '평균', ...pick((v) => Math.round(total(v) / v.length)) },
{ id: 3, group: '요약', item: '최대', ...pick((v) => Math.max(...v)) },
...items,
];
const options = {
columns: [
{ id: 'group', title: '구분', width: 110, pinned: 'start', merge: true },
{ id: 'item', title: '품목', width: 140, pinned: 'start' },
...years.map((id) => ({
id,
title: id.slice(1),
kind: 'number',
width: 150,
align: 'end',
})),
],
headerGroups: [
{ id: 'early', title: '2020~2022', children: ['y2020', 'y2021', 'y2022'] },
{ id: 'late', title: '2023~2025', children: ['y2023', 'y2024', 'y2025'] },
],
rows,
rowKey: 'id',
// 화면이 480px보다 좁으면 고정이 풀려서 모두 같이 스크롤되고, 넓어지면 다시 붙어요.
pinMinWidth: 480,
pinnedRows: 3,
};
const actions = {
rowCount: (api, value) => api.setOptions({ pinnedRows: Number(value ?? 0) }),
colCount: (api, value) => {
const count = Number(value ?? 0);
const ids = api.getColumns().map((c) => c.id);
// 뒤에서부터 풀어야 컬럼 순서가 그대로예요.
[...ids].reverse().forEach((id) => api.pinColumn(id, null));
ids.slice(0, count).forEach((id) => api.pinColumn(id, 'start'));
},
};
options.ariaLabel = "위쪽 줄과 왼쪽 컬럼을 같이 붙이기";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
document.getElementById('rowCount').addEventListener('change', (event) => actions.rowCount(api, event.target.value));
document.getElementById('colCount').addEventListener('change', (event) => actions.colCount(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.rowCount(api, event.target.value)}
>
<option value="3">3줄</option>
<option value="1">1줄</option>
<option value="0">고정 안 함</option>
</select>
<select
aria-label="고정 컬럼 수"
disabled={!api}
onChange={(event) => api && actions.colCount(api, event.target.value)}
>
<option value="2">2개</option>
<option value="1">1개</option>
<option value="0">고정 안 함</option>
</select>
</div>
<Grid style={{ height: 360 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
group: string;
item: string;
y2020: number;
y2021: number;
y2022: number;
y2023: number;
y2024: number;
y2025: number;
}
// 위쪽 줄은 pinnedRows로, 왼쪽 컬럼은 컬럼의 pinned로 붙여요. 둘을 같이 쓰면
// 가로로도 세로로도 스크롤해도 "합계 줄"과 "구분·품목" 컬럼이 항상 보여요.
const years = ['y2020', 'y2021', 'y2022', 'y2023', 'y2024', 'y2025'] as const;
const groups = ['곡류', '채소류', '과일류'];
const items: Row[] = Array.from({ length: 36 }, (_, i) => {
const base = 40 + ((i * 37) % 260);
return {
id: i + 4,
group: groups[Math.floor(i / 12)],
item: `품목 ${String(i + 1).padStart(2, '0')}`,
y2020: base,
y2021: base + 3,
y2022: base + 5,
y2023: base + 6,
y2024: base + 9,
y2025: base + 12,
};
});
// 맨 위 세 줄: 전체 합계, 평균, 가장 큰 값. 아래 품목에서 계산해요.
const pick = (fn: (values: number[]) => number): Pick<Row, (typeof years)[number]> =>
Object.fromEntries(years.map((y) => [y, fn(items.map((row) => row[y]))])) as Pick<
Row,
(typeof years)[number]
>;
const total = (values: number[]) => values.reduce((a, b) => a + b, 0);
const rows: Row[] = [
{ id: 1, group: '요약', item: '전체 합계', ...pick(total) },
{ id: 2, group: '요약', item: '평균', ...pick((v) => Math.round(total(v) / v.length)) },
{ id: 3, group: '요약', item: '최대', ...pick((v) => Math.max(...v)) },
...items,
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'group', title: '구분', width: 110, pinned: 'start', merge: true },
{ id: 'item', title: '품목', width: 140, pinned: 'start' },
...years.map((id) => ({
id,
title: id.slice(1),
kind: 'number' as const,
width: 150,
align: 'end' as const,
})),
],
headerGroups: [
{ id: 'early', title: '2020~2022', children: ['y2020', 'y2021', 'y2022'] },
{ id: 'late', title: '2023~2025', children: ['y2023', 'y2024', 'y2025'] },
],
rows,
rowKey: 'id',
// 화면이 480px보다 좁으면 고정이 풀려서 모두 같이 스크롤되고, 넓어지면 다시 붙어요.
pinMinWidth: 480,
pinnedRows: 3,
};
export const actions = {
rowCount: (api: GridApi<Row>, value?: string) =>
api.setOptions({ pinnedRows: Number(value ?? 0) }),
colCount: (api: GridApi<Row>, value?: string) => {
const count = Number(value ?? 0);
const ids = api.getColumns().map((c) => c.id);
// 뒤에서부터 풀어야 컬럼 순서가 그대로예요.
[...ids].reverse().forEach((id) => api.pinColumn(id, null));
ids.slice(0, count).forEach((id) => api.pinColumn(id, 'start'));
},
};버튼을 누르면 이 예제의 코드가 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.rowCount(api, ($event.target as HTMLSelectElement).value)"
>
<option value="3">3줄</option>
<option value="1">1줄</option>
<option value="0">고정 안 함</option>
</select>
<select
aria-label="고정 컬럼 수"
:disabled="!api"
@change="api && actions.colCount(api, ($event.target as HTMLSelectElement).value)"
>
<option value="2">2개</option>
<option value="1">1개</option>
<option value="0">고정 안 함</option>
</select>
</div>
<Grid style="height: 360px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
group: string;
item: string;
y2020: number;
y2021: number;
y2022: number;
y2023: number;
y2024: number;
y2025: number;
}
// 위쪽 줄은 pinnedRows로, 왼쪽 컬럼은 컬럼의 pinned로 붙여요. 둘을 같이 쓰면
// 가로로도 세로로도 스크롤해도 "합계 줄"과 "구분·품목" 컬럼이 항상 보여요.
const years = ['y2020', 'y2021', 'y2022', 'y2023', 'y2024', 'y2025'] as const;
const groups = ['곡류', '채소류', '과일류'];
const items: Row[] = Array.from({ length: 36 }, (_, i) => {
const base = 40 + ((i * 37) % 260);
return {
id: i + 4,
group: groups[Math.floor(i / 12)],
item: `품목 ${String(i + 1).padStart(2, '0')}`,
y2020: base,
y2021: base + 3,
y2022: base + 5,
y2023: base + 6,
y2024: base + 9,
y2025: base + 12,
};
});
// 맨 위 세 줄: 전체 합계, 평균, 가장 큰 값. 아래 품목에서 계산해요.
const pick = (fn: (values: number[]) => number): Pick<Row, (typeof years)[number]> =>
Object.fromEntries(years.map((y) => [y, fn(items.map((row) => row[y]))])) as Pick<
Row,
(typeof years)[number]
>;
const total = (values: number[]) => values.reduce((a, b) => a + b, 0);
const rows: Row[] = [
{ id: 1, group: '요약', item: '전체 합계', ...pick(total) },
{ id: 2, group: '요약', item: '평균', ...pick((v) => Math.round(total(v) / v.length)) },
{ id: 3, group: '요약', item: '최대', ...pick((v) => Math.max(...v)) },
...items,
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'group', title: '구분', width: 110, pinned: 'start', merge: true },
{ id: 'item', title: '품목', width: 140, pinned: 'start' },
...years.map((id) => ({
id,
title: id.slice(1),
kind: 'number' as const,
width: 150,
align: 'end' as const,
})),
],
headerGroups: [
{ id: 'early', title: '2020~2022', children: ['y2020', 'y2021', 'y2022'] },
{ id: 'late', title: '2023~2025', children: ['y2023', 'y2024', 'y2025'] },
],
rows,
rowKey: 'id',
// 화면이 480px보다 좁으면 고정이 풀려서 모두 같이 스크롤되고, 넓어지면 다시 붙어요.
pinMinWidth: 480,
pinnedRows: 3,
};
export const actions = {
rowCount: (api: GridApi<Row>, value?: string) =>
api.setOptions({ pinnedRows: Number(value ?? 0) }),
colCount: (api: GridApi<Row>, value?: string) => {
const count = Number(value ?? 0);
const ids = api.getColumns().map((c) => c.id);
// 뒤에서부터 풀어야 컬럼 순서가 그대로예요.
[...ids].reverse().forEach((id) => api.pinColumn(id, null));
ids.slice(0, count).forEach((id) => api.pinColumn(id, 'start'));
},
};버튼을 누르면 이 예제의 코드가 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.rowCount(api, $any($event.target).value)"
>
<option value="3">3줄</option>
<option value="1">1줄</option>
<option value="0">고정 안 함</option>
</select>
<select
aria-label="고정 컬럼 수"
[disabled]="!api"
(change)="api && actions.colCount(api, $any($event.target).value)"
>
<option value="2">2개</option>
<option value="1">1개</option>
<option value="0">고정 안 함</option>
</select>
</div>
<mx-grid style="height: 360px" [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 { GridApi, GridOptions } from '@maxgrid/angular';
export interface Row {
id: number;
group: string;
item: string;
y2020: number;
y2021: number;
y2022: number;
y2023: number;
y2024: number;
y2025: number;
}
// 위쪽 줄은 pinnedRows로, 왼쪽 컬럼은 컬럼의 pinned로 붙여요. 둘을 같이 쓰면
// 가로로도 세로로도 스크롤해도 "합계 줄"과 "구분·품목" 컬럼이 항상 보여요.
const years = ['y2020', 'y2021', 'y2022', 'y2023', 'y2024', 'y2025'] as const;
const groups = ['곡류', '채소류', '과일류'];
const items: Row[] = Array.from({ length: 36 }, (_, i) => {
const base = 40 + ((i * 37) % 260);
return {
id: i + 4,
group: groups[Math.floor(i / 12)],
item: `품목 ${String(i + 1).padStart(2, '0')}`,
y2020: base,
y2021: base + 3,
y2022: base + 5,
y2023: base + 6,
y2024: base + 9,
y2025: base + 12,
};
});
// 맨 위 세 줄: 전체 합계, 평균, 가장 큰 값. 아래 품목에서 계산해요.
const pick = (fn: (values: number[]) => number): Pick<Row, (typeof years)[number]> =>
Object.fromEntries(years.map((y) => [y, fn(items.map((row) => row[y]))])) as Pick<
Row,
(typeof years)[number]
>;
const total = (values: number[]) => values.reduce((a, b) => a + b, 0);
const rows: Row[] = [
{ id: 1, group: '요약', item: '전체 합계', ...pick(total) },
{ id: 2, group: '요약', item: '평균', ...pick((v) => Math.round(total(v) / v.length)) },
{ id: 3, group: '요약', item: '최대', ...pick((v) => Math.max(...v)) },
...items,
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'group', title: '구분', width: 110, pinned: 'start', merge: true },
{ id: 'item', title: '품목', width: 140, pinned: 'start' },
...years.map((id) => ({
id,
title: id.slice(1),
kind: 'number' as const,
width: 150,
align: 'end' as const,
})),
],
headerGroups: [
{ id: 'early', title: '2020~2022', children: ['y2020', 'y2021', 'y2022'] },
{ id: 'late', title: '2023~2025', children: ['y2023', 'y2024', 'y2025'] },
],
rows,
rowKey: 'id',
// 화면이 480px보다 좁으면 고정이 풀려서 모두 같이 스크롤되고, 넓어지면 다시 붙어요.
pinMinWidth: 480,
pinnedRows: 3,
};
export const actions = {
rowCount: (api: GridApi<Row>, value?: string) =>
api.setOptions({ pinnedRows: Number(value ?? 0) }),
colCount: (api: GridApi<Row>, value?: string) => {
const count = Number(value ?? 0);
const ids = api.getColumns().map((c) => c.id);
// 뒤에서부터 풀어야 컬럼 순서가 그대로예요.
[...ids].reverse().forEach((id) => api.pinColumn(id, null));
ids.slice(0, count).forEach((id) => api.pinColumn(id, 'start'));
},
};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="Pinned rows" id="rowCount">
<option value="3">3 rows</option>
<option value="1">1 row</option>
<option value="0">None</option>
</select>
<select aria-label="Pinned columns" id="colCount">
<option value="2">2 columns</option>
<option value="1">1 column</option>
<option value="0">None</option>
</select>
</div>
<div id="grid" style="height: 360px"></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('rowCount').addEventListener('change', (event) => actions.rowCount(api, event.target.value));
document.getElementById('colCount').addEventListener('change', (event) => actions.colCount(api, event.target.value));// The top rows are pinned with pinnedRows and the left columns with the column's pinned. Used together,
// the "total rows" and the "group / item" columns stay visible whether you scroll horizontally or vertically.
const years = ['y2020', 'y2021', 'y2022', 'y2023', 'y2024', 'y2025'];
const groups = ['Grains', 'Vegetables', 'Fruits'];
const items = Array.from({ length: 36 }, (_, i) => {
const base = 40 + ((i * 37) % 260);
return {
id: i + 4,
group: groups[Math.floor(i / 12)],
item: `Item ${String(i + 1).padStart(2, '0')}`,
y2020: base,
y2021: base + 3,
y2022: base + 5,
y2023: base + 6,
y2024: base + 9,
y2025: base + 12,
};
});
// Top three rows: grand total, average, largest value. Computed from the items below.
const pick = (fn) => Object.fromEntries(years.map((y) => [y, fn(items.map((row) => row[y]))]));
const total = (values) => values.reduce((a, b) => a + b, 0);
const rows = [
{ id: 1, group: 'Summary', item: 'Grand total', ...pick(total) },
{ id: 2, group: 'Summary', item: 'Average', ...pick((v) => Math.round(total(v) / v.length)) },
{ id: 3, group: 'Summary', item: 'Maximum', ...pick((v) => Math.max(...v)) },
...items,
];
export const options = {
columns: [
{ id: 'group', title: 'Group', width: 110, pinned: 'start', merge: true },
{ id: 'item', title: 'Item', width: 140, pinned: 'start' },
...years.map((id) => ({
id,
title: id.slice(1),
kind: 'number',
width: 150,
align: 'end',
})),
],
headerGroups: [
{ id: 'early', title: '2020-2022', children: ['y2020', 'y2021', 'y2022'] },
{ id: 'late', title: '2023-2025', children: ['y2023', 'y2024', 'y2025'] },
],
rows,
rowKey: 'id',
// On a screen narrower than 480px the pins are released and everything scrolls together; they reattach when it gets wider.
pinMinWidth: 480,
pinnedRows: 3,
};
export const actions = {
rowCount: (api, value) => api.setOptions({ pinnedRows: Number(value ?? 0) }),
colCount: (api, value) => {
const count = Number(value ?? 0);
const ids = api.getColumns().map((c) => c.id);
// Release from the back so the column order stays the same.
[...ids].reverse().forEach((id) => api.pinColumn(id, null));
ids.slice(0, count).forEach((id) => api.pinColumn(id, 'start'));
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Pin rows and columns together</title>
</head>
<body>
<div class="toolbar">
<select aria-label="Pinned rows" id="rowCount">
<option value="3">3 rows</option>
<option value="1">1 row</option>
<option value="0">None</option>
</select>
<select aria-label="Pinned columns" id="colCount">
<option value="2">2 columns</option>
<option value="1">1 column</option>
<option value="0">None</option>
</select>
</div>
<div id="grid" style="height: 360px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
// The top rows are pinned with pinnedRows and the left columns with the column's pinned. Used together,
// the "total rows" and the "group / item" columns stay visible whether you scroll horizontally or vertically.
const years = ['y2020', 'y2021', 'y2022', 'y2023', 'y2024', 'y2025'];
const groups = ['Grains', 'Vegetables', 'Fruits'];
const items = Array.from({ length: 36 }, (_, i) => {
const base = 40 + ((i * 37) % 260);
return {
id: i + 4,
group: groups[Math.floor(i / 12)],
item: `Item ${String(i + 1).padStart(2, '0')}`,
y2020: base,
y2021: base + 3,
y2022: base + 5,
y2023: base + 6,
y2024: base + 9,
y2025: base + 12,
};
});
// Top three rows: grand total, average, largest value. Computed from the items below.
const pick = (fn) => Object.fromEntries(years.map((y) => [y, fn(items.map((row) => row[y]))]));
const total = (values) => values.reduce((a, b) => a + b, 0);
const rows = [
{ id: 1, group: 'Summary', item: 'Grand total', ...pick(total) },
{ id: 2, group: 'Summary', item: 'Average', ...pick((v) => Math.round(total(v) / v.length)) },
{ id: 3, group: 'Summary', item: 'Maximum', ...pick((v) => Math.max(...v)) },
...items,
];
const options = {
columns: [
{ id: 'group', title: 'Group', width: 110, pinned: 'start', merge: true },
{ id: 'item', title: 'Item', width: 140, pinned: 'start' },
...years.map((id) => ({
id,
title: id.slice(1),
kind: 'number',
width: 150,
align: 'end',
})),
],
headerGroups: [
{ id: 'early', title: '2020-2022', children: ['y2020', 'y2021', 'y2022'] },
{ id: 'late', title: '2023-2025', children: ['y2023', 'y2024', 'y2025'] },
],
rows,
rowKey: 'id',
// On a screen narrower than 480px the pins are released and everything scrolls together; they reattach when it gets wider.
pinMinWidth: 480,
pinnedRows: 3,
};
const actions = {
rowCount: (api, value) => api.setOptions({ pinnedRows: Number(value ?? 0) }),
colCount: (api, value) => {
const count = Number(value ?? 0);
const ids = api.getColumns().map((c) => c.id);
// Release from the back so the column order stays the same.
[...ids].reverse().forEach((id) => api.pinColumn(id, null));
ids.slice(0, count).forEach((id) => api.pinColumn(id, 'start'));
},
};
options.ariaLabel = "Pin rows and columns together";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
document.getElementById('rowCount').addEventListener('change', (event) => actions.rowCount(api, event.target.value));
document.getElementById('colCount').addEventListener('change', (event) => actions.colCount(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="Pinned rows"
disabled={!api}
onChange={(event) => api && actions.rowCount(api, event.target.value)}
>
<option value="3">3 rows</option>
<option value="1">1 row</option>
<option value="0">None</option>
</select>
<select
aria-label="Pinned columns"
disabled={!api}
onChange={(event) => api && actions.colCount(api, event.target.value)}
>
<option value="2">2 columns</option>
<option value="1">1 column</option>
<option value="0">None</option>
</select>
</div>
<Grid style={{ height: 360 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
group: string;
item: string;
y2020: number;
y2021: number;
y2022: number;
y2023: number;
y2024: number;
y2025: number;
}
// The top rows are pinned with pinnedRows and the left columns with the column's pinned. Used together,
// the "total rows" and the "group / item" columns stay visible whether you scroll horizontally or vertically.
const years = ['y2020', 'y2021', 'y2022', 'y2023', 'y2024', 'y2025'] as const;
const groups = ['Grains', 'Vegetables', 'Fruits'];
const items: Row[] = Array.from({ length: 36 }, (_, i) => {
const base = 40 + ((i * 37) % 260);
return {
id: i + 4,
group: groups[Math.floor(i / 12)],
item: `Item ${String(i + 1).padStart(2, '0')}`,
y2020: base,
y2021: base + 3,
y2022: base + 5,
y2023: base + 6,
y2024: base + 9,
y2025: base + 12,
};
});
// Top three rows: grand total, average, largest value. Computed from the items below.
const pick = (fn: (values: number[]) => number): Pick<Row, (typeof years)[number]> =>
Object.fromEntries(years.map((y) => [y, fn(items.map((row) => row[y]))])) as Pick<
Row,
(typeof years)[number]
>;
const total = (values: number[]) => values.reduce((a, b) => a + b, 0);
const rows: Row[] = [
{ id: 1, group: 'Summary', item: 'Grand total', ...pick(total) },
{ id: 2, group: 'Summary', item: 'Average', ...pick((v) => Math.round(total(v) / v.length)) },
{ id: 3, group: 'Summary', item: 'Maximum', ...pick((v) => Math.max(...v)) },
...items,
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'group', title: 'Group', width: 110, pinned: 'start', merge: true },
{ id: 'item', title: 'Item', width: 140, pinned: 'start' },
...years.map((id) => ({
id,
title: id.slice(1),
kind: 'number' as const,
width: 150,
align: 'end' as const,
})),
],
headerGroups: [
{ id: 'early', title: '2020-2022', children: ['y2020', 'y2021', 'y2022'] },
{ id: 'late', title: '2023-2025', children: ['y2023', 'y2024', 'y2025'] },
],
rows,
rowKey: 'id',
// On a screen narrower than 480px the pins are released and everything scrolls together; they reattach when it gets wider.
pinMinWidth: 480,
pinnedRows: 3,
};
export const actions = {
rowCount: (api: GridApi<Row>, value?: string) =>
api.setOptions({ pinnedRows: Number(value ?? 0) }),
colCount: (api: GridApi<Row>, value?: string) => {
const count = Number(value ?? 0);
const ids = api.getColumns().map((c) => c.id);
// Release from the back so the column order stays the same.
[...ids].reverse().forEach((id) => api.pinColumn(id, null));
ids.slice(0, count).forEach((id) => api.pinColumn(id, 'start'));
},
};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="Pinned rows"
:disabled="!api"
@change="api && actions.rowCount(api, ($event.target as HTMLSelectElement).value)"
>
<option value="3">3 rows</option>
<option value="1">1 row</option>
<option value="0">None</option>
</select>
<select
aria-label="Pinned columns"
:disabled="!api"
@change="api && actions.colCount(api, ($event.target as HTMLSelectElement).value)"
>
<option value="2">2 columns</option>
<option value="1">1 column</option>
<option value="0">None</option>
</select>
</div>
<Grid style="height: 360px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
group: string;
item: string;
y2020: number;
y2021: number;
y2022: number;
y2023: number;
y2024: number;
y2025: number;
}
// The top rows are pinned with pinnedRows and the left columns with the column's pinned. Used together,
// the "total rows" and the "group / item" columns stay visible whether you scroll horizontally or vertically.
const years = ['y2020', 'y2021', 'y2022', 'y2023', 'y2024', 'y2025'] as const;
const groups = ['Grains', 'Vegetables', 'Fruits'];
const items: Row[] = Array.from({ length: 36 }, (_, i) => {
const base = 40 + ((i * 37) % 260);
return {
id: i + 4,
group: groups[Math.floor(i / 12)],
item: `Item ${String(i + 1).padStart(2, '0')}`,
y2020: base,
y2021: base + 3,
y2022: base + 5,
y2023: base + 6,
y2024: base + 9,
y2025: base + 12,
};
});
// Top three rows: grand total, average, largest value. Computed from the items below.
const pick = (fn: (values: number[]) => number): Pick<Row, (typeof years)[number]> =>
Object.fromEntries(years.map((y) => [y, fn(items.map((row) => row[y]))])) as Pick<
Row,
(typeof years)[number]
>;
const total = (values: number[]) => values.reduce((a, b) => a + b, 0);
const rows: Row[] = [
{ id: 1, group: 'Summary', item: 'Grand total', ...pick(total) },
{ id: 2, group: 'Summary', item: 'Average', ...pick((v) => Math.round(total(v) / v.length)) },
{ id: 3, group: 'Summary', item: 'Maximum', ...pick((v) => Math.max(...v)) },
...items,
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'group', title: 'Group', width: 110, pinned: 'start', merge: true },
{ id: 'item', title: 'Item', width: 140, pinned: 'start' },
...years.map((id) => ({
id,
title: id.slice(1),
kind: 'number' as const,
width: 150,
align: 'end' as const,
})),
],
headerGroups: [
{ id: 'early', title: '2020-2022', children: ['y2020', 'y2021', 'y2022'] },
{ id: 'late', title: '2023-2025', children: ['y2023', 'y2024', 'y2025'] },
],
rows,
rowKey: 'id',
// On a screen narrower than 480px the pins are released and everything scrolls together; they reattach when it gets wider.
pinMinWidth: 480,
pinnedRows: 3,
};
export const actions = {
rowCount: (api: GridApi<Row>, value?: string) =>
api.setOptions({ pinnedRows: Number(value ?? 0) }),
colCount: (api: GridApi<Row>, value?: string) => {
const count = Number(value ?? 0);
const ids = api.getColumns().map((c) => c.id);
// Release from the back so the column order stays the same.
[...ids].reverse().forEach((id) => api.pinColumn(id, null));
ids.slice(0, count).forEach((id) => api.pinColumn(id, 'start'));
},
};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="Pinned rows"
[disabled]="!api"
(change)="api && actions.rowCount(api, $any($event.target).value)"
>
<option value="3">3 rows</option>
<option value="1">1 row</option>
<option value="0">None</option>
</select>
<select
aria-label="Pinned columns"
[disabled]="!api"
(change)="api && actions.colCount(api, $any($event.target).value)"
>
<option value="2">2 columns</option>
<option value="1">1 column</option>
<option value="0">None</option>
</select>
</div>
<mx-grid style="height: 360px" [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 { GridApi, GridOptions } from '@maxgrid/angular';
export interface Row {
id: number;
group: string;
item: string;
y2020: number;
y2021: number;
y2022: number;
y2023: number;
y2024: number;
y2025: number;
}
// The top rows are pinned with pinnedRows and the left columns with the column's pinned. Used together,
// the "total rows" and the "group / item" columns stay visible whether you scroll horizontally or vertically.
const years = ['y2020', 'y2021', 'y2022', 'y2023', 'y2024', 'y2025'] as const;
const groups = ['Grains', 'Vegetables', 'Fruits'];
const items: Row[] = Array.from({ length: 36 }, (_, i) => {
const base = 40 + ((i * 37) % 260);
return {
id: i + 4,
group: groups[Math.floor(i / 12)],
item: `Item ${String(i + 1).padStart(2, '0')}`,
y2020: base,
y2021: base + 3,
y2022: base + 5,
y2023: base + 6,
y2024: base + 9,
y2025: base + 12,
};
});
// Top three rows: grand total, average, largest value. Computed from the items below.
const pick = (fn: (values: number[]) => number): Pick<Row, (typeof years)[number]> =>
Object.fromEntries(years.map((y) => [y, fn(items.map((row) => row[y]))])) as Pick<
Row,
(typeof years)[number]
>;
const total = (values: number[]) => values.reduce((a, b) => a + b, 0);
const rows: Row[] = [
{ id: 1, group: 'Summary', item: 'Grand total', ...pick(total) },
{ id: 2, group: 'Summary', item: 'Average', ...pick((v) => Math.round(total(v) / v.length)) },
{ id: 3, group: 'Summary', item: 'Maximum', ...pick((v) => Math.max(...v)) },
...items,
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'group', title: 'Group', width: 110, pinned: 'start', merge: true },
{ id: 'item', title: 'Item', width: 140, pinned: 'start' },
...years.map((id) => ({
id,
title: id.slice(1),
kind: 'number' as const,
width: 150,
align: 'end' as const,
})),
],
headerGroups: [
{ id: 'early', title: '2020-2022', children: ['y2020', 'y2021', 'y2022'] },
{ id: 'late', title: '2023-2025', children: ['y2023', 'y2024', 'y2025'] },
],
rows,
rowKey: 'id',
// On a screen narrower than 480px the pins are released and everything scrolls together; they reattach when it gets wider.
pinMinWidth: 480,
pinnedRows: 3,
};
export const actions = {
rowCount: (api: GridApi<Row>, value?: string) =>
api.setOptions({ pinnedRows: Number(value ?? 0) }),
colCount: (api: GridApi<Row>, value?: string) => {
const count = Number(value ?? 0);
const ids = api.getColumns().map((c) => c.id);
// Release from the back so the column order stays the same.
[...ids].reverse().forEach((id) => api.pinColumn(id, null));
ids.slice(0, count).forEach((id) => api.pinColumn(id, 'start'));
},
};