긴 헤더 제목을 두 줄로 나누기Break a long header title into two lines
제목이 길면 컬럼이 넓어지는 대신 줄바꿈으로 두 줄로 나눠 보여요. 제목에 줄바꿈을 넣고 헤더 높이만 키우면 돼요.Instead of widening a column for a long title, split it into two lines. Add a line break to the title and make the header taller.
코드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="lines">
<option value="two" selected>두 줄</option>
<option value="one">한 줄</option>
</select>
</div>
<div id="grid" style="height: 280px"></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('lines').addEventListener('change', (event) => actions.lines(api, event.target.value));const products = [
'무선 이어폰',
'스마트 워치',
'노트북 거치대',
'보조배터리',
'블루투스 스피커',
'웹캠',
];
// 제목에 줄바꿈(\n)을 넣으면 헤더가 두 줄로 나뉘어요. 헤더 높이를 두 줄이 들어갈 만큼 키워 주세요.
// 같은 컬럼의 두 가지 제목을 상수로 둬요. 어느 쪽을 적용해도 결과는 항상 같아요.
const TITLES = {
two: { stock: '현재\n재고(개)', incoming: '입고\n예정(개)', price: '판매가\n(부가세 포함)' },
one: { stock: '현재 재고(개)', incoming: '입고 예정(개)', price: '판매가(부가세 포함)' },
};
export const options = {
columns: [
{ id: 'product', title: '상품명', width: 150 },
{ id: 'stock', title: TITLES.two.stock, kind: 'number', width: 100, align: 'end' },
{ id: 'incoming', title: TITLES.two.incoming, kind: 'number', width: 100, align: 'end' },
{
id: 'price',
title: TITLES.two.price,
kind: 'number',
width: 130,
align: 'end',
formatValue: (value) => `${Number(value).toLocaleString('ko-KR')}원`,
},
],
rows: products.map((product, i) => ({
id: i + 1,
product,
stock: 12 + ((i * 37) % 90),
incoming: (i * 25) % 120,
price: 19900 + i * 12000,
})),
rowKey: 'id',
headerHeight: 56,
};
export const actions = {
// 같은 컬럼 제목을 한 줄/두 줄로 바꿔요. 두 줄일 때만 헤더를 높여요.
lines: (api, value) => {
const titles = value === 'one' ? TITLES.one : TITLES.two;
api.setColumns(api
.getColumns()
.map((c) => ({ ...c, title: titles[c.id] ?? c.title })));
api.setOptions({ headerHeight: value === 'one' ? 36 : 56 });
},
};<!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="lines">
<option value="two" selected>두 줄</option>
<option value="one">한 줄</option>
</select>
</div>
<div id="grid" style="height: 280px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const products = [
'무선 이어폰',
'스마트 워치',
'노트북 거치대',
'보조배터리',
'블루투스 스피커',
'웹캠',
];
// 제목에 줄바꿈(\n)을 넣으면 헤더가 두 줄로 나뉘어요. 헤더 높이를 두 줄이 들어갈 만큼 키워 주세요.
// 같은 컬럼의 두 가지 제목을 상수로 둬요. 어느 쪽을 적용해도 결과는 항상 같아요.
const TITLES = {
two: { stock: '현재\n재고(개)', incoming: '입고\n예정(개)', price: '판매가\n(부가세 포함)' },
one: { stock: '현재 재고(개)', incoming: '입고 예정(개)', price: '판매가(부가세 포함)' },
};
const options = {
columns: [
{ id: 'product', title: '상품명', width: 150 },
{ id: 'stock', title: TITLES.two.stock, kind: 'number', width: 100, align: 'end' },
{ id: 'incoming', title: TITLES.two.incoming, kind: 'number', width: 100, align: 'end' },
{
id: 'price',
title: TITLES.two.price,
kind: 'number',
width: 130,
align: 'end',
formatValue: (value) => `${Number(value).toLocaleString('ko-KR')}원`,
},
],
rows: products.map((product, i) => ({
id: i + 1,
product,
stock: 12 + ((i * 37) % 90),
incoming: (i * 25) % 120,
price: 19900 + i * 12000,
})),
rowKey: 'id',
headerHeight: 56,
};
const actions = {
// 같은 컬럼 제목을 한 줄/두 줄로 바꿔요. 두 줄일 때만 헤더를 높여요.
lines: (api, value) => {
const titles = value === 'one' ? TITLES.one : TITLES.two;
api.setColumns(api
.getColumns()
.map((c) => ({ ...c, title: titles[c.id] ?? c.title })));
api.setOptions({ headerHeight: value === 'one' ? 36 : 56 });
},
};
options.ariaLabel = "긴 헤더 제목을 두 줄로 나누기";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
document.getElementById('lines').addEventListener('change', (event) => actions.lines(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.lines(api, event.target.value)}
>
<option value="two">두 줄</option>
<option value="one">한 줄</option>
</select>
</div>
<Grid style={{ height: 280 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
product: string;
stock: number;
incoming: number;
price: number;
}
const products = [
'무선 이어폰',
'스마트 워치',
'노트북 거치대',
'보조배터리',
'블루투스 스피커',
'웹캠',
];
// 제목에 줄바꿈(\n)을 넣으면 헤더가 두 줄로 나뉘어요. 헤더 높이를 두 줄이 들어갈 만큼 키워 주세요.
// 같은 컬럼의 두 가지 제목을 상수로 둬요. 어느 쪽을 적용해도 결과는 항상 같아요.
const TITLES = {
two: { stock: '현재\n재고(개)', incoming: '입고\n예정(개)', price: '판매가\n(부가세 포함)' },
one: { stock: '현재 재고(개)', incoming: '입고 예정(개)', price: '판매가(부가세 포함)' },
};
export const options: GridOptions<Row> = {
columns: [
{ id: 'product', title: '상품명', width: 150 },
{ id: 'stock', title: TITLES.two.stock, kind: 'number', width: 100, align: 'end' },
{ id: 'incoming', title: TITLES.two.incoming, kind: 'number', width: 100, align: 'end' },
{
id: 'price',
title: TITLES.two.price,
kind: 'number',
width: 130,
align: 'end',
formatValue: (value) => `${Number(value).toLocaleString('ko-KR')}원`,
},
],
rows: products.map((product, i) => ({
id: i + 1,
product,
stock: 12 + ((i * 37) % 90),
incoming: (i * 25) % 120,
price: 19900 + i * 12000,
})),
rowKey: 'id',
headerHeight: 56,
};
export const actions = {
// 같은 컬럼 제목을 한 줄/두 줄로 바꿔요. 두 줄일 때만 헤더를 높여요.
lines: (api: GridApi<Row>, value?: string) => {
const titles = value === 'one' ? TITLES.one : TITLES.two;
api.setColumns(
api
.getColumns()
.map((c) => ({ ...c, title: titles[c.id as keyof typeof titles] ?? c.title })),
);
api.setOptions({ headerHeight: value === 'one' ? 36 : 56 });
},
};버튼을 누르면 이 예제의 코드가 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.lines(api, ($event.target as HTMLSelectElement).value)"
>
<option value="two" selected>두 줄</option>
<option value="one">한 줄</option>
</select>
</div>
<Grid style="height: 280px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
product: string;
stock: number;
incoming: number;
price: number;
}
const products = [
'무선 이어폰',
'스마트 워치',
'노트북 거치대',
'보조배터리',
'블루투스 스피커',
'웹캠',
];
// 제목에 줄바꿈(\n)을 넣으면 헤더가 두 줄로 나뉘어요. 헤더 높이를 두 줄이 들어갈 만큼 키워 주세요.
// 같은 컬럼의 두 가지 제목을 상수로 둬요. 어느 쪽을 적용해도 결과는 항상 같아요.
const TITLES = {
two: { stock: '현재\n재고(개)', incoming: '입고\n예정(개)', price: '판매가\n(부가세 포함)' },
one: { stock: '현재 재고(개)', incoming: '입고 예정(개)', price: '판매가(부가세 포함)' },
};
export const options: GridOptions<Row> = {
columns: [
{ id: 'product', title: '상품명', width: 150 },
{ id: 'stock', title: TITLES.two.stock, kind: 'number', width: 100, align: 'end' },
{ id: 'incoming', title: TITLES.two.incoming, kind: 'number', width: 100, align: 'end' },
{
id: 'price',
title: TITLES.two.price,
kind: 'number',
width: 130,
align: 'end',
formatValue: (value) => `${Number(value).toLocaleString('ko-KR')}원`,
},
],
rows: products.map((product, i) => ({
id: i + 1,
product,
stock: 12 + ((i * 37) % 90),
incoming: (i * 25) % 120,
price: 19900 + i * 12000,
})),
rowKey: 'id',
headerHeight: 56,
};
export const actions = {
// 같은 컬럼 제목을 한 줄/두 줄로 바꿔요. 두 줄일 때만 헤더를 높여요.
lines: (api: GridApi<Row>, value?: string) => {
const titles = value === 'one' ? TITLES.one : TITLES.two;
api.setColumns(
api
.getColumns()
.map((c) => ({ ...c, title: titles[c.id as keyof typeof titles] ?? c.title })),
);
api.setOptions({ headerHeight: value === 'one' ? 36 : 56 });
},
};버튼을 누르면 이 예제의 코드가 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.lines(api, $any($event.target).value)"
>
<option value="two" selected>두 줄</option>
<option value="one">한 줄</option>
</select>
</div>
<mx-grid style="height: 280px" [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;
product: string;
stock: number;
incoming: number;
price: number;
}
const products = [
'무선 이어폰',
'스마트 워치',
'노트북 거치대',
'보조배터리',
'블루투스 스피커',
'웹캠',
];
// 제목에 줄바꿈(\n)을 넣으면 헤더가 두 줄로 나뉘어요. 헤더 높이를 두 줄이 들어갈 만큼 키워 주세요.
// 같은 컬럼의 두 가지 제목을 상수로 둬요. 어느 쪽을 적용해도 결과는 항상 같아요.
const TITLES = {
two: { stock: '현재\n재고(개)', incoming: '입고\n예정(개)', price: '판매가\n(부가세 포함)' },
one: { stock: '현재 재고(개)', incoming: '입고 예정(개)', price: '판매가(부가세 포함)' },
};
export const options: GridOptions<Row> = {
columns: [
{ id: 'product', title: '상품명', width: 150 },
{ id: 'stock', title: TITLES.two.stock, kind: 'number', width: 100, align: 'end' },
{ id: 'incoming', title: TITLES.two.incoming, kind: 'number', width: 100, align: 'end' },
{
id: 'price',
title: TITLES.two.price,
kind: 'number',
width: 130,
align: 'end',
formatValue: (value) => `${Number(value).toLocaleString('ko-KR')}원`,
},
],
rows: products.map((product, i) => ({
id: i + 1,
product,
stock: 12 + ((i * 37) % 90),
incoming: (i * 25) % 120,
price: 19900 + i * 12000,
})),
rowKey: 'id',
headerHeight: 56,
};
export const actions = {
// 같은 컬럼 제목을 한 줄/두 줄로 바꿔요. 두 줄일 때만 헤더를 높여요.
lines: (api: GridApi<Row>, value?: string) => {
const titles = value === 'one' ? TITLES.one : TITLES.two;
api.setColumns(
api
.getColumns()
.map((c) => ({ ...c, title: titles[c.id as keyof typeof titles] ?? c.title })),
);
api.setOptions({ headerHeight: value === 'one' ? 36 : 56 });
},
};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="Title lines" id="lines">
<option value="two" selected>Two lines</option>
<option value="one">One line</option>
</select>
</div>
<div id="grid" style="height: 280px"></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('lines').addEventListener('change', (event) => actions.lines(api, event.target.value));const products = [
'Wireless earbuds',
'Smartwatch',
'Laptop stand',
'Power bank',
'Bluetooth speaker',
'Webcam',
];
// A line break (\n) in the title splits the header into two lines. Make the header tall enough for both lines.
// The two titles of the same columns are kept as constants. Whichever one you apply, the result is always the same.
const TITLES = {
two: { stock: 'In stock\n(units)', incoming: 'Incoming\n(units)', price: 'Price\n(incl. tax)' },
one: { stock: 'In stock (units)', incoming: 'Incoming (units)', price: 'Price (incl. tax)' },
};
export const options = {
columns: [
{ id: 'product', title: 'Product', width: 170 },
{ id: 'stock', title: TITLES.two.stock, kind: 'number', width: 100, align: 'end' },
{ id: 'incoming', title: TITLES.two.incoming, kind: 'number', width: 100, align: 'end' },
{
id: 'price',
title: TITLES.two.price,
kind: 'number',
width: 130,
align: 'end',
formatValue: (value) => `$${Number(value).toLocaleString('en-US')}`,
},
],
rows: products.map((product, i) => ({
id: i + 1,
product,
stock: 12 + ((i * 37) % 90),
incoming: (i * 25) % 120,
price: 20 + i * 12,
})),
rowKey: 'id',
headerHeight: 56,
};
export const actions = {
// Switch the same column titles between one and two lines. The header is made taller only for two lines.
lines: (api, value) => {
const titles = value === 'one' ? TITLES.one : TITLES.two;
api.setColumns(api
.getColumns()
.map((c) => ({ ...c, title: titles[c.id] ?? c.title })));
api.setOptions({ headerHeight: value === 'one' ? 36 : 56 });
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Break a long header title into two lines</title>
</head>
<body>
<div class="toolbar">
<select aria-label="Title lines" id="lines">
<option value="two" selected>Two lines</option>
<option value="one">One line</option>
</select>
</div>
<div id="grid" style="height: 280px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const products = [
'Wireless earbuds',
'Smartwatch',
'Laptop stand',
'Power bank',
'Bluetooth speaker',
'Webcam',
];
// A line break (\n) in the title splits the header into two lines. Make the header tall enough for both lines.
// The two titles of the same columns are kept as constants. Whichever one you apply, the result is always the same.
const TITLES = {
two: { stock: 'In stock\n(units)', incoming: 'Incoming\n(units)', price: 'Price\n(incl. tax)' },
one: { stock: 'In stock (units)', incoming: 'Incoming (units)', price: 'Price (incl. tax)' },
};
const options = {
columns: [
{ id: 'product', title: 'Product', width: 170 },
{ id: 'stock', title: TITLES.two.stock, kind: 'number', width: 100, align: 'end' },
{ id: 'incoming', title: TITLES.two.incoming, kind: 'number', width: 100, align: 'end' },
{
id: 'price',
title: TITLES.two.price,
kind: 'number',
width: 130,
align: 'end',
formatValue: (value) => `$${Number(value).toLocaleString('en-US')}`,
},
],
rows: products.map((product, i) => ({
id: i + 1,
product,
stock: 12 + ((i * 37) % 90),
incoming: (i * 25) % 120,
price: 20 + i * 12,
})),
rowKey: 'id',
headerHeight: 56,
};
const actions = {
// Switch the same column titles between one and two lines. The header is made taller only for two lines.
lines: (api, value) => {
const titles = value === 'one' ? TITLES.one : TITLES.two;
api.setColumns(api
.getColumns()
.map((c) => ({ ...c, title: titles[c.id] ?? c.title })));
api.setOptions({ headerHeight: value === 'one' ? 36 : 56 });
},
};
options.ariaLabel = "Break a long header title into two lines";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
document.getElementById('lines').addEventListener('change', (event) => actions.lines(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="Title lines"
disabled={!api}
onChange={(event) => api && actions.lines(api, event.target.value)}
>
<option value="two">Two lines</option>
<option value="one">One line</option>
</select>
</div>
<Grid style={{ height: 280 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
product: string;
stock: number;
incoming: number;
price: number;
}
const products = [
'Wireless earbuds',
'Smartwatch',
'Laptop stand',
'Power bank',
'Bluetooth speaker',
'Webcam',
];
// A line break (\n) in the title splits the header into two lines. Make the header tall enough for both lines.
// The two titles of the same columns are kept as constants. Whichever one you apply, the result is always the same.
const TITLES = {
two: { stock: 'In stock\n(units)', incoming: 'Incoming\n(units)', price: 'Price\n(incl. tax)' },
one: { stock: 'In stock (units)', incoming: 'Incoming (units)', price: 'Price (incl. tax)' },
};
export const options: GridOptions<Row> = {
columns: [
{ id: 'product', title: 'Product', width: 170 },
{ id: 'stock', title: TITLES.two.stock, kind: 'number', width: 100, align: 'end' },
{ id: 'incoming', title: TITLES.two.incoming, kind: 'number', width: 100, align: 'end' },
{
id: 'price',
title: TITLES.two.price,
kind: 'number',
width: 130,
align: 'end',
formatValue: (value) => `$${Number(value).toLocaleString('en-US')}`,
},
],
rows: products.map((product, i) => ({
id: i + 1,
product,
stock: 12 + ((i * 37) % 90),
incoming: (i * 25) % 120,
price: 20 + i * 12,
})),
rowKey: 'id',
headerHeight: 56,
};
export const actions = {
// Switch the same column titles between one and two lines. The header is made taller only for two lines.
lines: (api: GridApi<Row>, value?: string) => {
const titles = value === 'one' ? TITLES.one : TITLES.two;
api.setColumns(
api
.getColumns()
.map((c) => ({ ...c, title: titles[c.id as keyof typeof titles] ?? c.title })),
);
api.setOptions({ headerHeight: value === 'one' ? 36 : 56 });
},
};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="Title lines"
:disabled="!api"
@change="api && actions.lines(api, ($event.target as HTMLSelectElement).value)"
>
<option value="two" selected>Two lines</option>
<option value="one">One line</option>
</select>
</div>
<Grid style="height: 280px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
product: string;
stock: number;
incoming: number;
price: number;
}
const products = [
'Wireless earbuds',
'Smartwatch',
'Laptop stand',
'Power bank',
'Bluetooth speaker',
'Webcam',
];
// A line break (\n) in the title splits the header into two lines. Make the header tall enough for both lines.
// The two titles of the same columns are kept as constants. Whichever one you apply, the result is always the same.
const TITLES = {
two: { stock: 'In stock\n(units)', incoming: 'Incoming\n(units)', price: 'Price\n(incl. tax)' },
one: { stock: 'In stock (units)', incoming: 'Incoming (units)', price: 'Price (incl. tax)' },
};
export const options: GridOptions<Row> = {
columns: [
{ id: 'product', title: 'Product', width: 170 },
{ id: 'stock', title: TITLES.two.stock, kind: 'number', width: 100, align: 'end' },
{ id: 'incoming', title: TITLES.two.incoming, kind: 'number', width: 100, align: 'end' },
{
id: 'price',
title: TITLES.two.price,
kind: 'number',
width: 130,
align: 'end',
formatValue: (value) => `$${Number(value).toLocaleString('en-US')}`,
},
],
rows: products.map((product, i) => ({
id: i + 1,
product,
stock: 12 + ((i * 37) % 90),
incoming: (i * 25) % 120,
price: 20 + i * 12,
})),
rowKey: 'id',
headerHeight: 56,
};
export const actions = {
// Switch the same column titles between one and two lines. The header is made taller only for two lines.
lines: (api: GridApi<Row>, value?: string) => {
const titles = value === 'one' ? TITLES.one : TITLES.two;
api.setColumns(
api
.getColumns()
.map((c) => ({ ...c, title: titles[c.id as keyof typeof titles] ?? c.title })),
);
api.setOptions({ headerHeight: value === 'one' ? 36 : 56 });
},
};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="Title lines"
[disabled]="!api"
(change)="api && actions.lines(api, $any($event.target).value)"
>
<option value="two" selected>Two lines</option>
<option value="one">One line</option>
</select>
</div>
<mx-grid style="height: 280px" [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;
product: string;
stock: number;
incoming: number;
price: number;
}
const products = [
'Wireless earbuds',
'Smartwatch',
'Laptop stand',
'Power bank',
'Bluetooth speaker',
'Webcam',
];
// A line break (\n) in the title splits the header into two lines. Make the header tall enough for both lines.
// The two titles of the same columns are kept as constants. Whichever one you apply, the result is always the same.
const TITLES = {
two: { stock: 'In stock\n(units)', incoming: 'Incoming\n(units)', price: 'Price\n(incl. tax)' },
one: { stock: 'In stock (units)', incoming: 'Incoming (units)', price: 'Price (incl. tax)' },
};
export const options: GridOptions<Row> = {
columns: [
{ id: 'product', title: 'Product', width: 170 },
{ id: 'stock', title: TITLES.two.stock, kind: 'number', width: 100, align: 'end' },
{ id: 'incoming', title: TITLES.two.incoming, kind: 'number', width: 100, align: 'end' },
{
id: 'price',
title: TITLES.two.price,
kind: 'number',
width: 130,
align: 'end',
formatValue: (value) => `$${Number(value).toLocaleString('en-US')}`,
},
],
rows: products.map((product, i) => ({
id: i + 1,
product,
stock: 12 + ((i * 37) % 90),
incoming: (i * 25) % 120,
price: 20 + i * 12,
})),
rowKey: 'id',
headerHeight: 56,
};
export const actions = {
// Switch the same column titles between one and two lines. The header is made taller only for two lines.
lines: (api: GridApi<Row>, value?: string) => {
const titles = value === 'one' ? TITLES.one : TITLES.two;
api.setColumns(
api
.getColumns()
.map((c) => ({ ...c, title: titles[c.id as keyof typeof titles] ?? c.title })),
);
api.setOptions({ headerHeight: value === 'one' ? 36 : 56 });
},
};