헤더 글자를 실행 중에 바꾸기Change header text at runtime
헤더 글자는 컬럼 정의의 제목이라서, 그 컬럼의 제목만 바꿔 다시 넣으면 돼요. 폭·순서·고정은 그대로 남아요. 월이 바뀔 때 '3월 지급액'처럼 제목을 갈아 끼울 때 쓸 수 있어요.A header's text is the column's title, so put the list in again with only that title changed; width, order and pin stay. Handy when a title such as March payout ($) must change over time.
코드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">
<input
type="text"
aria-label="금액 제목"
placeholder="금액 제목 (예: 8월 지급액)"
id="title"
/>
<button type="button" id="month">'3월 지급액'으로</button>
<button type="button" id="reset">처음 제목으로</button>
</div>
<div id="grid" style="height: 170px"></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('title').addEventListener('input', (event) => actions.title(api, event.target.value));
document.getElementById('month').addEventListener('click', () => actions.month(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));const DEFAULT_TITLE = '금액(원)';
let title = DEFAULT_TITLE;
const columns = () => [
{ id: 'id', title: '번호', width: 80, align: 'end' },
{ id: 'name', title: '이름', width: 130 },
{ id: 'team', title: '팀', width: 130 },
{
id: 'amount',
title,
kind: 'number',
width: 150,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
},
];
export const options = {
columns: columns(),
rows: [
{ id: 1, name: '김민준', team: '개발팀', amount: 4200000 },
{ id: 2, name: '이서연', team: '디자인팀', amount: 1850000 },
{ id: 3, name: '박지호', team: '영업팀', amount: 960000 },
],
rowKey: 'id',
};
export function setup(_api) {
title = DEFAULT_TITLE;
return () => { };
}
// 헤더 제목은 컬럼 정의의 title이에요. 그 컬럼만 바꾼 정의로 setColumns를 부르면 폭·순서·고정은 그대로 남아요.
// 입력칸을 비우면 처음 제목으로 돌아가요.
export const actions = {
title: (api, value) => {
title = (value ?? '').trim() || DEFAULT_TITLE;
api.setColumns(columns());
},
month: (api) => {
title = '3월 지급액(원)';
api.setColumns(columns());
},
reset: (api) => {
title = DEFAULT_TITLE;
api.setColumns(columns());
},
};<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>헤더 글자를 실행 중에 바꾸기</title>
</head>
<body>
<div class="toolbar">
<input
type="text"
aria-label="금액 제목"
placeholder="금액 제목 (예: 8월 지급액)"
id="title"
/>
<button type="button" id="month">'3월 지급액'으로</button>
<button type="button" id="reset">처음 제목으로</button>
</div>
<div id="grid" style="height: 170px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const DEFAULT_TITLE = '금액(원)';
let title = DEFAULT_TITLE;
const columns = () => [
{ id: 'id', title: '번호', width: 80, align: 'end' },
{ id: 'name', title: '이름', width: 130 },
{ id: 'team', title: '팀', width: 130 },
{
id: 'amount',
title,
kind: 'number',
width: 150,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
},
];
const options = {
columns: columns(),
rows: [
{ id: 1, name: '김민준', team: '개발팀', amount: 4200000 },
{ id: 2, name: '이서연', team: '디자인팀', amount: 1850000 },
{ id: 3, name: '박지호', team: '영업팀', amount: 960000 },
],
rowKey: 'id',
};
function setup(_api) {
title = DEFAULT_TITLE;
return () => { };
}
// 헤더 제목은 컬럼 정의의 title이에요. 그 컬럼만 바꾼 정의로 setColumns를 부르면 폭·순서·고정은 그대로 남아요.
// 입력칸을 비우면 처음 제목으로 돌아가요.
const actions = {
title: (api, value) => {
title = (value ?? '').trim() || DEFAULT_TITLE;
api.setColumns(columns());
},
month: (api) => {
title = '3월 지급액(원)';
api.setColumns(columns());
},
reset: (api) => {
title = DEFAULT_TITLE;
api.setColumns(columns());
},
};
options.ariaLabel = "헤더 글자를 실행 중에 바꾸기";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api);
document.getElementById('title').addEventListener('input', (event) => actions.title(api, event.target.value));
document.getElementById('month').addEventListener('click', () => actions.month(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));
</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">
<input
type="text"
aria-label="금액 제목"
placeholder="금액 제목 (예: 8월 지급액)"
disabled={!api}
onChange={(event) => api && actions.title(api, event.target.value)}
/>
<button
type="button"
disabled={!api}
onClick={() => api && actions.month(api)}
>'3월 지급액'으로</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.reset(api)}
>처음 제목으로</button>
</div>
<Grid style={{ height: 170 }} ref={gridRef} options={options} />
</>
);
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
name: string;
team: string;
amount: number;
}
const DEFAULT_TITLE = '금액(원)';
let title = DEFAULT_TITLE;
const columns = (): ColumnDef<Row>[] => [
{ id: 'id', title: '번호', width: 80, align: 'end' },
{ id: 'name', title: '이름', width: 130 },
{ id: 'team', title: '팀', width: 130 },
{
id: 'amount',
title,
kind: 'number',
width: 150,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
},
];
export const options: GridOptions<Row> = {
columns: columns(),
rows: [
{ id: 1, name: '김민준', team: '개발팀', amount: 4200000 },
{ id: 2, name: '이서연', team: '디자인팀', amount: 1850000 },
{ id: 3, name: '박지호', team: '영업팀', amount: 960000 },
],
rowKey: 'id',
};
export function setup(_api: GridApi<Row>) {
title = DEFAULT_TITLE;
return () => {};
}
// 헤더 제목은 컬럼 정의의 title이에요. 그 컬럼만 바꾼 정의로 setColumns를 부르면 폭·순서·고정은 그대로 남아요.
// 입력칸을 비우면 처음 제목으로 돌아가요.
export const actions = {
title: (api: GridApi<Row>, value?: string) => {
title = (value ?? '').trim() || DEFAULT_TITLE;
api.setColumns(columns());
},
month: (api: GridApi<Row>) => {
title = '3월 지급액(원)';
api.setColumns(columns());
},
reset: (api: GridApi<Row>) => {
title = DEFAULT_TITLE;
api.setColumns(columns());
},
};버튼을 누르면 이 예제의 코드가 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">
<input
type="text"
aria-label="금액 제목"
placeholder="금액 제목 (예: 8월 지급액)"
:disabled="!api"
@input="api && actions.title(api, ($event.target as HTMLInputElement).value)"
/>
<button type="button" :disabled="!api" @click="api && actions.month(api)">'3월 지급액'으로</button>
<button type="button" :disabled="!api" @click="api && actions.reset(api)">처음 제목으로</button>
</div>
<Grid style="height: 170px" :options="options" @ready="onReady" />
</template>import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
name: string;
team: string;
amount: number;
}
const DEFAULT_TITLE = '금액(원)';
let title = DEFAULT_TITLE;
const columns = (): ColumnDef<Row>[] => [
{ id: 'id', title: '번호', width: 80, align: 'end' },
{ id: 'name', title: '이름', width: 130 },
{ id: 'team', title: '팀', width: 130 },
{
id: 'amount',
title,
kind: 'number',
width: 150,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
},
];
export const options: GridOptions<Row> = {
columns: columns(),
rows: [
{ id: 1, name: '김민준', team: '개발팀', amount: 4200000 },
{ id: 2, name: '이서연', team: '디자인팀', amount: 1850000 },
{ id: 3, name: '박지호', team: '영업팀', amount: 960000 },
],
rowKey: 'id',
};
export function setup(_api: GridApi<Row>) {
title = DEFAULT_TITLE;
return () => {};
}
// 헤더 제목은 컬럼 정의의 title이에요. 그 컬럼만 바꾼 정의로 setColumns를 부르면 폭·순서·고정은 그대로 남아요.
// 입력칸을 비우면 처음 제목으로 돌아가요.
export const actions = {
title: (api: GridApi<Row>, value?: string) => {
title = (value ?? '').trim() || DEFAULT_TITLE;
api.setColumns(columns());
},
month: (api: GridApi<Row>) => {
title = '3월 지급액(원)';
api.setColumns(columns());
},
reset: (api: GridApi<Row>) => {
title = DEFAULT_TITLE;
api.setColumns(columns());
},
};버튼을 누르면 이 예제의 코드가 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">
<input
type="text"
aria-label="금액 제목"
placeholder="금액 제목 (예: 8월 지급액)"
[disabled]="!api"
(input)="api && actions.title(api, $any($event.target).value)"
/>
<button
type="button"
[disabled]="!api"
(click)="api && actions.month(api)"
>'3월 지급액'으로</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.reset(api)"
>처음 제목으로</button>
</div>
<mx-grid style="height: 170px" [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 } from '@maxgrid/angular';
export interface Row {
id: number;
name: string;
team: string;
amount: number;
}
const DEFAULT_TITLE = '금액(원)';
let title = DEFAULT_TITLE;
const columns = (): ColumnDef<Row>[] => [
{ id: 'id', title: '번호', width: 80, align: 'end' },
{ id: 'name', title: '이름', width: 130 },
{ id: 'team', title: '팀', width: 130 },
{
id: 'amount',
title,
kind: 'number',
width: 150,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
},
];
export const options: GridOptions<Row> = {
columns: columns(),
rows: [
{ id: 1, name: '김민준', team: '개발팀', amount: 4200000 },
{ id: 2, name: '이서연', team: '디자인팀', amount: 1850000 },
{ id: 3, name: '박지호', team: '영업팀', amount: 960000 },
],
rowKey: 'id',
};
export function setup(_api: GridApi<Row>) {
title = DEFAULT_TITLE;
return () => {};
}
// 헤더 제목은 컬럼 정의의 title이에요. 그 컬럼만 바꾼 정의로 setColumns를 부르면 폭·순서·고정은 그대로 남아요.
// 입력칸을 비우면 처음 제목으로 돌아가요.
export const actions = {
title: (api: GridApi<Row>, value?: string) => {
title = (value ?? '').trim() || DEFAULT_TITLE;
api.setColumns(columns());
},
month: (api: GridApi<Row>) => {
title = '3월 지급액(원)';
api.setColumns(columns());
},
reset: (api: GridApi<Row>) => {
title = DEFAULT_TITLE;
api.setColumns(columns());
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<div id="app">
<div class="toolbar">
<input
type="text"
aria-label="Amount title"
placeholder="Amount title (e.g. August payout)"
id="title"
/>
<button type="button" id="month">Set "March payout ($)"</button>
<button type="button" id="reset">Reset title</button>
</div>
<div id="grid" style="height: 170px"></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('title').addEventListener('input', (event) => actions.title(api, event.target.value));
document.getElementById('month').addEventListener('click', () => actions.month(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));const DEFAULT_TITLE = 'Amount ($)';
let title = DEFAULT_TITLE;
const columns = () => [
{ id: 'id', title: 'No.', width: 80, align: 'end' },
{ id: 'name', title: 'Name', width: 130 },
{ id: 'team', title: 'Team', width: 130 },
{
id: 'amount',
title,
kind: 'number',
width: 170,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('en-US'),
},
];
export const options = {
columns: columns(),
rows: [
{ id: 1, name: 'Liam Carter', team: 'Dev', amount: 4200 },
{ id: 2, name: 'Emma Stone', team: 'Design', amount: 1850 },
{ id: 3, name: 'Noah Parker', team: 'Sales', amount: 960 },
],
rowKey: 'id',
};
export function setup(_api) {
title = DEFAULT_TITLE;
return () => { };
}
// The header title is the title in the column definition. Calling setColumns with a definition that changes only that column keeps the width, order and pinning.
// Emptying the input goes back to the initial title.
export const actions = {
title: (api, value) => {
title = (value ?? '').trim() || DEFAULT_TITLE;
api.setColumns(columns());
},
month: (api) => {
title = 'March payout ($)';
api.setColumns(columns());
},
reset: (api) => {
title = DEFAULT_TITLE;
api.setColumns(columns());
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Change header text at runtime</title>
</head>
<body>
<div class="toolbar">
<input
type="text"
aria-label="Amount title"
placeholder="Amount title (e.g. August payout)"
id="title"
/>
<button type="button" id="month">Set "March payout ($)"</button>
<button type="button" id="reset">Reset title</button>
</div>
<div id="grid" style="height: 170px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const DEFAULT_TITLE = 'Amount ($)';
let title = DEFAULT_TITLE;
const columns = () => [
{ id: 'id', title: 'No.', width: 80, align: 'end' },
{ id: 'name', title: 'Name', width: 130 },
{ id: 'team', title: 'Team', width: 130 },
{
id: 'amount',
title,
kind: 'number',
width: 170,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('en-US'),
},
];
const options = {
columns: columns(),
rows: [
{ id: 1, name: 'Liam Carter', team: 'Dev', amount: 4200 },
{ id: 2, name: 'Emma Stone', team: 'Design', amount: 1850 },
{ id: 3, name: 'Noah Parker', team: 'Sales', amount: 960 },
],
rowKey: 'id',
};
function setup(_api) {
title = DEFAULT_TITLE;
return () => { };
}
// The header title is the title in the column definition. Calling setColumns with a definition that changes only that column keeps the width, order and pinning.
// Emptying the input goes back to the initial title.
const actions = {
title: (api, value) => {
title = (value ?? '').trim() || DEFAULT_TITLE;
api.setColumns(columns());
},
month: (api) => {
title = 'March payout ($)';
api.setColumns(columns());
},
reset: (api) => {
title = DEFAULT_TITLE;
api.setColumns(columns());
},
};
options.ariaLabel = "Change header text at runtime";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api);
document.getElementById('title').addEventListener('input', (event) => actions.title(api, event.target.value));
document.getElementById('month').addEventListener('click', () => actions.month(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));
</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">
<input
type="text"
aria-label="Amount title"
placeholder="Amount title (e.g. August payout)"
disabled={!api}
onChange={(event) => api && actions.title(api, event.target.value)}
/>
<button
type="button"
disabled={!api}
onClick={() => api && actions.month(api)}
>Set "March payout ($)"</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.reset(api)}
>Reset title</button>
</div>
<Grid style={{ height: 170 }} ref={gridRef} options={options} />
</>
);
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
name: string;
team: string;
amount: number;
}
const DEFAULT_TITLE = 'Amount ($)';
let title = DEFAULT_TITLE;
const columns = (): ColumnDef<Row>[] => [
{ id: 'id', title: 'No.', width: 80, align: 'end' },
{ id: 'name', title: 'Name', width: 130 },
{ id: 'team', title: 'Team', width: 130 },
{
id: 'amount',
title,
kind: 'number',
width: 170,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('en-US'),
},
];
export const options: GridOptions<Row> = {
columns: columns(),
rows: [
{ id: 1, name: 'Liam Carter', team: 'Dev', amount: 4200 },
{ id: 2, name: 'Emma Stone', team: 'Design', amount: 1850 },
{ id: 3, name: 'Noah Parker', team: 'Sales', amount: 960 },
],
rowKey: 'id',
};
export function setup(_api: GridApi<Row>) {
title = DEFAULT_TITLE;
return () => {};
}
// The header title is the title in the column definition. Calling setColumns with a definition that changes only that column keeps the width, order and pinning.
// Emptying the input goes back to the initial title.
export const actions = {
title: (api: GridApi<Row>, value?: string) => {
title = (value ?? '').trim() || DEFAULT_TITLE;
api.setColumns(columns());
},
month: (api: GridApi<Row>) => {
title = 'March payout ($)';
api.setColumns(columns());
},
reset: (api: GridApi<Row>) => {
title = DEFAULT_TITLE;
api.setColumns(columns());
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<script setup lang="ts">
import { 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">
<input
type="text"
aria-label="Amount title"
placeholder="Amount title (e.g. August payout)"
:disabled="!api"
@input="api && actions.title(api, ($event.target as HTMLInputElement).value)"
/>
<button type="button" :disabled="!api" @click="api && actions.month(api)">Set "March payout ($)"</button>
<button type="button" :disabled="!api" @click="api && actions.reset(api)">Reset title</button>
</div>
<Grid style="height: 170px" :options="options" @ready="onReady" />
</template>import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
name: string;
team: string;
amount: number;
}
const DEFAULT_TITLE = 'Amount ($)';
let title = DEFAULT_TITLE;
const columns = (): ColumnDef<Row>[] => [
{ id: 'id', title: 'No.', width: 80, align: 'end' },
{ id: 'name', title: 'Name', width: 130 },
{ id: 'team', title: 'Team', width: 130 },
{
id: 'amount',
title,
kind: 'number',
width: 170,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('en-US'),
},
];
export const options: GridOptions<Row> = {
columns: columns(),
rows: [
{ id: 1, name: 'Liam Carter', team: 'Dev', amount: 4200 },
{ id: 2, name: 'Emma Stone', team: 'Design', amount: 1850 },
{ id: 3, name: 'Noah Parker', team: 'Sales', amount: 960 },
],
rowKey: 'id',
};
export function setup(_api: GridApi<Row>) {
title = DEFAULT_TITLE;
return () => {};
}
// The header title is the title in the column definition. Calling setColumns with a definition that changes only that column keeps the width, order and pinning.
// Emptying the input goes back to the initial title.
export const actions = {
title: (api: GridApi<Row>, value?: string) => {
title = (value ?? '').trim() || DEFAULT_TITLE;
api.setColumns(columns());
},
month: (api: GridApi<Row>) => {
title = 'March payout ($)';
api.setColumns(columns());
},
reset: (api: GridApi<Row>) => {
title = DEFAULT_TITLE;
api.setColumns(columns());
},
};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">
<input
type="text"
aria-label="Amount title"
placeholder="Amount title (e.g. August payout)"
[disabled]="!api"
(input)="api && actions.title(api, $any($event.target).value)"
/>
<button
type="button"
[disabled]="!api"
(click)="api && actions.month(api)"
>Set "March payout ($)"</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.reset(api)"
>Reset title</button>
</div>
<mx-grid style="height: 170px" [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 } from '@maxgrid/angular';
export interface Row {
id: number;
name: string;
team: string;
amount: number;
}
const DEFAULT_TITLE = 'Amount ($)';
let title = DEFAULT_TITLE;
const columns = (): ColumnDef<Row>[] => [
{ id: 'id', title: 'No.', width: 80, align: 'end' },
{ id: 'name', title: 'Name', width: 130 },
{ id: 'team', title: 'Team', width: 130 },
{
id: 'amount',
title,
kind: 'number',
width: 170,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('en-US'),
},
];
export const options: GridOptions<Row> = {
columns: columns(),
rows: [
{ id: 1, name: 'Liam Carter', team: 'Dev', amount: 4200 },
{ id: 2, name: 'Emma Stone', team: 'Design', amount: 1850 },
{ id: 3, name: 'Noah Parker', team: 'Sales', amount: 960 },
],
rowKey: 'id',
};
export function setup(_api: GridApi<Row>) {
title = DEFAULT_TITLE;
return () => {};
}
// The header title is the title in the column definition. Calling setColumns with a definition that changes only that column keeps the width, order and pinning.
// Emptying the input goes back to the initial title.
export const actions = {
title: (api: GridApi<Row>, value?: string) => {
title = (value ?? '').trim() || DEFAULT_TITLE;
api.setColumns(columns());
},
month: (api: GridApi<Row>) => {
title = 'March payout ($)';
api.setColumns(columns());
},
reset: (api: GridApi<Row>) => {
title = DEFAULT_TITLE;
api.setColumns(columns());
},
};