컬럼 설정 일부만 바꾸기Patch part of a column
updateColumn은 컬럼 정의의 일부만 얕게 합쳐 바꿔요. id와 key는 바꿀 수 없고, 사용자가 끈 폭·순서·고정은 그대로 남아요. columnsChanged의 updated에 바뀐 id가 실려요.updateColumn shallow-merges a patch into one column definition. id and key cannot change, and the width, order and pin the user chose stay. columnsChanged carries the changed id in updated.
코드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">
<button type="button" id="title">이름 제목 바꾸기</button>
<button type="button" id="wide">연봉 폭 넓히기</button>
<button type="button" id="align">팀 가운데 정렬</button>
<button type="button" id="bad">금지된 변경</button>
</div>
<div id="grid" style="height: 220px"></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, console.log);
document.getElementById('title').addEventListener('click', () => actions.title(api));
document.getElementById('wide').addEventListener('click', () => actions.wide(api));
document.getElementById('align').addEventListener('click', () => actions.align(api));
document.getElementById('bad').addEventListener('click', () => actions.bad(api));const COLUMNS = [
{ id: 'id', title: '사번', width: 80, align: 'end' },
{ id: 'name', title: '이름', width: 110 },
{ id: 'team', title: '팀', width: 110 },
{ id: 'city', title: '근무지', width: 100 },
{ id: 'salary', title: '연봉(만원)', kind: 'number', width: 120, align: 'end' },
];
const ROWS = [
{ id: 1001, name: '김민준', team: '개발팀', city: '서울', salary: 6200 },
{ id: 1002, name: '이서연', team: '디자인팀', city: '부산', salary: 4800 },
{ id: 1003, name: '박지호', team: '영업팀', city: '대구', salary: 7100 },
{ id: 1004, name: '최유나', team: '인사팀', city: '서울', salary: 3900 },
];
let say = () => { };
export const options = { columns: COLUMNS, rows: ROWS, rowKey: 'id' };
export function setup(api, log) {
say = log;
const off = api.on('columnsChanged', (e) => say({
ko: `columnsChanged: ${e.reason ?? 'set'} ${(e.updated ?? []).join(',')}`,
en: `columnsChanged: ${e.reason ?? 'set'} ${(e.updated ?? []).join(',')}`,
}));
return () => off();
}
let wide = false;
export const actions = {
title: (api) => api.updateColumn('name', { title: `이름 ${Math.floor(Math.random() * 90 + 10)}` }),
wide: (api) => {
wide = !wide;
api.updateColumn('salary', { width: wide ? 200 : 120 });
},
align: (api) => api.updateColumn('team', { align: 'center' }),
bad: (api) => {
try {
api.updateColumn('name', { id: 'other' });
}
catch (error) {
say({
ko: `막혔어요: ${error.message}`,
en: `Refused: ${error.message}`,
});
}
},
};<!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">
<button type="button" id="title">이름 제목 바꾸기</button>
<button type="button" id="wide">연봉 폭 넓히기</button>
<button type="button" id="align">팀 가운데 정렬</button>
<button type="button" id="bad">금지된 변경</button>
</div>
<div id="grid" style="height: 220px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const COLUMNS = [
{ id: 'id', title: '사번', width: 80, align: 'end' },
{ id: 'name', title: '이름', width: 110 },
{ id: 'team', title: '팀', width: 110 },
{ id: 'city', title: '근무지', width: 100 },
{ id: 'salary', title: '연봉(만원)', kind: 'number', width: 120, align: 'end' },
];
const ROWS = [
{ id: 1001, name: '김민준', team: '개발팀', city: '서울', salary: 6200 },
{ id: 1002, name: '이서연', team: '디자인팀', city: '부산', salary: 4800 },
{ id: 1003, name: '박지호', team: '영업팀', city: '대구', salary: 7100 },
{ id: 1004, name: '최유나', team: '인사팀', city: '서울', salary: 3900 },
];
let say = () => { };
const options = { columns: COLUMNS, rows: ROWS, rowKey: 'id' };
function setup(api, log) {
say = log;
const off = api.on('columnsChanged', (e) => say({
ko: `columnsChanged: ${e.reason ?? 'set'} ${(e.updated ?? []).join(',')}`,
en: `columnsChanged: ${e.reason ?? 'set'} ${(e.updated ?? []).join(',')}`,
}));
return () => off();
}
let wide = false;
const actions = {
title: (api) => api.updateColumn('name', { title: `이름 ${Math.floor(Math.random() * 90 + 10)}` }),
wide: (api) => {
wide = !wide;
api.updateColumn('salary', { width: wide ? 200 : 120 });
},
align: (api) => api.updateColumn('team', { align: 'center' }),
bad: (api) => {
try {
api.updateColumn('name', { id: 'other' });
}
catch (error) {
say({
ko: `막혔어요: ${error.message}`,
en: `Refused: ${error.message}`,
});
}
},
};
options.ariaLabel = "컬럼 설정 일부만 바꾸기";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
document.getElementById('title').addEventListener('click', () => actions.title(api));
document.getElementById('wide').addEventListener('click', () => actions.wide(api));
document.getElementById('align').addEventListener('click', () => actions.align(api));
document.getElementById('bad').addEventListener('click', () => actions.bad(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, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<button
type="button"
disabled={!api}
onClick={() => api && actions.title(api)}
>이름 제목 바꾸기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.wide(api)}
>연봉 폭 넓히기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.align(api)}
>팀 가운데 정렬</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.bad(api)}
>금지된 변경</button>
</div>
<Grid style={{ height: 220 }} ref={gridRef} options={options} />
</>
);
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
team: string;
city: string;
salary: number;
}
const COLUMNS: ColumnDef<Row>[] = [
{ id: 'id', title: '사번', width: 80, align: 'end' },
{ id: 'name', title: '이름', width: 110 },
{ id: 'team', title: '팀', width: 110 },
{ id: 'city', title: '근무지', width: 100 },
{ id: 'salary', title: '연봉(만원)', kind: 'number', width: 120, align: 'end' },
];
const ROWS: Row[] = [
{ id: 1001, name: '김민준', team: '개발팀', city: '서울', salary: 6200 },
{ id: 1002, name: '이서연', team: '디자인팀', city: '부산', salary: 4800 },
{ id: 1003, name: '박지호', team: '영업팀', city: '대구', salary: 7100 },
{ id: 1004, name: '최유나', team: '인사팀', city: '서울', salary: 3900 },
];
let say: (message: LogText) => void = () => {};
export const options: GridOptions<Row> = { columns: COLUMNS, rows: ROWS, rowKey: 'id' };
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
const off = api.on('columnsChanged', (e) =>
say({
ko: `columnsChanged: ${e.reason ?? 'set'} ${(e.updated ?? []).join(',')}`,
en: `columnsChanged: ${e.reason ?? 'set'} ${(e.updated ?? []).join(',')}`,
}),
);
return () => off();
}
let wide = false;
export const actions = {
title: (api: GridApi<Row>) =>
api.updateColumn('name', { title: `이름 ${Math.floor(Math.random() * 90 + 10)}` }),
wide: (api: GridApi<Row>) => {
wide = !wide;
api.updateColumn('salary', { width: wide ? 200 : 120 });
},
align: (api: GridApi<Row>) => api.updateColumn('team', { align: 'center' }),
bad: (api: GridApi<Row>) => {
try {
api.updateColumn('name', { id: 'other' });
} catch (error) {
say({
ko: `막혔어요: ${(error as Error).message}`,
en: `Refused: ${(error as Error).message}`,
});
}
},
};버튼을 누르면 이 예제의 코드가 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, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<button type="button" :disabled="!api" @click="api && actions.title(api)">이름 제목 바꾸기</button>
<button type="button" :disabled="!api" @click="api && actions.wide(api)">연봉 폭 넓히기</button>
<button type="button" :disabled="!api" @click="api && actions.align(api)">팀 가운데 정렬</button>
<button type="button" :disabled="!api" @click="api && actions.bad(api)">금지된 변경</button>
</div>
<Grid style="height: 220px" :options="options" @ready="onReady" />
</template>import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
team: string;
city: string;
salary: number;
}
const COLUMNS: ColumnDef<Row>[] = [
{ id: 'id', title: '사번', width: 80, align: 'end' },
{ id: 'name', title: '이름', width: 110 },
{ id: 'team', title: '팀', width: 110 },
{ id: 'city', title: '근무지', width: 100 },
{ id: 'salary', title: '연봉(만원)', kind: 'number', width: 120, align: 'end' },
];
const ROWS: Row[] = [
{ id: 1001, name: '김민준', team: '개발팀', city: '서울', salary: 6200 },
{ id: 1002, name: '이서연', team: '디자인팀', city: '부산', salary: 4800 },
{ id: 1003, name: '박지호', team: '영업팀', city: '대구', salary: 7100 },
{ id: 1004, name: '최유나', team: '인사팀', city: '서울', salary: 3900 },
];
let say: (message: LogText) => void = () => {};
export const options: GridOptions<Row> = { columns: COLUMNS, rows: ROWS, rowKey: 'id' };
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
const off = api.on('columnsChanged', (e) =>
say({
ko: `columnsChanged: ${e.reason ?? 'set'} ${(e.updated ?? []).join(',')}`,
en: `columnsChanged: ${e.reason ?? 'set'} ${(e.updated ?? []).join(',')}`,
}),
);
return () => off();
}
let wide = false;
export const actions = {
title: (api: GridApi<Row>) =>
api.updateColumn('name', { title: `이름 ${Math.floor(Math.random() * 90 + 10)}` }),
wide: (api: GridApi<Row>) => {
wide = !wide;
api.updateColumn('salary', { width: wide ? 200 : 120 });
},
align: (api: GridApi<Row>) => api.updateColumn('team', { align: 'center' }),
bad: (api: GridApi<Row>) => {
try {
api.updateColumn('name', { id: 'other' });
} catch (error) {
say({
ko: `막혔어요: ${(error as Error).message}`,
en: `Refused: ${(error as Error).message}`,
});
}
},
};버튼을 누르면 이 예제의 코드가 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">
<button
type="button"
[disabled]="!api"
(click)="api && actions.title(api)"
>이름 제목 바꾸기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.wide(api)"
>연봉 폭 넓히기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.align(api)"
>팀 가운데 정렬</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.bad(api)"
>금지된 변경</button>
</div>
<mx-grid style="height: 220px" [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, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
team: string;
city: string;
salary: number;
}
const COLUMNS: ColumnDef<Row>[] = [
{ id: 'id', title: '사번', width: 80, align: 'end' },
{ id: 'name', title: '이름', width: 110 },
{ id: 'team', title: '팀', width: 110 },
{ id: 'city', title: '근무지', width: 100 },
{ id: 'salary', title: '연봉(만원)', kind: 'number', width: 120, align: 'end' },
];
const ROWS: Row[] = [
{ id: 1001, name: '김민준', team: '개발팀', city: '서울', salary: 6200 },
{ id: 1002, name: '이서연', team: '디자인팀', city: '부산', salary: 4800 },
{ id: 1003, name: '박지호', team: '영업팀', city: '대구', salary: 7100 },
{ id: 1004, name: '최유나', team: '인사팀', city: '서울', salary: 3900 },
];
let say: (message: LogText) => void = () => {};
export const options: GridOptions<Row> = { columns: COLUMNS, rows: ROWS, rowKey: 'id' };
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
const off = api.on('columnsChanged', (e) =>
say({
ko: `columnsChanged: ${e.reason ?? 'set'} ${(e.updated ?? []).join(',')}`,
en: `columnsChanged: ${e.reason ?? 'set'} ${(e.updated ?? []).join(',')}`,
}),
);
return () => off();
}
let wide = false;
export const actions = {
title: (api: GridApi<Row>) =>
api.updateColumn('name', { title: `이름 ${Math.floor(Math.random() * 90 + 10)}` }),
wide: (api: GridApi<Row>) => {
wide = !wide;
api.updateColumn('salary', { width: wide ? 200 : 120 });
},
align: (api: GridApi<Row>) => api.updateColumn('team', { align: 'center' }),
bad: (api: GridApi<Row>) => {
try {
api.updateColumn('name', { id: 'other' });
} catch (error) {
say({
ko: `막혔어요: ${(error as Error).message}`,
en: `Refused: ${(error as Error).message}`,
});
}
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<div id="app">
<div class="toolbar">
<button type="button" id="title">Rename Name</button>
<button type="button" id="wide">Toggle Salary width</button>
<button type="button" id="align">Center Team</button>
<button type="button" id="bad">Forbidden change</button>
</div>
<div id="grid" style="height: 220px"></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, console.log);
document.getElementById('title').addEventListener('click', () => actions.title(api));
document.getElementById('wide').addEventListener('click', () => actions.wide(api));
document.getElementById('align').addEventListener('click', () => actions.align(api));
document.getElementById('bad').addEventListener('click', () => actions.bad(api));const COLUMNS = [
{ id: 'id', title: 'Emp. ID', width: 90, align: 'end' },
{ id: 'name', title: 'Name', width: 110 },
{ id: 'team', title: 'Team', width: 110 },
{ id: 'city', title: 'Office', width: 100 },
{ id: 'salary', title: 'Salary ($)', kind: 'number', width: 120, align: 'end' },
];
const ROWS = [
{ id: 1001, name: 'Liam Carter', team: 'Dev', city: 'Seoul', salary: 62000 },
{ id: 1002, name: 'Emma Stone', team: 'Design', city: 'Busan', salary: 48000 },
{ id: 1003, name: 'Noah Parker', team: 'Sales', city: 'Daegu', salary: 71000 },
{ id: 1004, name: 'Olivia Reed', team: 'HR', city: 'Seoul', salary: 39000 },
];
let say = () => { };
export const options = { columns: COLUMNS, rows: ROWS, rowKey: 'id' };
export function setup(api, log) {
say = log;
const off = api.on('columnsChanged', (e) => say(`columnsChanged: ${e.reason ?? 'set'} ${(e.updated ?? []).join(',')}`));
return () => off();
}
let wide = false;
export const actions = {
title: (api) => api.updateColumn('name', { title: `Name ${Math.floor(Math.random() * 90 + 10)}` }),
wide: (api) => {
wide = !wide;
api.updateColumn('salary', { width: wide ? 200 : 120 });
},
align: (api) => api.updateColumn('team', { align: 'center' }),
bad: (api) => {
try {
api.updateColumn('name', { id: 'other' });
}
catch (error) {
say(`Refused: ${error.message}`);
}
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Patch part of a column</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="title">Rename Name</button>
<button type="button" id="wide">Toggle Salary width</button>
<button type="button" id="align">Center Team</button>
<button type="button" id="bad">Forbidden change</button>
</div>
<div id="grid" style="height: 220px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const COLUMNS = [
{ id: 'id', title: 'Emp. ID', width: 90, align: 'end' },
{ id: 'name', title: 'Name', width: 110 },
{ id: 'team', title: 'Team', width: 110 },
{ id: 'city', title: 'Office', width: 100 },
{ id: 'salary', title: 'Salary ($)', kind: 'number', width: 120, align: 'end' },
];
const ROWS = [
{ id: 1001, name: 'Liam Carter', team: 'Dev', city: 'Seoul', salary: 62000 },
{ id: 1002, name: 'Emma Stone', team: 'Design', city: 'Busan', salary: 48000 },
{ id: 1003, name: 'Noah Parker', team: 'Sales', city: 'Daegu', salary: 71000 },
{ id: 1004, name: 'Olivia Reed', team: 'HR', city: 'Seoul', salary: 39000 },
];
let say = () => { };
const options = { columns: COLUMNS, rows: ROWS, rowKey: 'id' };
function setup(api, log) {
say = log;
const off = api.on('columnsChanged', (e) => say(`columnsChanged: ${e.reason ?? 'set'} ${(e.updated ?? []).join(',')}`));
return () => off();
}
let wide = false;
const actions = {
title: (api) => api.updateColumn('name', { title: `Name ${Math.floor(Math.random() * 90 + 10)}` }),
wide: (api) => {
wide = !wide;
api.updateColumn('salary', { width: wide ? 200 : 120 });
},
align: (api) => api.updateColumn('team', { align: 'center' }),
bad: (api) => {
try {
api.updateColumn('name', { id: 'other' });
}
catch (error) {
say(`Refused: ${error.message}`);
}
},
};
options.ariaLabel = "Patch part of a column";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
document.getElementById('title').addEventListener('click', () => actions.title(api));
document.getElementById('wide').addEventListener('click', () => actions.wide(api));
document.getElementById('align').addEventListener('click', () => actions.align(api));
document.getElementById('bad').addEventListener('click', () => actions.bad(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, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<button
type="button"
disabled={!api}
onClick={() => api && actions.title(api)}
>Rename Name</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.wide(api)}
>Toggle Salary width</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.align(api)}
>Center Team</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.bad(api)}
>Forbidden change</button>
</div>
<Grid style={{ height: 220 }} ref={gridRef} options={options} />
</>
);
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
team: string;
city: string;
salary: number;
}
const COLUMNS: ColumnDef<Row>[] = [
{ id: 'id', title: 'Emp. ID', width: 90, align: 'end' },
{ id: 'name', title: 'Name', width: 110 },
{ id: 'team', title: 'Team', width: 110 },
{ id: 'city', title: 'Office', width: 100 },
{ id: 'salary', title: 'Salary ($)', kind: 'number', width: 120, align: 'end' },
];
const ROWS: Row[] = [
{ id: 1001, name: 'Liam Carter', team: 'Dev', city: 'Seoul', salary: 62000 },
{ id: 1002, name: 'Emma Stone', team: 'Design', city: 'Busan', salary: 48000 },
{ id: 1003, name: 'Noah Parker', team: 'Sales', city: 'Daegu', salary: 71000 },
{ id: 1004, name: 'Olivia Reed', team: 'HR', city: 'Seoul', salary: 39000 },
];
let say: (message: LogText) => void = () => {};
export const options: GridOptions<Row> = { columns: COLUMNS, rows: ROWS, rowKey: 'id' };
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
const off = api.on('columnsChanged', (e) =>
say(`columnsChanged: ${e.reason ?? 'set'} ${(e.updated ?? []).join(',')}`),
);
return () => off();
}
let wide = false;
export const actions = {
title: (api: GridApi<Row>) =>
api.updateColumn('name', { title: `Name ${Math.floor(Math.random() * 90 + 10)}` }),
wide: (api: GridApi<Row>) => {
wide = !wide;
api.updateColumn('salary', { width: wide ? 200 : 120 });
},
align: (api: GridApi<Row>) => api.updateColumn('team', { align: 'center' }),
bad: (api: GridApi<Row>) => {
try {
api.updateColumn('name', { id: 'other' });
} catch (error) {
say(`Refused: ${(error as Error).message}`);
}
},
};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, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<button type="button" :disabled="!api" @click="api && actions.title(api)">Rename Name</button>
<button type="button" :disabled="!api" @click="api && actions.wide(api)">Toggle Salary width</button>
<button type="button" :disabled="!api" @click="api && actions.align(api)">Center Team</button>
<button type="button" :disabled="!api" @click="api && actions.bad(api)">Forbidden change</button>
</div>
<Grid style="height: 220px" :options="options" @ready="onReady" />
</template>import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
team: string;
city: string;
salary: number;
}
const COLUMNS: ColumnDef<Row>[] = [
{ id: 'id', title: 'Emp. ID', width: 90, align: 'end' },
{ id: 'name', title: 'Name', width: 110 },
{ id: 'team', title: 'Team', width: 110 },
{ id: 'city', title: 'Office', width: 100 },
{ id: 'salary', title: 'Salary ($)', kind: 'number', width: 120, align: 'end' },
];
const ROWS: Row[] = [
{ id: 1001, name: 'Liam Carter', team: 'Dev', city: 'Seoul', salary: 62000 },
{ id: 1002, name: 'Emma Stone', team: 'Design', city: 'Busan', salary: 48000 },
{ id: 1003, name: 'Noah Parker', team: 'Sales', city: 'Daegu', salary: 71000 },
{ id: 1004, name: 'Olivia Reed', team: 'HR', city: 'Seoul', salary: 39000 },
];
let say: (message: LogText) => void = () => {};
export const options: GridOptions<Row> = { columns: COLUMNS, rows: ROWS, rowKey: 'id' };
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
const off = api.on('columnsChanged', (e) =>
say(`columnsChanged: ${e.reason ?? 'set'} ${(e.updated ?? []).join(',')}`),
);
return () => off();
}
let wide = false;
export const actions = {
title: (api: GridApi<Row>) =>
api.updateColumn('name', { title: `Name ${Math.floor(Math.random() * 90 + 10)}` }),
wide: (api: GridApi<Row>) => {
wide = !wide;
api.updateColumn('salary', { width: wide ? 200 : 120 });
},
align: (api: GridApi<Row>) => api.updateColumn('team', { align: 'center' }),
bad: (api: GridApi<Row>) => {
try {
api.updateColumn('name', { id: 'other' });
} catch (error) {
say(`Refused: ${(error as Error).message}`);
}
},
};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">
<button
type="button"
[disabled]="!api"
(click)="api && actions.title(api)"
>Rename Name</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.wide(api)"
>Toggle Salary width</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.align(api)"
>Center Team</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.bad(api)"
>Forbidden change</button>
</div>
<mx-grid style="height: 220px" [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, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
team: string;
city: string;
salary: number;
}
const COLUMNS: ColumnDef<Row>[] = [
{ id: 'id', title: 'Emp. ID', width: 90, align: 'end' },
{ id: 'name', title: 'Name', width: 110 },
{ id: 'team', title: 'Team', width: 110 },
{ id: 'city', title: 'Office', width: 100 },
{ id: 'salary', title: 'Salary ($)', kind: 'number', width: 120, align: 'end' },
];
const ROWS: Row[] = [
{ id: 1001, name: 'Liam Carter', team: 'Dev', city: 'Seoul', salary: 62000 },
{ id: 1002, name: 'Emma Stone', team: 'Design', city: 'Busan', salary: 48000 },
{ id: 1003, name: 'Noah Parker', team: 'Sales', city: 'Daegu', salary: 71000 },
{ id: 1004, name: 'Olivia Reed', team: 'HR', city: 'Seoul', salary: 39000 },
];
let say: (message: LogText) => void = () => {};
export const options: GridOptions<Row> = { columns: COLUMNS, rows: ROWS, rowKey: 'id' };
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
const off = api.on('columnsChanged', (e) =>
say(`columnsChanged: ${e.reason ?? 'set'} ${(e.updated ?? []).join(',')}`),
);
return () => off();
}
let wide = false;
export const actions = {
title: (api: GridApi<Row>) =>
api.updateColumn('name', { title: `Name ${Math.floor(Math.random() * 90 + 10)}` }),
wide: (api: GridApi<Row>) => {
wide = !wide;
api.updateColumn('salary', { width: wide ? 200 : 120 });
},
align: (api: GridApi<Row>) => api.updateColumn('team', { align: 'center' }),
bad: (api: GridApi<Row>) => {
try {
api.updateColumn('name', { id: 'other' });
} catch (error) {
say(`Refused: ${(error as Error).message}`);
}
},
};