눈에 띄는 헤더만 골라서 꾸미기Style only the headers that matter
지역, 1분기, 2월처럼 강조하고 싶은 헤더만 색과 글자를 바꿔요. 컬럼과 그룹 정의에 스타일 이름만 적으면 되고, 색은 테마 색을 써서 밝은 화면과 어두운 화면 모두에서 읽혀요.Restyle only the headers that matter, such as Region, Q1 and February. Just name a style on the column or group; colors come from the theme so they read in light and dark.
코드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="preset">
<option value="mark" selected>세 곳만 강조</option>
<option value="all">헤더 전체</option>
<option value="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, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api);
document.getElementById('preset').addEventListener('change', (event) => actions.preset(api, event.target.value));const regions = ['서울', '경기·인천', '강원', '충북', '충남', '대전', '전북', '전남'];
const MONTHS = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6'];
// headerClass에 CSS 클래스 이름을 적으면 그 헤더 칸에만 붙어요. 컬럼과 그룹 모두 쓸 수 있어요.
// 색은 아래 CSS에서 정해요. 테마 색(변수)으로 만들어서 밝은 화면과 어두운 화면 모두에서 읽혀요.
const CSS = `
.hs-blue { background: var(--mxg-accent); color: var(--mxg-accent-fg); font-weight: 700; }
.hs-green { background: var(--mxg-tone-success-bg); color: var(--mxg-tone-success-fg); font-weight: 700; }
.hs-yellow { background: var(--mxg-tone-warning-bg); color: var(--mxg-tone-warning-fg); font-weight: 700; }
.hs-all { background: color-mix(in srgb, var(--mxg-accent) 20%, var(--mxg-bg)); font-weight: 700; }
`;
// 꾸밀 헤더와 클래스를 정해 둔 표예요. 'mark'는 세 곳만, 'all'은 모든 헤더, 'none'은 꾸미지 않아요.
const CLASSES = {
mark: { col: { region: 'hs-blue', m2: 'hs-yellow' }, group: { q1: 'hs-green' } },
all: {
col: Object.fromEntries(['region', ...MONTHS].map((id) => [id, 'hs-all'])),
group: { q1: 'hs-all', q2: 'hs-all' },
},
none: { col: {}, group: {} },
};
const columns = (preset) => [
{ id: 'region', title: '지역', width: 110, headerClass: CLASSES[preset]?.col.region },
...MONTHS.map((id, i) => ({
id,
title: `${i + 1}월`,
kind: 'number',
width: 90,
align: 'end',
headerClass: CLASSES[preset]?.col[id],
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
})),
];
const groups = (preset) => [
{
id: 'q1',
title: '1분기',
children: ['m1', 'm2', 'm3'],
headerClass: CLASSES[preset]?.group.q1,
},
{
id: 'q2',
title: '2분기',
children: ['m4', 'm5', 'm6'],
headerClass: CLASSES[preset]?.group.q2,
},
];
export const options = {
columns: columns('mark'),
headerGroups: groups('mark'),
rows: regions.map((region, r) => {
const value = (i) => 3200 + ((r * 5113 + i * 1993 + r * i * 211) % 7000);
return {
id: r + 1,
region,
m1: value(0),
m2: value(1),
m3: value(2),
m4: value(3),
m5: value(4),
m6: value(5),
};
}),
rowKey: 'id',
};
let style;
export function setup(_api) {
style = document.createElement('style');
style.textContent = CSS;
document.head.append(style);
return () => {
style?.remove();
style = undefined;
};
}
export const actions = {
preset: (api, value) => {
const preset = value && CLASSES[value] ? value : 'mark';
api.setColumns(columns(preset));
api.setOptions({ headerGroups: groups(preset) });
},
};<!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="preset">
<option value="mark" selected>세 곳만 강조</option>
<option value="all">헤더 전체</option>
<option value="none">꾸미지 않기</option>
</select>
</div>
<div id="grid" style="height: 360px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const regions = ['서울', '경기·인천', '강원', '충북', '충남', '대전', '전북', '전남'];
const MONTHS = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6'];
// headerClass에 CSS 클래스 이름을 적으면 그 헤더 칸에만 붙어요. 컬럼과 그룹 모두 쓸 수 있어요.
// 색은 아래 CSS에서 정해요. 테마 색(변수)으로 만들어서 밝은 화면과 어두운 화면 모두에서 읽혀요.
const CSS = `
.hs-blue { background: var(--mxg-accent); color: var(--mxg-accent-fg); font-weight: 700; }
.hs-green { background: var(--mxg-tone-success-bg); color: var(--mxg-tone-success-fg); font-weight: 700; }
.hs-yellow { background: var(--mxg-tone-warning-bg); color: var(--mxg-tone-warning-fg); font-weight: 700; }
.hs-all { background: color-mix(in srgb, var(--mxg-accent) 20%, var(--mxg-bg)); font-weight: 700; }
`;
// 꾸밀 헤더와 클래스를 정해 둔 표예요. 'mark'는 세 곳만, 'all'은 모든 헤더, 'none'은 꾸미지 않아요.
const CLASSES = {
mark: { col: { region: 'hs-blue', m2: 'hs-yellow' }, group: { q1: 'hs-green' } },
all: {
col: Object.fromEntries(['region', ...MONTHS].map((id) => [id, 'hs-all'])),
group: { q1: 'hs-all', q2: 'hs-all' },
},
none: { col: {}, group: {} },
};
const columns = (preset) => [
{ id: 'region', title: '지역', width: 110, headerClass: CLASSES[preset]?.col.region },
...MONTHS.map((id, i) => ({
id,
title: `${i + 1}월`,
kind: 'number',
width: 90,
align: 'end',
headerClass: CLASSES[preset]?.col[id],
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
})),
];
const groups = (preset) => [
{
id: 'q1',
title: '1분기',
children: ['m1', 'm2', 'm3'],
headerClass: CLASSES[preset]?.group.q1,
},
{
id: 'q2',
title: '2분기',
children: ['m4', 'm5', 'm6'],
headerClass: CLASSES[preset]?.group.q2,
},
];
const options = {
columns: columns('mark'),
headerGroups: groups('mark'),
rows: regions.map((region, r) => {
const value = (i) => 3200 + ((r * 5113 + i * 1993 + r * i * 211) % 7000);
return {
id: r + 1,
region,
m1: value(0),
m2: value(1),
m3: value(2),
m4: value(3),
m5: value(4),
m6: value(5),
};
}),
rowKey: 'id',
};
let style;
function setup(_api) {
style = document.createElement('style');
style.textContent = CSS;
document.head.append(style);
return () => {
style?.remove();
style = undefined;
};
}
const actions = {
preset: (api, value) => {
const preset = value && CLASSES[value] ? value : 'mark';
api.setColumns(columns(preset));
api.setOptions({ headerGroups: groups(preset) });
},
};
options.ariaLabel = "눈에 띄는 헤더만 골라서 꾸미기";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api);
document.getElementById('preset').addEventListener('change', (event) => actions.preset(api, event.target.value));
</script>
</body>
</html>버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api) : undefined), [api]);
return (
<>
<div className="toolbar">
<select
aria-label="헤더 꾸미기"
disabled={!api}
onChange={(event) => api && actions.preset(api, event.target.value)}
>
<option value="mark">세 곳만 강조</option>
<option value="all">헤더 전체</option>
<option value="none">꾸미지 않기</option>
</select>
</div>
<Grid style={{ height: 360 }} ref={gridRef} options={options} />
</>
);
}import type { ColumnDef, GridApi, GridOptions, HeaderGroupDef } from '@maxgrid/react';
export interface Row {
id: number;
region: string;
m1: number;
m2: number;
m3: number;
m4: number;
m5: number;
m6: number;
}
const regions = ['서울', '경기·인천', '강원', '충북', '충남', '대전', '전북', '전남'];
const MONTHS = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6'] as const;
// headerClass에 CSS 클래스 이름을 적으면 그 헤더 칸에만 붙어요. 컬럼과 그룹 모두 쓸 수 있어요.
// 색은 아래 CSS에서 정해요. 테마 색(변수)으로 만들어서 밝은 화면과 어두운 화면 모두에서 읽혀요.
const CSS = `
.hs-blue { background: var(--mxg-accent); color: var(--mxg-accent-fg); font-weight: 700; }
.hs-green { background: var(--mxg-tone-success-bg); color: var(--mxg-tone-success-fg); font-weight: 700; }
.hs-yellow { background: var(--mxg-tone-warning-bg); color: var(--mxg-tone-warning-fg); font-weight: 700; }
.hs-all { background: color-mix(in srgb, var(--mxg-accent) 20%, var(--mxg-bg)); font-weight: 700; }
`;
// 꾸밀 헤더와 클래스를 정해 둔 표예요. 'mark'는 세 곳만, 'all'은 모든 헤더, 'none'은 꾸미지 않아요.
const CLASSES: Record<string, { col: Record<string, string>; group: Record<string, string> }> = {
mark: { col: { region: 'hs-blue', m2: 'hs-yellow' }, group: { q1: 'hs-green' } },
all: {
col: Object.fromEntries(['region', ...MONTHS].map((id) => [id, 'hs-all'])),
group: { q1: 'hs-all', q2: 'hs-all' },
},
none: { col: {}, group: {} },
};
const columns = (preset: string): ColumnDef<Row>[] => [
{ id: 'region', title: '지역', width: 110, headerClass: CLASSES[preset]?.col.region },
...MONTHS.map((id, i) => ({
id,
title: `${i + 1}월`,
kind: 'number' as const,
width: 90,
align: 'end' as const,
headerClass: CLASSES[preset]?.col[id],
formatValue: (value: unknown) => Number(value).toLocaleString('ko-KR'),
})),
];
const groups = (preset: string): HeaderGroupDef[] => [
{
id: 'q1',
title: '1분기',
children: ['m1', 'm2', 'm3'],
headerClass: CLASSES[preset]?.group.q1,
},
{
id: 'q2',
title: '2분기',
children: ['m4', 'm5', 'm6'],
headerClass: CLASSES[preset]?.group.q2,
},
];
export const options: GridOptions<Row> = {
columns: columns('mark'),
headerGroups: groups('mark'),
rows: regions.map((region, r) => {
const value = (i: number): number => 3200 + ((r * 5113 + i * 1993 + r * i * 211) % 7000);
return {
id: r + 1,
region,
m1: value(0),
m2: value(1),
m3: value(2),
m4: value(3),
m5: value(4),
m6: value(5),
};
}),
rowKey: 'id',
};
let style: HTMLStyleElement | undefined;
export function setup(_api: GridApi<Row>) {
style = document.createElement('style');
style.textContent = CSS;
document.head.append(style);
return () => {
style?.remove();
style = undefined;
};
}
export const actions = {
preset: (api: GridApi<Row>, value?: string) => {
const preset = value && CLASSES[value] ? value : 'mark';
api.setColumns(columns(preset));
api.setOptions({ headerGroups: groups(preset) });
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<select
aria-label="헤더 꾸미기"
:disabled="!api"
@change="api && actions.preset(api, ($event.target as HTMLSelectElement).value)"
>
<option value="mark" selected>세 곳만 강조</option>
<option value="all">헤더 전체</option>
<option value="none">꾸미지 않기</option>
</select>
</div>
<Grid style="height: 360px" :options="options" @ready="onReady" />
</template>import type { ColumnDef, GridApi, GridOptions, HeaderGroupDef } from '@maxgrid/vue';
export interface Row {
id: number;
region: string;
m1: number;
m2: number;
m3: number;
m4: number;
m5: number;
m6: number;
}
const regions = ['서울', '경기·인천', '강원', '충북', '충남', '대전', '전북', '전남'];
const MONTHS = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6'] as const;
// headerClass에 CSS 클래스 이름을 적으면 그 헤더 칸에만 붙어요. 컬럼과 그룹 모두 쓸 수 있어요.
// 색은 아래 CSS에서 정해요. 테마 색(변수)으로 만들어서 밝은 화면과 어두운 화면 모두에서 읽혀요.
const CSS = `
.hs-blue { background: var(--mxg-accent); color: var(--mxg-accent-fg); font-weight: 700; }
.hs-green { background: var(--mxg-tone-success-bg); color: var(--mxg-tone-success-fg); font-weight: 700; }
.hs-yellow { background: var(--mxg-tone-warning-bg); color: var(--mxg-tone-warning-fg); font-weight: 700; }
.hs-all { background: color-mix(in srgb, var(--mxg-accent) 20%, var(--mxg-bg)); font-weight: 700; }
`;
// 꾸밀 헤더와 클래스를 정해 둔 표예요. 'mark'는 세 곳만, 'all'은 모든 헤더, 'none'은 꾸미지 않아요.
const CLASSES: Record<string, { col: Record<string, string>; group: Record<string, string> }> = {
mark: { col: { region: 'hs-blue', m2: 'hs-yellow' }, group: { q1: 'hs-green' } },
all: {
col: Object.fromEntries(['region', ...MONTHS].map((id) => [id, 'hs-all'])),
group: { q1: 'hs-all', q2: 'hs-all' },
},
none: { col: {}, group: {} },
};
const columns = (preset: string): ColumnDef<Row>[] => [
{ id: 'region', title: '지역', width: 110, headerClass: CLASSES[preset]?.col.region },
...MONTHS.map((id, i) => ({
id,
title: `${i + 1}월`,
kind: 'number' as const,
width: 90,
align: 'end' as const,
headerClass: CLASSES[preset]?.col[id],
formatValue: (value: unknown) => Number(value).toLocaleString('ko-KR'),
})),
];
const groups = (preset: string): HeaderGroupDef[] => [
{
id: 'q1',
title: '1분기',
children: ['m1', 'm2', 'm3'],
headerClass: CLASSES[preset]?.group.q1,
},
{
id: 'q2',
title: '2분기',
children: ['m4', 'm5', 'm6'],
headerClass: CLASSES[preset]?.group.q2,
},
];
export const options: GridOptions<Row> = {
columns: columns('mark'),
headerGroups: groups('mark'),
rows: regions.map((region, r) => {
const value = (i: number): number => 3200 + ((r * 5113 + i * 1993 + r * i * 211) % 7000);
return {
id: r + 1,
region,
m1: value(0),
m2: value(1),
m3: value(2),
m4: value(3),
m5: value(4),
m6: value(5),
};
}),
rowKey: 'id',
};
let style: HTMLStyleElement | undefined;
export function setup(_api: GridApi<Row>) {
style = document.createElement('style');
style.textContent = CSS;
document.head.append(style);
return () => {
style?.remove();
style = undefined;
};
}
export const actions = {
preset: (api: GridApi<Row>, value?: string) => {
const preset = value && CLASSES[value] ? value : 'mark';
api.setColumns(columns(preset));
api.setOptions({ headerGroups: groups(preset) });
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<select
aria-label="헤더 꾸미기"
[disabled]="!api"
(change)="api && actions.preset(api, $any($event.target).value)"
>
<option value="mark" selected>세 곳만 강조</option>
<option value="all">헤더 전체</option>
<option value="none">꾸미지 않기</option>
</select>
</div>
<mx-grid style="height: 360px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { ColumnDef, GridApi, GridOptions, HeaderGroupDef } from '@maxgrid/angular';
export interface Row {
id: number;
region: string;
m1: number;
m2: number;
m3: number;
m4: number;
m5: number;
m6: number;
}
const regions = ['서울', '경기·인천', '강원', '충북', '충남', '대전', '전북', '전남'];
const MONTHS = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6'] as const;
// headerClass에 CSS 클래스 이름을 적으면 그 헤더 칸에만 붙어요. 컬럼과 그룹 모두 쓸 수 있어요.
// 색은 아래 CSS에서 정해요. 테마 색(변수)으로 만들어서 밝은 화면과 어두운 화면 모두에서 읽혀요.
const CSS = `
.hs-blue { background: var(--mxg-accent); color: var(--mxg-accent-fg); font-weight: 700; }
.hs-green { background: var(--mxg-tone-success-bg); color: var(--mxg-tone-success-fg); font-weight: 700; }
.hs-yellow { background: var(--mxg-tone-warning-bg); color: var(--mxg-tone-warning-fg); font-weight: 700; }
.hs-all { background: color-mix(in srgb, var(--mxg-accent) 20%, var(--mxg-bg)); font-weight: 700; }
`;
// 꾸밀 헤더와 클래스를 정해 둔 표예요. 'mark'는 세 곳만, 'all'은 모든 헤더, 'none'은 꾸미지 않아요.
const CLASSES: Record<string, { col: Record<string, string>; group: Record<string, string> }> = {
mark: { col: { region: 'hs-blue', m2: 'hs-yellow' }, group: { q1: 'hs-green' } },
all: {
col: Object.fromEntries(['region', ...MONTHS].map((id) => [id, 'hs-all'])),
group: { q1: 'hs-all', q2: 'hs-all' },
},
none: { col: {}, group: {} },
};
const columns = (preset: string): ColumnDef<Row>[] => [
{ id: 'region', title: '지역', width: 110, headerClass: CLASSES[preset]?.col.region },
...MONTHS.map((id, i) => ({
id,
title: `${i + 1}월`,
kind: 'number' as const,
width: 90,
align: 'end' as const,
headerClass: CLASSES[preset]?.col[id],
formatValue: (value: unknown) => Number(value).toLocaleString('ko-KR'),
})),
];
const groups = (preset: string): HeaderGroupDef[] => [
{
id: 'q1',
title: '1분기',
children: ['m1', 'm2', 'm3'],
headerClass: CLASSES[preset]?.group.q1,
},
{
id: 'q2',
title: '2분기',
children: ['m4', 'm5', 'm6'],
headerClass: CLASSES[preset]?.group.q2,
},
];
export const options: GridOptions<Row> = {
columns: columns('mark'),
headerGroups: groups('mark'),
rows: regions.map((region, r) => {
const value = (i: number): number => 3200 + ((r * 5113 + i * 1993 + r * i * 211) % 7000);
return {
id: r + 1,
region,
m1: value(0),
m2: value(1),
m3: value(2),
m4: value(3),
m5: value(4),
m6: value(5),
};
}),
rowKey: 'id',
};
let style: HTMLStyleElement | undefined;
export function setup(_api: GridApi<Row>) {
style = document.createElement('style');
style.textContent = CSS;
document.head.append(style);
return () => {
style?.remove();
style = undefined;
};
}
export const actions = {
preset: (api: GridApi<Row>, value?: string) => {
const preset = value && CLASSES[value] ? value : 'mark';
api.setColumns(columns(preset));
api.setOptions({ headerGroups: groups(preset) });
},
};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="Header style" id="preset">
<option value="mark" selected>Highlight three</option>
<option value="all">All headers</option>
<option value="none">No styling</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, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api);
document.getElementById('preset').addEventListener('change', (event) => actions.preset(api, event.target.value));const regions = [
'Seoul',
'Gyeonggi-Incheon',
'Gangwon',
'Chungbuk',
'Chungnam',
'Daejeon',
'Jeonbuk',
'Jeonnam',
];
const MONTHS = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6'];
// A CSS class name in headerClass is added only to that header cell. Both columns and groups can use it.
// The colors are set in the CSS below. They use theme colors (variables), so they read well on both light and dark screens.
const CSS = `
.hs-blue { background: var(--mxg-accent); color: var(--mxg-accent-fg); font-weight: 700; }
.hs-green { background: var(--mxg-tone-success-bg); color: var(--mxg-tone-success-fg); font-weight: 700; }
.hs-yellow { background: var(--mxg-tone-warning-bg); color: var(--mxg-tone-warning-fg); font-weight: 700; }
.hs-all { background: color-mix(in srgb, var(--mxg-accent) 20%, var(--mxg-bg)); font-weight: 700; }
`;
// A table of which headers get which class. 'mark' styles three spots, 'all' every header, 'none' nothing.
const CLASSES = {
mark: { col: { region: 'hs-blue', m2: 'hs-yellow' }, group: { q1: 'hs-green' } },
all: {
col: Object.fromEntries(['region', ...MONTHS].map((id) => [id, 'hs-all'])),
group: { q1: 'hs-all', q2: 'hs-all' },
},
none: { col: {}, group: {} },
};
const columns = (preset) => [
{ id: 'region', title: 'Region', width: 150, headerClass: CLASSES[preset]?.col.region },
...MONTHS.map((id, i) => ({
id,
title: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'][i],
kind: 'number',
width: 90,
align: 'end',
headerClass: CLASSES[preset]?.col[id],
formatValue: (value) => Number(value).toLocaleString('en-US'),
})),
];
const groups = (preset) => [
{
id: 'q1',
title: 'Q1',
children: ['m1', 'm2', 'm3'],
headerClass: CLASSES[preset]?.group.q1,
},
{
id: 'q2',
title: 'Q2',
children: ['m4', 'm5', 'm6'],
headerClass: CLASSES[preset]?.group.q2,
},
];
export const options = {
columns: columns('mark'),
headerGroups: groups('mark'),
rows: regions.map((region, r) => {
const value = (i) => 3200 + ((r * 5113 + i * 1993 + r * i * 211) % 7000);
return {
id: r + 1,
region,
m1: value(0),
m2: value(1),
m3: value(2),
m4: value(3),
m5: value(4),
m6: value(5),
};
}),
rowKey: 'id',
};
let style;
export function setup(_api) {
style = document.createElement('style');
style.textContent = CSS;
document.head.append(style);
return () => {
style?.remove();
style = undefined;
};
}
export const actions = {
preset: (api, value) => {
const preset = value && CLASSES[value] ? value : 'mark';
api.setColumns(columns(preset));
api.setOptions({ headerGroups: groups(preset) });
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Style only the headers that matter</title>
</head>
<body>
<div class="toolbar">
<select aria-label="Header style" id="preset">
<option value="mark" selected>Highlight three</option>
<option value="all">All headers</option>
<option value="none">No styling</option>
</select>
</div>
<div id="grid" style="height: 360px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const regions = [
'Seoul',
'Gyeonggi-Incheon',
'Gangwon',
'Chungbuk',
'Chungnam',
'Daejeon',
'Jeonbuk',
'Jeonnam',
];
const MONTHS = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6'];
// A CSS class name in headerClass is added only to that header cell. Both columns and groups can use it.
// The colors are set in the CSS below. They use theme colors (variables), so they read well on both light and dark screens.
const CSS = `
.hs-blue { background: var(--mxg-accent); color: var(--mxg-accent-fg); font-weight: 700; }
.hs-green { background: var(--mxg-tone-success-bg); color: var(--mxg-tone-success-fg); font-weight: 700; }
.hs-yellow { background: var(--mxg-tone-warning-bg); color: var(--mxg-tone-warning-fg); font-weight: 700; }
.hs-all { background: color-mix(in srgb, var(--mxg-accent) 20%, var(--mxg-bg)); font-weight: 700; }
`;
// A table of which headers get which class. 'mark' styles three spots, 'all' every header, 'none' nothing.
const CLASSES = {
mark: { col: { region: 'hs-blue', m2: 'hs-yellow' }, group: { q1: 'hs-green' } },
all: {
col: Object.fromEntries(['region', ...MONTHS].map((id) => [id, 'hs-all'])),
group: { q1: 'hs-all', q2: 'hs-all' },
},
none: { col: {}, group: {} },
};
const columns = (preset) => [
{ id: 'region', title: 'Region', width: 150, headerClass: CLASSES[preset]?.col.region },
...MONTHS.map((id, i) => ({
id,
title: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'][i],
kind: 'number',
width: 90,
align: 'end',
headerClass: CLASSES[preset]?.col[id],
formatValue: (value) => Number(value).toLocaleString('en-US'),
})),
];
const groups = (preset) => [
{
id: 'q1',
title: 'Q1',
children: ['m1', 'm2', 'm3'],
headerClass: CLASSES[preset]?.group.q1,
},
{
id: 'q2',
title: 'Q2',
children: ['m4', 'm5', 'm6'],
headerClass: CLASSES[preset]?.group.q2,
},
];
const options = {
columns: columns('mark'),
headerGroups: groups('mark'),
rows: regions.map((region, r) => {
const value = (i) => 3200 + ((r * 5113 + i * 1993 + r * i * 211) % 7000);
return {
id: r + 1,
region,
m1: value(0),
m2: value(1),
m3: value(2),
m4: value(3),
m5: value(4),
m6: value(5),
};
}),
rowKey: 'id',
};
let style;
function setup(_api) {
style = document.createElement('style');
style.textContent = CSS;
document.head.append(style);
return () => {
style?.remove();
style = undefined;
};
}
const actions = {
preset: (api, value) => {
const preset = value && CLASSES[value] ? value : 'mark';
api.setColumns(columns(preset));
api.setOptions({ headerGroups: groups(preset) });
},
};
options.ariaLabel = "Style only the headers that matter";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api);
document.getElementById('preset').addEventListener('change', (event) => actions.preset(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 { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api) : undefined), [api]);
return (
<>
<div className="toolbar">
<select
aria-label="Header style"
disabled={!api}
onChange={(event) => api && actions.preset(api, event.target.value)}
>
<option value="mark">Highlight three</option>
<option value="all">All headers</option>
<option value="none">No styling</option>
</select>
</div>
<Grid style={{ height: 360 }} ref={gridRef} options={options} />
</>
);
}import type { ColumnDef, GridApi, GridOptions, HeaderGroupDef } from '@maxgrid/react';
export interface Row {
id: number;
region: string;
m1: number;
m2: number;
m3: number;
m4: number;
m5: number;
m6: number;
}
const regions = [
'Seoul',
'Gyeonggi-Incheon',
'Gangwon',
'Chungbuk',
'Chungnam',
'Daejeon',
'Jeonbuk',
'Jeonnam',
];
const MONTHS = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6'] as const;
// A CSS class name in headerClass is added only to that header cell. Both columns and groups can use it.
// The colors are set in the CSS below. They use theme colors (variables), so they read well on both light and dark screens.
const CSS = `
.hs-blue { background: var(--mxg-accent); color: var(--mxg-accent-fg); font-weight: 700; }
.hs-green { background: var(--mxg-tone-success-bg); color: var(--mxg-tone-success-fg); font-weight: 700; }
.hs-yellow { background: var(--mxg-tone-warning-bg); color: var(--mxg-tone-warning-fg); font-weight: 700; }
.hs-all { background: color-mix(in srgb, var(--mxg-accent) 20%, var(--mxg-bg)); font-weight: 700; }
`;
// A table of which headers get which class. 'mark' styles three spots, 'all' every header, 'none' nothing.
const CLASSES: Record<string, { col: Record<string, string>; group: Record<string, string> }> = {
mark: { col: { region: 'hs-blue', m2: 'hs-yellow' }, group: { q1: 'hs-green' } },
all: {
col: Object.fromEntries(['region', ...MONTHS].map((id) => [id, 'hs-all'])),
group: { q1: 'hs-all', q2: 'hs-all' },
},
none: { col: {}, group: {} },
};
const columns = (preset: string): ColumnDef<Row>[] => [
{ id: 'region', title: 'Region', width: 150, headerClass: CLASSES[preset]?.col.region },
...MONTHS.map((id, i) => ({
id,
title: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'][i] as string,
kind: 'number' as const,
width: 90,
align: 'end' as const,
headerClass: CLASSES[preset]?.col[id],
formatValue: (value: unknown) => Number(value).toLocaleString('en-US'),
})),
];
const groups = (preset: string): HeaderGroupDef[] => [
{
id: 'q1',
title: 'Q1',
children: ['m1', 'm2', 'm3'],
headerClass: CLASSES[preset]?.group.q1,
},
{
id: 'q2',
title: 'Q2',
children: ['m4', 'm5', 'm6'],
headerClass: CLASSES[preset]?.group.q2,
},
];
export const options: GridOptions<Row> = {
columns: columns('mark'),
headerGroups: groups('mark'),
rows: regions.map((region, r) => {
const value = (i: number): number => 3200 + ((r * 5113 + i * 1993 + r * i * 211) % 7000);
return {
id: r + 1,
region,
m1: value(0),
m2: value(1),
m3: value(2),
m4: value(3),
m5: value(4),
m6: value(5),
};
}),
rowKey: 'id',
};
let style: HTMLStyleElement | undefined;
export function setup(_api: GridApi<Row>) {
style = document.createElement('style');
style.textContent = CSS;
document.head.append(style);
return () => {
style?.remove();
style = undefined;
};
}
export const actions = {
preset: (api: GridApi<Row>, value?: string) => {
const preset = value && CLASSES[value] ? value : 'mark';
api.setColumns(columns(preset));
api.setOptions({ headerGroups: groups(preset) });
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<select
aria-label="Header style"
:disabled="!api"
@change="api && actions.preset(api, ($event.target as HTMLSelectElement).value)"
>
<option value="mark" selected>Highlight three</option>
<option value="all">All headers</option>
<option value="none">No styling</option>
</select>
</div>
<Grid style="height: 360px" :options="options" @ready="onReady" />
</template>import type { ColumnDef, GridApi, GridOptions, HeaderGroupDef } from '@maxgrid/vue';
export interface Row {
id: number;
region: string;
m1: number;
m2: number;
m3: number;
m4: number;
m5: number;
m6: number;
}
const regions = [
'Seoul',
'Gyeonggi-Incheon',
'Gangwon',
'Chungbuk',
'Chungnam',
'Daejeon',
'Jeonbuk',
'Jeonnam',
];
const MONTHS = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6'] as const;
// A CSS class name in headerClass is added only to that header cell. Both columns and groups can use it.
// The colors are set in the CSS below. They use theme colors (variables), so they read well on both light and dark screens.
const CSS = `
.hs-blue { background: var(--mxg-accent); color: var(--mxg-accent-fg); font-weight: 700; }
.hs-green { background: var(--mxg-tone-success-bg); color: var(--mxg-tone-success-fg); font-weight: 700; }
.hs-yellow { background: var(--mxg-tone-warning-bg); color: var(--mxg-tone-warning-fg); font-weight: 700; }
.hs-all { background: color-mix(in srgb, var(--mxg-accent) 20%, var(--mxg-bg)); font-weight: 700; }
`;
// A table of which headers get which class. 'mark' styles three spots, 'all' every header, 'none' nothing.
const CLASSES: Record<string, { col: Record<string, string>; group: Record<string, string> }> = {
mark: { col: { region: 'hs-blue', m2: 'hs-yellow' }, group: { q1: 'hs-green' } },
all: {
col: Object.fromEntries(['region', ...MONTHS].map((id) => [id, 'hs-all'])),
group: { q1: 'hs-all', q2: 'hs-all' },
},
none: { col: {}, group: {} },
};
const columns = (preset: string): ColumnDef<Row>[] => [
{ id: 'region', title: 'Region', width: 150, headerClass: CLASSES[preset]?.col.region },
...MONTHS.map((id, i) => ({
id,
title: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'][i] as string,
kind: 'number' as const,
width: 90,
align: 'end' as const,
headerClass: CLASSES[preset]?.col[id],
formatValue: (value: unknown) => Number(value).toLocaleString('en-US'),
})),
];
const groups = (preset: string): HeaderGroupDef[] => [
{
id: 'q1',
title: 'Q1',
children: ['m1', 'm2', 'm3'],
headerClass: CLASSES[preset]?.group.q1,
},
{
id: 'q2',
title: 'Q2',
children: ['m4', 'm5', 'm6'],
headerClass: CLASSES[preset]?.group.q2,
},
];
export const options: GridOptions<Row> = {
columns: columns('mark'),
headerGroups: groups('mark'),
rows: regions.map((region, r) => {
const value = (i: number): number => 3200 + ((r * 5113 + i * 1993 + r * i * 211) % 7000);
return {
id: r + 1,
region,
m1: value(0),
m2: value(1),
m3: value(2),
m4: value(3),
m5: value(4),
m6: value(5),
};
}),
rowKey: 'id',
};
let style: HTMLStyleElement | undefined;
export function setup(_api: GridApi<Row>) {
style = document.createElement('style');
style.textContent = CSS;
document.head.append(style);
return () => {
style?.remove();
style = undefined;
};
}
export const actions = {
preset: (api: GridApi<Row>, value?: string) => {
const preset = value && CLASSES[value] ? value : 'mark';
api.setColumns(columns(preset));
api.setOptions({ headerGroups: groups(preset) });
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<select
aria-label="Header style"
[disabled]="!api"
(change)="api && actions.preset(api, $any($event.target).value)"
>
<option value="mark" selected>Highlight three</option>
<option value="all">All headers</option>
<option value="none">No styling</option>
</select>
</div>
<mx-grid style="height: 360px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { ColumnDef, GridApi, GridOptions, HeaderGroupDef } from '@maxgrid/angular';
export interface Row {
id: number;
region: string;
m1: number;
m2: number;
m3: number;
m4: number;
m5: number;
m6: number;
}
const regions = [
'Seoul',
'Gyeonggi-Incheon',
'Gangwon',
'Chungbuk',
'Chungnam',
'Daejeon',
'Jeonbuk',
'Jeonnam',
];
const MONTHS = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6'] as const;
// A CSS class name in headerClass is added only to that header cell. Both columns and groups can use it.
// The colors are set in the CSS below. They use theme colors (variables), so they read well on both light and dark screens.
const CSS = `
.hs-blue { background: var(--mxg-accent); color: var(--mxg-accent-fg); font-weight: 700; }
.hs-green { background: var(--mxg-tone-success-bg); color: var(--mxg-tone-success-fg); font-weight: 700; }
.hs-yellow { background: var(--mxg-tone-warning-bg); color: var(--mxg-tone-warning-fg); font-weight: 700; }
.hs-all { background: color-mix(in srgb, var(--mxg-accent) 20%, var(--mxg-bg)); font-weight: 700; }
`;
// A table of which headers get which class. 'mark' styles three spots, 'all' every header, 'none' nothing.
const CLASSES: Record<string, { col: Record<string, string>; group: Record<string, string> }> = {
mark: { col: { region: 'hs-blue', m2: 'hs-yellow' }, group: { q1: 'hs-green' } },
all: {
col: Object.fromEntries(['region', ...MONTHS].map((id) => [id, 'hs-all'])),
group: { q1: 'hs-all', q2: 'hs-all' },
},
none: { col: {}, group: {} },
};
const columns = (preset: string): ColumnDef<Row>[] => [
{ id: 'region', title: 'Region', width: 150, headerClass: CLASSES[preset]?.col.region },
...MONTHS.map((id, i) => ({
id,
title: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'][i] as string,
kind: 'number' as const,
width: 90,
align: 'end' as const,
headerClass: CLASSES[preset]?.col[id],
formatValue: (value: unknown) => Number(value).toLocaleString('en-US'),
})),
];
const groups = (preset: string): HeaderGroupDef[] => [
{
id: 'q1',
title: 'Q1',
children: ['m1', 'm2', 'm3'],
headerClass: CLASSES[preset]?.group.q1,
},
{
id: 'q2',
title: 'Q2',
children: ['m4', 'm5', 'm6'],
headerClass: CLASSES[preset]?.group.q2,
},
];
export const options: GridOptions<Row> = {
columns: columns('mark'),
headerGroups: groups('mark'),
rows: regions.map((region, r) => {
const value = (i: number): number => 3200 + ((r * 5113 + i * 1993 + r * i * 211) % 7000);
return {
id: r + 1,
region,
m1: value(0),
m2: value(1),
m3: value(2),
m4: value(3),
m5: value(4),
m6: value(5),
};
}),
rowKey: 'id',
};
let style: HTMLStyleElement | undefined;
export function setup(_api: GridApi<Row>) {
style = document.createElement('style');
style.textContent = CSS;
document.head.append(style);
return () => {
style?.remove();
style = undefined;
};
}
export const actions = {
preset: (api: GridApi<Row>, value?: string) => {
const preset = value && CLASSES[value] ? value : 'mark';
api.setColumns(columns(preset));
api.setOptions({ headerGroups: groups(preset) });
},
};