한 줄짜리 기본 헤더와 넓은 표A plain header on a wide table
가장 기본적인 한 줄 헤더예요. 2년치 월별 매출처럼 컬럼이 25개나 되어도, 왼쪽 지역 컬럼은 고정하고 나머지는 가로로 스크롤해서 볼 수 있어요.The simplest one-line header. Even with 25 columns of monthly sales, the Region column stays pinned while you scroll the rest sideways.
코드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="period">
<option value="all" selected>2022~2023 전체</option>
<option value="2022">2022년만</option>
<option value="2023">2023년만</option>
</select>
<select aria-label="지역 컬럼" id="pin">
<option value="on" selected>왼쪽에 고정</option>
<option value="off">고정 안 함</option>
</select>
</div>
<div id="grid" style="height: 560px"></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('period').addEventListener('change', (event) => actions.period(api, event.target.value));
document.getElementById('pin').addEventListener('change', (event) => actions.pin(api, event.target.value));// 지역별 월 매출(만 원) 2년치. 컬럼이 25개라서 한 화면에 다 안 들어가는 넓은 표예요.
const regions = [
'서울',
'경기·인천',
'강원',
'충북',
'충남',
'대전',
'전북',
'전남',
'광주',
'경북',
'대구',
'경남',
'부산',
'울산',
'제주',
];
const MONTHS = Array.from({ length: 24 }, (_, i) => ({
id: `m${i + 1}`,
year: i < 12 ? 2022 : 2023,
month: (i % 12) + 1,
}));
export const options = {
columns: [
// 지역 컬럼은 왼쪽에 고정해서, 가로로 스크롤해도 어느 지역인지 계속 보여요.
{ id: 'region', title: '지역', width: 110, pinned: 'start' },
...MONTHS.map(({ id, year, month }) => ({
id,
title: `${year}년 ${month}월`,
kind: 'number',
width: 110,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
})),
],
rows: regions.map((region, r) => ({
id: r + 1,
region,
...Object.fromEntries(MONTHS.map(({ id }, i) => [
id,
4000 + ((r * 7919 + i * 2711 + (r + 1) * (i + 3) * 137) % 9000),
])),
})),
rowKey: 'id',
};
export const actions = {
// 보이는 기간을 바꿔요. 숨긴 컬럼은 폭을 기억하고 있다가 다시 보이면 그대로 돌아와요.
period: (api, value) => {
for (const { id, year } of MONTHS) {
api.setColumnVisible(id, value === 'all' || value === undefined || String(year) === value);
}
},
// 지역 컬럼 고정을 켜고 꺼요.
pin: (api, value) => api.pinColumn('region', value === 'off' ? null : '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="period">
<option value="all" selected>2022~2023 전체</option>
<option value="2022">2022년만</option>
<option value="2023">2023년만</option>
</select>
<select aria-label="지역 컬럼" id="pin">
<option value="on" selected>왼쪽에 고정</option>
<option value="off">고정 안 함</option>
</select>
</div>
<div id="grid" style="height: 560px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
// 지역별 월 매출(만 원) 2년치. 컬럼이 25개라서 한 화면에 다 안 들어가는 넓은 표예요.
const regions = [
'서울',
'경기·인천',
'강원',
'충북',
'충남',
'대전',
'전북',
'전남',
'광주',
'경북',
'대구',
'경남',
'부산',
'울산',
'제주',
];
const MONTHS = Array.from({ length: 24 }, (_, i) => ({
id: `m${i + 1}`,
year: i < 12 ? 2022 : 2023,
month: (i % 12) + 1,
}));
const options = {
columns: [
// 지역 컬럼은 왼쪽에 고정해서, 가로로 스크롤해도 어느 지역인지 계속 보여요.
{ id: 'region', title: '지역', width: 110, pinned: 'start' },
...MONTHS.map(({ id, year, month }) => ({
id,
title: `${year}년 ${month}월`,
kind: 'number',
width: 110,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
})),
],
rows: regions.map((region, r) => ({
id: r + 1,
region,
...Object.fromEntries(MONTHS.map(({ id }, i) => [
id,
4000 + ((r * 7919 + i * 2711 + (r + 1) * (i + 3) * 137) % 9000),
])),
})),
rowKey: 'id',
};
const actions = {
// 보이는 기간을 바꿔요. 숨긴 컬럼은 폭을 기억하고 있다가 다시 보이면 그대로 돌아와요.
period: (api, value) => {
for (const { id, year } of MONTHS) {
api.setColumnVisible(id, value === 'all' || value === undefined || String(year) === value);
}
},
// 지역 컬럼 고정을 켜고 꺼요.
pin: (api, value) => api.pinColumn('region', value === 'off' ? null : 'start'),
};
options.ariaLabel = "한 줄짜리 기본 헤더와 넓은 표";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
document.getElementById('period').addEventListener('change', (event) => actions.period(api, event.target.value));
document.getElementById('pin').addEventListener('change', (event) => actions.pin(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.period(api, event.target.value)}
>
<option value="all">2022~2023 전체</option>
<option value="2022">2022년만</option>
<option value="2023">2023년만</option>
</select>
<select defaultValue="all"
aria-label="지역 컬럼"
disabled={!api}
onChange={(event) => api && actions.pin(api, event.target.value)}
>
<option value="on">왼쪽에 고정</option>
<option value="off">고정 안 함</option>
</select>
</div>
<Grid style={{ height: 560 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
[month: string]: string | number;
}
// 지역별 월 매출(만 원) 2년치. 컬럼이 25개라서 한 화면에 다 안 들어가는 넓은 표예요.
const regions = [
'서울',
'경기·인천',
'강원',
'충북',
'충남',
'대전',
'전북',
'전남',
'광주',
'경북',
'대구',
'경남',
'부산',
'울산',
'제주',
];
const MONTHS = Array.from({ length: 24 }, (_, i) => ({
id: `m${i + 1}`,
year: i < 12 ? 2022 : 2023,
month: (i % 12) + 1,
}));
export const options: GridOptions<Row> = {
columns: [
// 지역 컬럼은 왼쪽에 고정해서, 가로로 스크롤해도 어느 지역인지 계속 보여요.
{ id: 'region', title: '지역', width: 110, pinned: 'start' },
...MONTHS.map(({ id, year, month }) => ({
id,
title: `${year}년 ${month}월`,
kind: 'number' as const,
width: 110,
align: 'end' as const,
formatValue: (value: unknown) => Number(value).toLocaleString('ko-KR'),
})),
],
rows: regions.map((region, r) => ({
id: r + 1,
region,
...Object.fromEntries(
MONTHS.map(({ id }, i) => [
id,
4000 + ((r * 7919 + i * 2711 + (r + 1) * (i + 3) * 137) % 9000),
]),
),
})),
rowKey: 'id',
};
export const actions = {
// 보이는 기간을 바꿔요. 숨긴 컬럼은 폭을 기억하고 있다가 다시 보이면 그대로 돌아와요.
period: (api: GridApi<Row>, value?: string) => {
for (const { id, year } of MONTHS) {
api.setColumnVisible(id, value === 'all' || value === undefined || String(year) === value);
}
},
// 지역 컬럼 고정을 켜고 꺼요.
pin: (api: GridApi<Row>, value?: string) =>
api.pinColumn('region', value === 'off' ? null : '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.period(api, ($event.target as HTMLSelectElement).value)"
>
<option value="all" selected>2022~2023 전체</option>
<option value="2022">2022년만</option>
<option value="2023">2023년만</option>
</select>
<select
aria-label="지역 컬럼"
:disabled="!api"
@change="api && actions.pin(api, ($event.target as HTMLSelectElement).value)"
>
<option value="on" selected>왼쪽에 고정</option>
<option value="off">고정 안 함</option>
</select>
</div>
<Grid style="height: 560px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
[month: string]: string | number;
}
// 지역별 월 매출(만 원) 2년치. 컬럼이 25개라서 한 화면에 다 안 들어가는 넓은 표예요.
const regions = [
'서울',
'경기·인천',
'강원',
'충북',
'충남',
'대전',
'전북',
'전남',
'광주',
'경북',
'대구',
'경남',
'부산',
'울산',
'제주',
];
const MONTHS = Array.from({ length: 24 }, (_, i) => ({
id: `m${i + 1}`,
year: i < 12 ? 2022 : 2023,
month: (i % 12) + 1,
}));
export const options: GridOptions<Row> = {
columns: [
// 지역 컬럼은 왼쪽에 고정해서, 가로로 스크롤해도 어느 지역인지 계속 보여요.
{ id: 'region', title: '지역', width: 110, pinned: 'start' },
...MONTHS.map(({ id, year, month }) => ({
id,
title: `${year}년 ${month}월`,
kind: 'number' as const,
width: 110,
align: 'end' as const,
formatValue: (value: unknown) => Number(value).toLocaleString('ko-KR'),
})),
],
rows: regions.map((region, r) => ({
id: r + 1,
region,
...Object.fromEntries(
MONTHS.map(({ id }, i) => [
id,
4000 + ((r * 7919 + i * 2711 + (r + 1) * (i + 3) * 137) % 9000),
]),
),
})),
rowKey: 'id',
};
export const actions = {
// 보이는 기간을 바꿔요. 숨긴 컬럼은 폭을 기억하고 있다가 다시 보이면 그대로 돌아와요.
period: (api: GridApi<Row>, value?: string) => {
for (const { id, year } of MONTHS) {
api.setColumnVisible(id, value === 'all' || value === undefined || String(year) === value);
}
},
// 지역 컬럼 고정을 켜고 꺼요.
pin: (api: GridApi<Row>, value?: string) =>
api.pinColumn('region', value === 'off' ? null : '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.period(api, $any($event.target).value)"
>
<option value="all" selected>2022~2023 전체</option>
<option value="2022">2022년만</option>
<option value="2023">2023년만</option>
</select>
<select
aria-label="지역 컬럼"
[disabled]="!api"
(change)="api && actions.pin(api, $any($event.target).value)"
>
<option value="on" selected>왼쪽에 고정</option>
<option value="off">고정 안 함</option>
</select>
</div>
<mx-grid style="height: 560px" [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 {
[month: string]: string | number;
}
// 지역별 월 매출(만 원) 2년치. 컬럼이 25개라서 한 화면에 다 안 들어가는 넓은 표예요.
const regions = [
'서울',
'경기·인천',
'강원',
'충북',
'충남',
'대전',
'전북',
'전남',
'광주',
'경북',
'대구',
'경남',
'부산',
'울산',
'제주',
];
const MONTHS = Array.from({ length: 24 }, (_, i) => ({
id: `m${i + 1}`,
year: i < 12 ? 2022 : 2023,
month: (i % 12) + 1,
}));
export const options: GridOptions<Row> = {
columns: [
// 지역 컬럼은 왼쪽에 고정해서, 가로로 스크롤해도 어느 지역인지 계속 보여요.
{ id: 'region', title: '지역', width: 110, pinned: 'start' },
...MONTHS.map(({ id, year, month }) => ({
id,
title: `${year}년 ${month}월`,
kind: 'number' as const,
width: 110,
align: 'end' as const,
formatValue: (value: unknown) => Number(value).toLocaleString('ko-KR'),
})),
],
rows: regions.map((region, r) => ({
id: r + 1,
region,
...Object.fromEntries(
MONTHS.map(({ id }, i) => [
id,
4000 + ((r * 7919 + i * 2711 + (r + 1) * (i + 3) * 137) % 9000),
]),
),
})),
rowKey: 'id',
};
export const actions = {
// 보이는 기간을 바꿔요. 숨긴 컬럼은 폭을 기억하고 있다가 다시 보이면 그대로 돌아와요.
period: (api: GridApi<Row>, value?: string) => {
for (const { id, year } of MONTHS) {
api.setColumnVisible(id, value === 'all' || value === undefined || String(year) === value);
}
},
// 지역 컬럼 고정을 켜고 꺼요.
pin: (api: GridApi<Row>, value?: string) =>
api.pinColumn('region', value === 'off' ? null : '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="Period" id="period">
<option value="all" selected>2022-2023</option>
<option value="2022">2022 only</option>
<option value="2023">2023 only</option>
</select>
<select aria-label="Region column" id="pin">
<option value="on" selected>Pinned left</option>
<option value="off">Not pinned</option>
</select>
</div>
<div id="grid" style="height: 560px"></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('period').addEventListener('change', (event) => actions.period(api, event.target.value));
document.getElementById('pin').addEventListener('change', (event) => actions.pin(api, event.target.value));// Two years of monthly sales by region ($). With 25 columns it is a wide table that does not fit on one screen.
const regions = [
'Seoul',
'Gyeonggi-Incheon',
'Gangwon',
'Chungbuk',
'Chungnam',
'Daejeon',
'Jeonbuk',
'Jeonnam',
'Gwangju',
'Gyeongbuk',
'Daegu',
'Gyeongnam',
'Busan',
'Ulsan',
'Jeju',
];
const MONTHS = Array.from({ length: 24 }, (_, i) => ({
id: `m${i + 1}`,
year: i < 12 ? 2022 : 2023,
month: (i % 12) + 1,
}));
const MONTH_NAMES = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
];
export const options = {
columns: [
// The Region column is pinned on the left, so you can always see which region a row is while scrolling sideways.
{ id: 'region', title: 'Region', width: 150, pinned: 'start' },
...MONTHS.map(({ id, year, month }) => ({
id,
title: `${MONTH_NAMES[month - 1]} ${year}`,
kind: 'number',
width: 110,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('en-US'),
})),
],
rows: regions.map((region, r) => ({
id: r + 1,
region,
...Object.fromEntries(MONTHS.map(({ id }, i) => [
id,
10 * (4000 + ((r * 7919 + i * 2711 + (r + 1) * (i + 3) * 137) % 9000)),
])),
})),
rowKey: 'id',
};
export const actions = {
// Change the visible period. Hidden columns remember their width and come back unchanged when shown again.
period: (api, value) => {
for (const { id, year } of MONTHS) {
api.setColumnVisible(id, value === 'all' || value === undefined || String(year) === value);
}
},
// Turn pinning of the Region column on or off.
pin: (api, value) => api.pinColumn('region', value === 'off' ? null : 'start'),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>A plain header on a wide table</title>
</head>
<body>
<div class="toolbar">
<select aria-label="Period" id="period">
<option value="all" selected>2022-2023</option>
<option value="2022">2022 only</option>
<option value="2023">2023 only</option>
</select>
<select aria-label="Region column" id="pin">
<option value="on" selected>Pinned left</option>
<option value="off">Not pinned</option>
</select>
</div>
<div id="grid" style="height: 560px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
// Two years of monthly sales by region ($). With 25 columns it is a wide table that does not fit on one screen.
const regions = [
'Seoul',
'Gyeonggi-Incheon',
'Gangwon',
'Chungbuk',
'Chungnam',
'Daejeon',
'Jeonbuk',
'Jeonnam',
'Gwangju',
'Gyeongbuk',
'Daegu',
'Gyeongnam',
'Busan',
'Ulsan',
'Jeju',
];
const MONTHS = Array.from({ length: 24 }, (_, i) => ({
id: `m${i + 1}`,
year: i < 12 ? 2022 : 2023,
month: (i % 12) + 1,
}));
const MONTH_NAMES = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
];
const options = {
columns: [
// The Region column is pinned on the left, so you can always see which region a row is while scrolling sideways.
{ id: 'region', title: 'Region', width: 150, pinned: 'start' },
...MONTHS.map(({ id, year, month }) => ({
id,
title: `${MONTH_NAMES[month - 1]} ${year}`,
kind: 'number',
width: 110,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('en-US'),
})),
],
rows: regions.map((region, r) => ({
id: r + 1,
region,
...Object.fromEntries(MONTHS.map(({ id }, i) => [
id,
10 * (4000 + ((r * 7919 + i * 2711 + (r + 1) * (i + 3) * 137) % 9000)),
])),
})),
rowKey: 'id',
};
const actions = {
// Change the visible period. Hidden columns remember their width and come back unchanged when shown again.
period: (api, value) => {
for (const { id, year } of MONTHS) {
api.setColumnVisible(id, value === 'all' || value === undefined || String(year) === value);
}
},
// Turn pinning of the Region column on or off.
pin: (api, value) => api.pinColumn('region', value === 'off' ? null : 'start'),
};
options.ariaLabel = "A plain header on a wide table";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
document.getElementById('period').addEventListener('change', (event) => actions.period(api, event.target.value));
document.getElementById('pin').addEventListener('change', (event) => actions.pin(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="Period"
disabled={!api}
onChange={(event) => api && actions.period(api, event.target.value)}
>
<option value="all">2022-2023</option>
<option value="2022">2022 only</option>
<option value="2023">2023 only</option>
</select>
<select defaultValue="all"
aria-label="Region column"
disabled={!api}
onChange={(event) => api && actions.pin(api, event.target.value)}
>
<option value="on">Pinned left</option>
<option value="off">Not pinned</option>
</select>
</div>
<Grid style={{ height: 560 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
[month: string]: string | number;
}
// Two years of monthly sales by region ($). With 25 columns it is a wide table that does not fit on one screen.
const regions = [
'Seoul',
'Gyeonggi-Incheon',
'Gangwon',
'Chungbuk',
'Chungnam',
'Daejeon',
'Jeonbuk',
'Jeonnam',
'Gwangju',
'Gyeongbuk',
'Daegu',
'Gyeongnam',
'Busan',
'Ulsan',
'Jeju',
];
const MONTHS = Array.from({ length: 24 }, (_, i) => ({
id: `m${i + 1}`,
year: i < 12 ? 2022 : 2023,
month: (i % 12) + 1,
}));
const MONTH_NAMES = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
];
export const options: GridOptions<Row> = {
columns: [
// The Region column is pinned on the left, so you can always see which region a row is while scrolling sideways.
{ id: 'region', title: 'Region', width: 150, pinned: 'start' },
...MONTHS.map(({ id, year, month }) => ({
id,
title: `${MONTH_NAMES[month - 1]} ${year}`,
kind: 'number' as const,
width: 110,
align: 'end' as const,
formatValue: (value: unknown) => Number(value).toLocaleString('en-US'),
})),
],
rows: regions.map((region, r) => ({
id: r + 1,
region,
...Object.fromEntries(
MONTHS.map(({ id }, i) => [
id,
10 * (4000 + ((r * 7919 + i * 2711 + (r + 1) * (i + 3) * 137) % 9000)),
]),
),
})),
rowKey: 'id',
};
export const actions = {
// Change the visible period. Hidden columns remember their width and come back unchanged when shown again.
period: (api: GridApi<Row>, value?: string) => {
for (const { id, year } of MONTHS) {
api.setColumnVisible(id, value === 'all' || value === undefined || String(year) === value);
}
},
// Turn pinning of the Region column on or off.
pin: (api: GridApi<Row>, value?: string) =>
api.pinColumn('region', value === 'off' ? null : '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="Period"
:disabled="!api"
@change="api && actions.period(api, ($event.target as HTMLSelectElement).value)"
>
<option value="all" selected>2022-2023</option>
<option value="2022">2022 only</option>
<option value="2023">2023 only</option>
</select>
<select
aria-label="Region column"
:disabled="!api"
@change="api && actions.pin(api, ($event.target as HTMLSelectElement).value)"
>
<option value="on" selected>Pinned left</option>
<option value="off">Not pinned</option>
</select>
</div>
<Grid style="height: 560px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
[month: string]: string | number;
}
// Two years of monthly sales by region ($). With 25 columns it is a wide table that does not fit on one screen.
const regions = [
'Seoul',
'Gyeonggi-Incheon',
'Gangwon',
'Chungbuk',
'Chungnam',
'Daejeon',
'Jeonbuk',
'Jeonnam',
'Gwangju',
'Gyeongbuk',
'Daegu',
'Gyeongnam',
'Busan',
'Ulsan',
'Jeju',
];
const MONTHS = Array.from({ length: 24 }, (_, i) => ({
id: `m${i + 1}`,
year: i < 12 ? 2022 : 2023,
month: (i % 12) + 1,
}));
const MONTH_NAMES = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
];
export const options: GridOptions<Row> = {
columns: [
// The Region column is pinned on the left, so you can always see which region a row is while scrolling sideways.
{ id: 'region', title: 'Region', width: 150, pinned: 'start' },
...MONTHS.map(({ id, year, month }) => ({
id,
title: `${MONTH_NAMES[month - 1]} ${year}`,
kind: 'number' as const,
width: 110,
align: 'end' as const,
formatValue: (value: unknown) => Number(value).toLocaleString('en-US'),
})),
],
rows: regions.map((region, r) => ({
id: r + 1,
region,
...Object.fromEntries(
MONTHS.map(({ id }, i) => [
id,
10 * (4000 + ((r * 7919 + i * 2711 + (r + 1) * (i + 3) * 137) % 9000)),
]),
),
})),
rowKey: 'id',
};
export const actions = {
// Change the visible period. Hidden columns remember their width and come back unchanged when shown again.
period: (api: GridApi<Row>, value?: string) => {
for (const { id, year } of MONTHS) {
api.setColumnVisible(id, value === 'all' || value === undefined || String(year) === value);
}
},
// Turn pinning of the Region column on or off.
pin: (api: GridApi<Row>, value?: string) =>
api.pinColumn('region', value === 'off' ? null : '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="Period"
[disabled]="!api"
(change)="api && actions.period(api, $any($event.target).value)"
>
<option value="all" selected>2022-2023</option>
<option value="2022">2022 only</option>
<option value="2023">2023 only</option>
</select>
<select
aria-label="Region column"
[disabled]="!api"
(change)="api && actions.pin(api, $any($event.target).value)"
>
<option value="on" selected>Pinned left</option>
<option value="off">Not pinned</option>
</select>
</div>
<mx-grid style="height: 560px" [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 {
[month: string]: string | number;
}
// Two years of monthly sales by region ($). With 25 columns it is a wide table that does not fit on one screen.
const regions = [
'Seoul',
'Gyeonggi-Incheon',
'Gangwon',
'Chungbuk',
'Chungnam',
'Daejeon',
'Jeonbuk',
'Jeonnam',
'Gwangju',
'Gyeongbuk',
'Daegu',
'Gyeongnam',
'Busan',
'Ulsan',
'Jeju',
];
const MONTHS = Array.from({ length: 24 }, (_, i) => ({
id: `m${i + 1}`,
year: i < 12 ? 2022 : 2023,
month: (i % 12) + 1,
}));
const MONTH_NAMES = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
];
export const options: GridOptions<Row> = {
columns: [
// The Region column is pinned on the left, so you can always see which region a row is while scrolling sideways.
{ id: 'region', title: 'Region', width: 150, pinned: 'start' },
...MONTHS.map(({ id, year, month }) => ({
id,
title: `${MONTH_NAMES[month - 1]} ${year}`,
kind: 'number' as const,
width: 110,
align: 'end' as const,
formatValue: (value: unknown) => Number(value).toLocaleString('en-US'),
})),
],
rows: regions.map((region, r) => ({
id: r + 1,
region,
...Object.fromEntries(
MONTHS.map(({ id }, i) => [
id,
10 * (4000 + ((r * 7919 + i * 2711 + (r + 1) * (i + 3) * 137) % 9000)),
]),
),
})),
rowKey: 'id',
};
export const actions = {
// Change the visible period. Hidden columns remember their width and come back unchanged when shown again.
period: (api: GridApi<Row>, value?: string) => {
for (const { id, year } of MONTHS) {
api.setColumnVisible(id, value === 'all' || value === undefined || String(year) === value);
}
},
// Turn pinning of the Region column on or off.
pin: (api: GridApi<Row>, value?: string) =>
api.pinColumn('region', value === 'off' ? null : 'start'),
};