내장 배지와 직접 만든 칸 비교하기Built-in badge versus a custom cell
같은 상태 배지를 내장 기능으로 한 줄에, 직접 만든 칸으로 한 줄에 만들어서 어떻게 다른지 비교해요. 내장으로 되면 내장을, 모자랄 때만 직접 만들어요.Build the same status badge once with the built-in feature and once as a custom cell, and compare. Use the built-in one when it fits; build your own only when it falls short.
코드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="mark">
<option value="star" selected>★</option>
<option value="flag">🚩</option>
</select>
</div>
<div id="grid" style="height: 240px"></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('mark').addEventListener('change', (event) => actions.mark(api, event.target.value));// 칸 안의 내용은 기본으로 세로 가운데가 아니에요. .mxg-cell-center 상자에 넣으면 가운데에 와요.
const centered = (el) => {
const box = document.createElement('div');
box.className = 'mxg-cell-center';
el.append(box);
return box;
};
// 같은 배지를 두 가지로 만들어요. 왼쪽은 내장 'badge'(이름과 map만 적으면 끝), 오른쪽은 직접 만든 칸으로,
// 이쪽은 모양(HTML)과 가운데 맞춤, 색을 모두 직접 정해요. 내장으로 되면 내장을 쓰고, 내장에 없는 모양이나 동작이 필요할 때 직접 만드세요.
let flag = false;
const customChip = () => {
let chip;
return {
mount(el, ctx) {
chip = document.createElement('span');
chip.style.cssText =
'padding: 1px 10px; border-radius: 999px; font-weight: 600; background: color-mix(in srgb, var(--mxg-accent) 22%, var(--mxg-bg)); color: var(--mxg-fg); border: 1px solid var(--mxg-accent);';
centered(el).append(chip);
this.update(ctx);
},
update(ctx) {
chip.textContent = `${flag ? '🚩 ' : '★ '}${ctx.text}`;
},
destroy() { },
};
};
export const options = {
columns: [
{ id: 'order', title: '주문', width: 110 },
{
id: 'status',
title: '내장 배지',
width: 150,
cellRenderer: 'badge',
cellRendererParams: {
map: {
paid: { label: '결제 완료', tone: 'success' },
late: { label: '연체', tone: 'danger' },
},
},
},
{ id: 'custom', key: 'status', title: '직접 만든 칸', width: 170, cellRenderer: customChip },
],
rows: [
{ id: 1, order: 'SO-2001', status: 'paid' },
{ id: 2, order: 'SO-2002', status: 'late' },
{ id: 3, order: 'SO-2003', status: 'paid' },
{ id: 4, order: 'SO-2004', status: 'draft' },
],
rowKey: 'id',
};
let say = () => { };
export function setup(_api, log) {
say = log;
flag = false;
log({
ko: '내장 배지는 "paid"를 "결제 완료"로 바꿔 주고, 직접 만든 칸은 값 그대로에 ★을 붙여요.',
en: 'The built-in badge turns "paid" into "결제 완료" (paid); the cell you make yourself shows the value as it is plus a ★.',
});
}
export const actions = {
mark: (api, value) => {
flag = value === 'flag';
api.refresh(); // 칸을 다시 그려서 새 표시를 반영해요
say({
ko: flag
? '직접 만든 칸의 표시를 🚩로 바꿨어요. (내장 배지는 그대로예요)'
: '직접 만든 칸의 표시를 ★로 되돌렸어요.',
en: flag
? 'Changed the mark of the custom cell to 🚩. (The built-in badge is unchanged)'
: 'Changed the mark of the custom cell back to ★.',
});
},
};<!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="mark">
<option value="star" selected>★</option>
<option value="flag">🚩</option>
</select>
</div>
<div id="grid" style="height: 240px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
// 칸 안의 내용은 기본으로 세로 가운데가 아니에요. .mxg-cell-center 상자에 넣으면 가운데에 와요.
const centered = (el) => {
const box = document.createElement('div');
box.className = 'mxg-cell-center';
el.append(box);
return box;
};
// 같은 배지를 두 가지로 만들어요. 왼쪽은 내장 'badge'(이름과 map만 적으면 끝), 오른쪽은 직접 만든 칸으로,
// 이쪽은 모양(HTML)과 가운데 맞춤, 색을 모두 직접 정해요. 내장으로 되면 내장을 쓰고, 내장에 없는 모양이나 동작이 필요할 때 직접 만드세요.
let flag = false;
const customChip = () => {
let chip;
return {
mount(el, ctx) {
chip = document.createElement('span');
chip.style.cssText =
'padding: 1px 10px; border-radius: 999px; font-weight: 600; background: color-mix(in srgb, var(--mxg-accent) 22%, var(--mxg-bg)); color: var(--mxg-fg); border: 1px solid var(--mxg-accent);';
centered(el).append(chip);
this.update(ctx);
},
update(ctx) {
chip.textContent = `${flag ? '🚩 ' : '★ '}${ctx.text}`;
},
destroy() { },
};
};
const options = {
columns: [
{ id: 'order', title: '주문', width: 110 },
{
id: 'status',
title: '내장 배지',
width: 150,
cellRenderer: 'badge',
cellRendererParams: {
map: {
paid: { label: '결제 완료', tone: 'success' },
late: { label: '연체', tone: 'danger' },
},
},
},
{ id: 'custom', key: 'status', title: '직접 만든 칸', width: 170, cellRenderer: customChip },
],
rows: [
{ id: 1, order: 'SO-2001', status: 'paid' },
{ id: 2, order: 'SO-2002', status: 'late' },
{ id: 3, order: 'SO-2003', status: 'paid' },
{ id: 4, order: 'SO-2004', status: 'draft' },
],
rowKey: 'id',
};
let say = () => { };
function setup(_api, log) {
say = log;
flag = false;
log({
ko: '내장 배지는 "paid"를 "결제 완료"로 바꿔 주고, 직접 만든 칸은 값 그대로에 ★을 붙여요.',
en: 'The built-in badge turns "paid" into "결제 완료" (paid); the cell you make yourself shows the value as it is plus a ★.',
});
}
const actions = {
mark: (api, value) => {
flag = value === 'flag';
api.refresh(); // 칸을 다시 그려서 새 표시를 반영해요
say({
ko: flag
? '직접 만든 칸의 표시를 🚩로 바꿨어요. (내장 배지는 그대로예요)'
: '직접 만든 칸의 표시를 ★로 되돌렸어요.',
en: flag
? 'Changed the mark of the custom cell to 🚩. (The built-in badge is unchanged)'
: 'Changed the mark of the custom cell back to ★.',
});
},
};
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('mark').addEventListener('change', (event) => actions.mark(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, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<select
aria-label="직접 만든 칸의 표시"
disabled={!api}
onChange={(event) => api && actions.mark(api, event.target.value)}
>
<option value="star">★</option>
<option value="flag">🚩</option>
</select>
</div>
<Grid style={{ height: 240 }} ref={gridRef} options={options} />
</>
);
}import type { CellRendererAdapter, GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
order: string;
status: string;
}
// 칸 안의 내용은 기본으로 세로 가운데가 아니에요. .mxg-cell-center 상자에 넣으면 가운데에 와요.
const centered = (el: HTMLElement): HTMLElement => {
const box = document.createElement('div');
box.className = 'mxg-cell-center';
el.append(box);
return box;
};
// 같은 배지를 두 가지로 만들어요. 왼쪽은 내장 'badge'(이름과 map만 적으면 끝), 오른쪽은 직접 만든 칸으로,
// 이쪽은 모양(HTML)과 가운데 맞춤, 색을 모두 직접 정해요. 내장으로 되면 내장을 쓰고, 내장에 없는 모양이나 동작이 필요할 때 직접 만드세요.
let flag = false;
const customChip = (): CellRendererAdapter<Row> => {
let chip: HTMLSpanElement;
return {
mount(el, ctx) {
chip = document.createElement('span');
chip.style.cssText =
'padding: 1px 10px; border-radius: 999px; font-weight: 600; background: color-mix(in srgb, var(--mxg-accent) 22%, var(--mxg-bg)); color: var(--mxg-fg); border: 1px solid var(--mxg-accent);';
centered(el).append(chip);
this.update(ctx);
},
update(ctx) {
chip.textContent = `${flag ? '🚩 ' : '★ '}${ctx.text}`;
},
destroy() {},
};
};
export const options: GridOptions<Row> = {
columns: [
{ id: 'order', title: '주문', width: 110 },
{
id: 'status',
title: '내장 배지',
width: 150,
cellRenderer: 'badge',
cellRendererParams: {
map: {
paid: { label: '결제 완료', tone: 'success' },
late: { label: '연체', tone: 'danger' },
},
},
},
{ id: 'custom', key: 'status', title: '직접 만든 칸', width: 170, cellRenderer: customChip },
],
rows: [
{ id: 1, order: 'SO-2001', status: 'paid' },
{ id: 2, order: 'SO-2002', status: 'late' },
{ id: 3, order: 'SO-2003', status: 'paid' },
{ id: 4, order: 'SO-2004', status: 'draft' },
],
rowKey: 'id',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
flag = false;
log({
ko: '내장 배지는 "paid"를 "결제 완료"로 바꿔 주고, 직접 만든 칸은 값 그대로에 ★을 붙여요.',
en: 'The built-in badge turns "paid" into "결제 완료" (paid); the cell you make yourself shows the value as it is plus a ★.',
});
}
export const actions = {
mark: (api: GridApi<Row>, value?: string) => {
flag = value === 'flag';
api.refresh(); // 칸을 다시 그려서 새 표시를 반영해요
say({
ko: flag
? '직접 만든 칸의 표시를 🚩로 바꿨어요. (내장 배지는 그대로예요)'
: '직접 만든 칸의 표시를 ★로 되돌렸어요.',
en: flag
? 'Changed the mark of the custom cell to 🚩. (The built-in badge is unchanged)'
: 'Changed the mark of the custom cell back to ★.',
});
},
};버튼을 누르면 이 예제의 코드가 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">
<select
aria-label="직접 만든 칸의 표시"
:disabled="!api"
@change="api && actions.mark(api, ($event.target as HTMLSelectElement).value)"
>
<option value="star" selected>★</option>
<option value="flag">🚩</option>
</select>
</div>
<Grid style="height: 240px" :options="options" @ready="onReady" />
</template>import type { CellRendererAdapter, GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
order: string;
status: string;
}
// 칸 안의 내용은 기본으로 세로 가운데가 아니에요. .mxg-cell-center 상자에 넣으면 가운데에 와요.
const centered = (el: HTMLElement): HTMLElement => {
const box = document.createElement('div');
box.className = 'mxg-cell-center';
el.append(box);
return box;
};
// 같은 배지를 두 가지로 만들어요. 왼쪽은 내장 'badge'(이름과 map만 적으면 끝), 오른쪽은 직접 만든 칸으로,
// 이쪽은 모양(HTML)과 가운데 맞춤, 색을 모두 직접 정해요. 내장으로 되면 내장을 쓰고, 내장에 없는 모양이나 동작이 필요할 때 직접 만드세요.
let flag = false;
const customChip = (): CellRendererAdapter<Row> => {
let chip: HTMLSpanElement;
return {
mount(el, ctx) {
chip = document.createElement('span');
chip.style.cssText =
'padding: 1px 10px; border-radius: 999px; font-weight: 600; background: color-mix(in srgb, var(--mxg-accent) 22%, var(--mxg-bg)); color: var(--mxg-fg); border: 1px solid var(--mxg-accent);';
centered(el).append(chip);
this.update(ctx);
},
update(ctx) {
chip.textContent = `${flag ? '🚩 ' : '★ '}${ctx.text}`;
},
destroy() {},
};
};
export const options: GridOptions<Row> = {
columns: [
{ id: 'order', title: '주문', width: 110 },
{
id: 'status',
title: '내장 배지',
width: 150,
cellRenderer: 'badge',
cellRendererParams: {
map: {
paid: { label: '결제 완료', tone: 'success' },
late: { label: '연체', tone: 'danger' },
},
},
},
{ id: 'custom', key: 'status', title: '직접 만든 칸', width: 170, cellRenderer: customChip },
],
rows: [
{ id: 1, order: 'SO-2001', status: 'paid' },
{ id: 2, order: 'SO-2002', status: 'late' },
{ id: 3, order: 'SO-2003', status: 'paid' },
{ id: 4, order: 'SO-2004', status: 'draft' },
],
rowKey: 'id',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
flag = false;
log({
ko: '내장 배지는 "paid"를 "결제 완료"로 바꿔 주고, 직접 만든 칸은 값 그대로에 ★을 붙여요.',
en: 'The built-in badge turns "paid" into "결제 완료" (paid); the cell you make yourself shows the value as it is plus a ★.',
});
}
export const actions = {
mark: (api: GridApi<Row>, value?: string) => {
flag = value === 'flag';
api.refresh(); // 칸을 다시 그려서 새 표시를 반영해요
say({
ko: flag
? '직접 만든 칸의 표시를 🚩로 바꿨어요. (내장 배지는 그대로예요)'
: '직접 만든 칸의 표시를 ★로 되돌렸어요.',
en: flag
? 'Changed the mark of the custom cell to 🚩. (The built-in badge is unchanged)'
: 'Changed the mark of the custom cell back to ★.',
});
},
};버튼을 누르면 이 예제의 코드가 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.mark(api, $any($event.target).value)"
>
<option value="star" selected>★</option>
<option value="flag">🚩</option>
</select>
</div>
<mx-grid style="height: 240px" [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 { CellRendererAdapter, GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
order: string;
status: string;
}
// 칸 안의 내용은 기본으로 세로 가운데가 아니에요. .mxg-cell-center 상자에 넣으면 가운데에 와요.
const centered = (el: HTMLElement): HTMLElement => {
const box = document.createElement('div');
box.className = 'mxg-cell-center';
el.append(box);
return box;
};
// 같은 배지를 두 가지로 만들어요. 왼쪽은 내장 'badge'(이름과 map만 적으면 끝), 오른쪽은 직접 만든 칸으로,
// 이쪽은 모양(HTML)과 가운데 맞춤, 색을 모두 직접 정해요. 내장으로 되면 내장을 쓰고, 내장에 없는 모양이나 동작이 필요할 때 직접 만드세요.
let flag = false;
const customChip = (): CellRendererAdapter<Row> => {
let chip: HTMLSpanElement;
return {
mount(el, ctx) {
chip = document.createElement('span');
chip.style.cssText =
'padding: 1px 10px; border-radius: 999px; font-weight: 600; background: color-mix(in srgb, var(--mxg-accent) 22%, var(--mxg-bg)); color: var(--mxg-fg); border: 1px solid var(--mxg-accent);';
centered(el).append(chip);
this.update(ctx);
},
update(ctx) {
chip.textContent = `${flag ? '🚩 ' : '★ '}${ctx.text}`;
},
destroy() {},
};
};
export const options: GridOptions<Row> = {
columns: [
{ id: 'order', title: '주문', width: 110 },
{
id: 'status',
title: '내장 배지',
width: 150,
cellRenderer: 'badge',
cellRendererParams: {
map: {
paid: { label: '결제 완료', tone: 'success' },
late: { label: '연체', tone: 'danger' },
},
},
},
{ id: 'custom', key: 'status', title: '직접 만든 칸', width: 170, cellRenderer: customChip },
],
rows: [
{ id: 1, order: 'SO-2001', status: 'paid' },
{ id: 2, order: 'SO-2002', status: 'late' },
{ id: 3, order: 'SO-2003', status: 'paid' },
{ id: 4, order: 'SO-2004', status: 'draft' },
],
rowKey: 'id',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
flag = false;
log({
ko: '내장 배지는 "paid"를 "결제 완료"로 바꿔 주고, 직접 만든 칸은 값 그대로에 ★을 붙여요.',
en: 'The built-in badge turns "paid" into "결제 완료" (paid); the cell you make yourself shows the value as it is plus a ★.',
});
}
export const actions = {
mark: (api: GridApi<Row>, value?: string) => {
flag = value === 'flag';
api.refresh(); // 칸을 다시 그려서 새 표시를 반영해요
say({
ko: flag
? '직접 만든 칸의 표시를 🚩로 바꿨어요. (내장 배지는 그대로예요)'
: '직접 만든 칸의 표시를 ★로 되돌렸어요.',
en: flag
? 'Changed the mark of the custom cell to 🚩. (The built-in badge is unchanged)'
: 'Changed the mark of the custom cell back to ★.',
});
},
};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="Mark in the custom cell" id="mark">
<option value="star" selected>Star</option>
<option value="flag">Flag</option>
</select>
</div>
<div id="grid" style="height: 240px"></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('mark').addEventListener('change', (event) => actions.mark(api, event.target.value));// Cell content is not vertically centered by default. Putting it in a .mxg-cell-center box centers it.
const centered = (el) => {
const box = document.createElement('div');
box.className = 'mxg-cell-center';
el.append(box);
return box;
};
// Builds the same badge two ways. The left one is the built-in 'badge' (just a name and a map); the right one is a custom cell
// where you set the markup (HTML), the centering and the color yourself. Use the built-in one when it fits; build your own when you need a look or behavior it lacks.
let flag = false;
const customChip = () => {
let chip;
return {
mount(el, ctx) {
chip = document.createElement('span');
chip.style.cssText =
'padding: 1px 10px; border-radius: 999px; font-weight: 600; background: color-mix(in srgb, var(--mxg-accent) 22%, var(--mxg-bg)); color: var(--mxg-fg); border: 1px solid var(--mxg-accent);';
centered(el).append(chip);
this.update(ctx);
},
update(ctx) {
chip.textContent = `${flag ? '🚩 ' : '★ '}${ctx.text}`;
},
destroy() { },
};
};
export const options = {
columns: [
{ id: 'order', title: 'Order', width: 110 },
{
id: 'status',
title: 'Built-in badge',
width: 150,
cellRenderer: 'badge',
cellRendererParams: {
map: {
paid: { label: 'Paid', tone: 'success' },
late: { label: 'Overdue', tone: 'danger' },
},
},
},
{ id: 'custom', key: 'status', title: 'Custom cell', width: 170, cellRenderer: customChip },
],
rows: [
{ id: 1, order: 'SO-2001', status: 'paid' },
{ id: 2, order: 'SO-2002', status: 'late' },
{ id: 3, order: 'SO-2003', status: 'paid' },
{ id: 4, order: 'SO-2004', status: 'draft' },
],
rowKey: 'id',
};
let say = () => { };
export function setup(_api, log) {
say = log;
flag = false;
log('The built-in badge turns "paid" into "Paid"; the custom cell shows the value as it is plus a ★.');
}
export const actions = {
mark: (api, value) => {
flag = value === 'flag';
api.refresh(); // redraw the cells to apply the new mark
say(flag
? 'Changed the mark of the custom cell to 🚩. (The built-in badge is unchanged)'
: 'Changed the mark of the custom cell back to ★.');
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Built-in badge versus a custom cell</title>
</head>
<body>
<div class="toolbar">
<select aria-label="Mark in the custom cell" id="mark">
<option value="star" selected>Star</option>
<option value="flag">Flag</option>
</select>
</div>
<div id="grid" style="height: 240px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
// Cell content is not vertically centered by default. Putting it in a .mxg-cell-center box centers it.
const centered = (el) => {
const box = document.createElement('div');
box.className = 'mxg-cell-center';
el.append(box);
return box;
};
// Builds the same badge two ways. The left one is the built-in 'badge' (just a name and a map); the right one is a custom cell
// where you set the markup (HTML), the centering and the color yourself. Use the built-in one when it fits; build your own when you need a look or behavior it lacks.
let flag = false;
const customChip = () => {
let chip;
return {
mount(el, ctx) {
chip = document.createElement('span');
chip.style.cssText =
'padding: 1px 10px; border-radius: 999px; font-weight: 600; background: color-mix(in srgb, var(--mxg-accent) 22%, var(--mxg-bg)); color: var(--mxg-fg); border: 1px solid var(--mxg-accent);';
centered(el).append(chip);
this.update(ctx);
},
update(ctx) {
chip.textContent = `${flag ? '🚩 ' : '★ '}${ctx.text}`;
},
destroy() { },
};
};
const options = {
columns: [
{ id: 'order', title: 'Order', width: 110 },
{
id: 'status',
title: 'Built-in badge',
width: 150,
cellRenderer: 'badge',
cellRendererParams: {
map: {
paid: { label: 'Paid', tone: 'success' },
late: { label: 'Overdue', tone: 'danger' },
},
},
},
{ id: 'custom', key: 'status', title: 'Custom cell', width: 170, cellRenderer: customChip },
],
rows: [
{ id: 1, order: 'SO-2001', status: 'paid' },
{ id: 2, order: 'SO-2002', status: 'late' },
{ id: 3, order: 'SO-2003', status: 'paid' },
{ id: 4, order: 'SO-2004', status: 'draft' },
],
rowKey: 'id',
};
let say = () => { };
function setup(_api, log) {
say = log;
flag = false;
log('The built-in badge turns "paid" into "Paid"; the custom cell shows the value as it is plus a ★.');
}
const actions = {
mark: (api, value) => {
flag = value === 'flag';
api.refresh(); // redraw the cells to apply the new mark
say(flag
? 'Changed the mark of the custom cell to 🚩. (The built-in badge is unchanged)'
: 'Changed the mark of the custom cell back to ★.');
},
};
options.ariaLabel = "Built-in badge versus a custom cell";
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('mark').addEventListener('change', (event) => actions.mark(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, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<select
aria-label="Mark in the custom cell"
disabled={!api}
onChange={(event) => api && actions.mark(api, event.target.value)}
>
<option value="star">Star</option>
<option value="flag">Flag</option>
</select>
</div>
<Grid style={{ height: 240 }} ref={gridRef} options={options} />
</>
);
}import type { CellRendererAdapter, GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
order: string;
status: string;
}
// Cell content is not vertically centered by default. Putting it in a .mxg-cell-center box centers it.
const centered = (el: HTMLElement): HTMLElement => {
const box = document.createElement('div');
box.className = 'mxg-cell-center';
el.append(box);
return box;
};
// Builds the same badge two ways. The left one is the built-in 'badge' (just a name and a map); the right one is a custom cell
// where you set the markup (HTML), the centering and the color yourself. Use the built-in one when it fits; build your own when you need a look or behavior it lacks.
let flag = false;
const customChip = (): CellRendererAdapter<Row> => {
let chip: HTMLSpanElement;
return {
mount(el, ctx) {
chip = document.createElement('span');
chip.style.cssText =
'padding: 1px 10px; border-radius: 999px; font-weight: 600; background: color-mix(in srgb, var(--mxg-accent) 22%, var(--mxg-bg)); color: var(--mxg-fg); border: 1px solid var(--mxg-accent);';
centered(el).append(chip);
this.update(ctx);
},
update(ctx) {
chip.textContent = `${flag ? '🚩 ' : '★ '}${ctx.text}`;
},
destroy() {},
};
};
export const options: GridOptions<Row> = {
columns: [
{ id: 'order', title: 'Order', width: 110 },
{
id: 'status',
title: 'Built-in badge',
width: 150,
cellRenderer: 'badge',
cellRendererParams: {
map: {
paid: { label: 'Paid', tone: 'success' },
late: { label: 'Overdue', tone: 'danger' },
},
},
},
{ id: 'custom', key: 'status', title: 'Custom cell', width: 170, cellRenderer: customChip },
],
rows: [
{ id: 1, order: 'SO-2001', status: 'paid' },
{ id: 2, order: 'SO-2002', status: 'late' },
{ id: 3, order: 'SO-2003', status: 'paid' },
{ id: 4, order: 'SO-2004', status: 'draft' },
],
rowKey: 'id',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
flag = false;
log(
'The built-in badge turns "paid" into "Paid"; the custom cell shows the value as it is plus a ★.',
);
}
export const actions = {
mark: (api: GridApi<Row>, value?: string) => {
flag = value === 'flag';
api.refresh(); // redraw the cells to apply the new mark
say(
flag
? 'Changed the mark of the custom cell to 🚩. (The built-in badge is unchanged)'
: 'Changed the mark of the custom cell back to ★.',
);
},
};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">
<select
aria-label="Mark in the custom cell"
:disabled="!api"
@change="api && actions.mark(api, ($event.target as HTMLSelectElement).value)"
>
<option value="star" selected>Star</option>
<option value="flag">Flag</option>
</select>
</div>
<Grid style="height: 240px" :options="options" @ready="onReady" />
</template>import type { CellRendererAdapter, GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
order: string;
status: string;
}
// Cell content is not vertically centered by default. Putting it in a .mxg-cell-center box centers it.
const centered = (el: HTMLElement): HTMLElement => {
const box = document.createElement('div');
box.className = 'mxg-cell-center';
el.append(box);
return box;
};
// Builds the same badge two ways. The left one is the built-in 'badge' (just a name and a map); the right one is a custom cell
// where you set the markup (HTML), the centering and the color yourself. Use the built-in one when it fits; build your own when you need a look or behavior it lacks.
let flag = false;
const customChip = (): CellRendererAdapter<Row> => {
let chip: HTMLSpanElement;
return {
mount(el, ctx) {
chip = document.createElement('span');
chip.style.cssText =
'padding: 1px 10px; border-radius: 999px; font-weight: 600; background: color-mix(in srgb, var(--mxg-accent) 22%, var(--mxg-bg)); color: var(--mxg-fg); border: 1px solid var(--mxg-accent);';
centered(el).append(chip);
this.update(ctx);
},
update(ctx) {
chip.textContent = `${flag ? '🚩 ' : '★ '}${ctx.text}`;
},
destroy() {},
};
};
export const options: GridOptions<Row> = {
columns: [
{ id: 'order', title: 'Order', width: 110 },
{
id: 'status',
title: 'Built-in badge',
width: 150,
cellRenderer: 'badge',
cellRendererParams: {
map: {
paid: { label: 'Paid', tone: 'success' },
late: { label: 'Overdue', tone: 'danger' },
},
},
},
{ id: 'custom', key: 'status', title: 'Custom cell', width: 170, cellRenderer: customChip },
],
rows: [
{ id: 1, order: 'SO-2001', status: 'paid' },
{ id: 2, order: 'SO-2002', status: 'late' },
{ id: 3, order: 'SO-2003', status: 'paid' },
{ id: 4, order: 'SO-2004', status: 'draft' },
],
rowKey: 'id',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
flag = false;
log(
'The built-in badge turns "paid" into "Paid"; the custom cell shows the value as it is plus a ★.',
);
}
export const actions = {
mark: (api: GridApi<Row>, value?: string) => {
flag = value === 'flag';
api.refresh(); // redraw the cells to apply the new mark
say(
flag
? 'Changed the mark of the custom cell to 🚩. (The built-in badge is unchanged)'
: 'Changed the mark of the custom cell back to ★.',
);
},
};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="Mark in the custom cell"
[disabled]="!api"
(change)="api && actions.mark(api, $any($event.target).value)"
>
<option value="star" selected>Star</option>
<option value="flag">Flag</option>
</select>
</div>
<mx-grid style="height: 240px" [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 { CellRendererAdapter, GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
order: string;
status: string;
}
// Cell content is not vertically centered by default. Putting it in a .mxg-cell-center box centers it.
const centered = (el: HTMLElement): HTMLElement => {
const box = document.createElement('div');
box.className = 'mxg-cell-center';
el.append(box);
return box;
};
// Builds the same badge two ways. The left one is the built-in 'badge' (just a name and a map); the right one is a custom cell
// where you set the markup (HTML), the centering and the color yourself. Use the built-in one when it fits; build your own when you need a look or behavior it lacks.
let flag = false;
const customChip = (): CellRendererAdapter<Row> => {
let chip: HTMLSpanElement;
return {
mount(el, ctx) {
chip = document.createElement('span');
chip.style.cssText =
'padding: 1px 10px; border-radius: 999px; font-weight: 600; background: color-mix(in srgb, var(--mxg-accent) 22%, var(--mxg-bg)); color: var(--mxg-fg); border: 1px solid var(--mxg-accent);';
centered(el).append(chip);
this.update(ctx);
},
update(ctx) {
chip.textContent = `${flag ? '🚩 ' : '★ '}${ctx.text}`;
},
destroy() {},
};
};
export const options: GridOptions<Row> = {
columns: [
{ id: 'order', title: 'Order', width: 110 },
{
id: 'status',
title: 'Built-in badge',
width: 150,
cellRenderer: 'badge',
cellRendererParams: {
map: {
paid: { label: 'Paid', tone: 'success' },
late: { label: 'Overdue', tone: 'danger' },
},
},
},
{ id: 'custom', key: 'status', title: 'Custom cell', width: 170, cellRenderer: customChip },
],
rows: [
{ id: 1, order: 'SO-2001', status: 'paid' },
{ id: 2, order: 'SO-2002', status: 'late' },
{ id: 3, order: 'SO-2003', status: 'paid' },
{ id: 4, order: 'SO-2004', status: 'draft' },
],
rowKey: 'id',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
flag = false;
log(
'The built-in badge turns "paid" into "Paid"; the custom cell shows the value as it is plus a ★.',
);
}
export const actions = {
mark: (api: GridApi<Row>, value?: string) => {
flag = value === 'flag';
api.refresh(); // redraw the cells to apply the new mark
say(
flag
? 'Changed the mark of the custom cell to 🚩. (The built-in badge is unchanged)'
: 'Changed the mark of the custom cell back to ★.',
);
},
};