상태 배지: 값에 따라 이름과 색 정하기Status badges: names and colors by value
주문 상태나 재고 같은 값을 색 배지로 보여 줘요. 값마다 이름과 색을 정하거나, 숫자에 따라 색을 고를 수 있어요.Show values such as order status or stock as colored badges. Name and color can be set per value, or chosen by the number.
코드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="warn">
<option value="5">5개 미만</option>
<option value="10" selected>10개 미만</option>
<option value="20">20개 미만</option>
</select>
<button type="button" id="next">1번 줄 상태 바꾸기</button>
</div>
<div id="grid" style="height: 280px"></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('warn').addEventListener('change', (event) => actions.warn(api, event.target.value));
document.getElementById('next').addEventListener('click', () => actions.next(api));// cellRenderer: 'badge'는 값을 색 배지로 그려요. map은 값마다 이름과 색(tone)을 정하고, map에 없는 값은 그대로 보여 줘요.
// tone에 함수를 주면 값에 따라 색을 골라요. 배지 뒤의 값은 그대로 글자·숫자라서 정렬·검색은 원래 값으로 해요.
const rows = [
{ id: 1, order: 'SO-1001', customer: '한빛푸드', status: 'paid', stock: 42 },
{ id: 2, order: 'SO-1002', customer: '파란마켓', status: 'pending', stock: 4 },
{ id: 3, order: 'SO-1003', customer: '단풍상회', status: 'shipped', stock: 0 },
{ id: 4, order: 'SO-1004', customer: '바다유통', status: 'refunded', stock: 12 },
{ id: 5, order: 'SO-1005', customer: '새벽식자재', status: '', stock: 9 },
];
const order = ['paid', 'pending', 'shipped', 'failed'];
let warnBelow = 10;
const columns = () => [
{ id: 'order', title: '주문', width: 100 },
{ id: 'customer', title: '거래처', width: 110 },
{
id: 'status',
title: '상태',
width: 110,
cellRenderer: 'badge',
cellRendererParams: {
map: {
paid: { label: '결제 완료', tone: 'success' },
pending: { label: '입금 대기', tone: 'warning' },
shipped: { label: '배송 중', tone: 'info' },
failed: { label: '결제 실패', tone: 'danger' },
},
},
},
{
id: 'stock',
title: '재고',
kind: 'number',
width: 120,
formatValue: (value) => (value === 0 ? '품절' : `${String(value)}개 남음`),
cellRenderer: 'badge',
cellRendererParams: {
tone: (value) => Number(value) === 0 ? 'danger' : Number(value) < warnBelow ? 'warning' : 'success',
},
},
];
export const options = {
columns: columns(),
rows,
rowKey: 'id',
saveMode: 'none',
};
let say = () => { };
const names = {
paid: '결제 완료',
pending: '입금 대기',
shipped: '배송 중',
failed: '결제 실패',
};
export function setup(_api, log) {
say = log;
warnBelow = 10;
log({
ko: '"refunded"처럼 정해 두지 않은 값은 글자 그대로, 빈 값은 비어 보여요.',
en: 'A value that is not defined, like "refunded", is shown as plain text, and an empty value shows nothing.',
});
}
export const actions = {
warn: (api, value) => {
warnBelow = Number(value ?? '10');
api.setColumns(columns());
say({
ko: `재고가 ${warnBelow}개보다 적으면 노란색으로 보여요.`,
en: `Stock below ${warnBelow} shows in yellow.`,
});
},
next: (api) => {
const row = api.getRowByKey(1);
if (!row)
return;
const after = order[(order.indexOf(row.status) + 1) % order.length];
api.updateRows([{ ...row, status: after }]);
say({
ko: `1번 줄 상태가 바뀌었어요 → ${names[after]}`,
en: `Row 1 status changed → ${names[after]}`,
});
},
};<!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="warn">
<option value="5">5개 미만</option>
<option value="10" selected>10개 미만</option>
<option value="20">20개 미만</option>
</select>
<button type="button" id="next">1번 줄 상태 바꾸기</button>
</div>
<div id="grid" style="height: 280px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
// cellRenderer: 'badge'는 값을 색 배지로 그려요. map은 값마다 이름과 색(tone)을 정하고, map에 없는 값은 그대로 보여 줘요.
// tone에 함수를 주면 값에 따라 색을 골라요. 배지 뒤의 값은 그대로 글자·숫자라서 정렬·검색은 원래 값으로 해요.
const rows = [
{ id: 1, order: 'SO-1001', customer: '한빛푸드', status: 'paid', stock: 42 },
{ id: 2, order: 'SO-1002', customer: '파란마켓', status: 'pending', stock: 4 },
{ id: 3, order: 'SO-1003', customer: '단풍상회', status: 'shipped', stock: 0 },
{ id: 4, order: 'SO-1004', customer: '바다유통', status: 'refunded', stock: 12 },
{ id: 5, order: 'SO-1005', customer: '새벽식자재', status: '', stock: 9 },
];
const order = ['paid', 'pending', 'shipped', 'failed'];
let warnBelow = 10;
const columns = () => [
{ id: 'order', title: '주문', width: 100 },
{ id: 'customer', title: '거래처', width: 110 },
{
id: 'status',
title: '상태',
width: 110,
cellRenderer: 'badge',
cellRendererParams: {
map: {
paid: { label: '결제 완료', tone: 'success' },
pending: { label: '입금 대기', tone: 'warning' },
shipped: { label: '배송 중', tone: 'info' },
failed: { label: '결제 실패', tone: 'danger' },
},
},
},
{
id: 'stock',
title: '재고',
kind: 'number',
width: 120,
formatValue: (value) => (value === 0 ? '품절' : `${String(value)}개 남음`),
cellRenderer: 'badge',
cellRendererParams: {
tone: (value) => Number(value) === 0 ? 'danger' : Number(value) < warnBelow ? 'warning' : 'success',
},
},
];
const options = {
columns: columns(),
rows,
rowKey: 'id',
saveMode: 'none',
};
let say = () => { };
const names = {
paid: '결제 완료',
pending: '입금 대기',
shipped: '배송 중',
failed: '결제 실패',
};
function setup(_api, log) {
say = log;
warnBelow = 10;
log({
ko: '"refunded"처럼 정해 두지 않은 값은 글자 그대로, 빈 값은 비어 보여요.',
en: 'A value that is not defined, like "refunded", is shown as plain text, and an empty value shows nothing.',
});
}
const actions = {
warn: (api, value) => {
warnBelow = Number(value ?? '10');
api.setColumns(columns());
say({
ko: `재고가 ${warnBelow}개보다 적으면 노란색으로 보여요.`,
en: `Stock below ${warnBelow} shows in yellow.`,
});
},
next: (api) => {
const row = api.getRowByKey(1);
if (!row)
return;
const after = order[(order.indexOf(row.status) + 1) % order.length];
api.updateRows([{ ...row, status: after }]);
say({
ko: `1번 줄 상태가 바뀌었어요 → ${names[after]}`,
en: `Row 1 status changed → ${names[after]}`,
});
},
};
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('warn').addEventListener('change', (event) => actions.warn(api, event.target.value));
document.getElementById('next').addEventListener('click', () => actions.next(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">
<select
aria-label="노란색 기준"
disabled={!api}
onChange={(event) => api && actions.warn(api, event.target.value)}
>
<option value="5">5개 미만</option>
<option value="10">10개 미만</option>
<option value="20">20개 미만</option>
</select>
<button
type="button"
disabled={!api}
onClick={() => api && actions.next(api)}
>1번 줄 상태 바꾸기</button>
</div>
<Grid style={{ height: 280 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
order: string;
customer: string;
status: string;
stock: number;
}
// cellRenderer: 'badge'는 값을 색 배지로 그려요. map은 값마다 이름과 색(tone)을 정하고, map에 없는 값은 그대로 보여 줘요.
// tone에 함수를 주면 값에 따라 색을 골라요. 배지 뒤의 값은 그대로 글자·숫자라서 정렬·검색은 원래 값으로 해요.
const rows: Row[] = [
{ id: 1, order: 'SO-1001', customer: '한빛푸드', status: 'paid', stock: 42 },
{ id: 2, order: 'SO-1002', customer: '파란마켓', status: 'pending', stock: 4 },
{ id: 3, order: 'SO-1003', customer: '단풍상회', status: 'shipped', stock: 0 },
{ id: 4, order: 'SO-1004', customer: '바다유통', status: 'refunded', stock: 12 },
{ id: 5, order: 'SO-1005', customer: '새벽식자재', status: '', stock: 9 },
];
const order = ['paid', 'pending', 'shipped', 'failed'];
let warnBelow = 10;
const columns = (): NonNullable<GridOptions<Row>['columns']> => [
{ id: 'order', title: '주문', width: 100 },
{ id: 'customer', title: '거래처', width: 110 },
{
id: 'status',
title: '상태',
width: 110,
cellRenderer: 'badge',
cellRendererParams: {
map: {
paid: { label: '결제 완료', tone: 'success' },
pending: { label: '입금 대기', tone: 'warning' },
shipped: { label: '배송 중', tone: 'info' },
failed: { label: '결제 실패', tone: 'danger' },
},
},
},
{
id: 'stock',
title: '재고',
kind: 'number',
width: 120,
formatValue: (value) => (value === 0 ? '품절' : `${String(value)}개 남음`),
cellRenderer: 'badge',
cellRendererParams: {
tone: (value: unknown) =>
Number(value) === 0 ? 'danger' : Number(value) < warnBelow ? 'warning' : 'success',
},
},
];
export const options: GridOptions<Row> = {
columns: columns(),
rows,
rowKey: 'id',
saveMode: 'none',
};
let say: (message: LogText) => void = () => {};
const names: Record<string, string> = {
paid: '결제 완료',
pending: '입금 대기',
shipped: '배송 중',
failed: '결제 실패',
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
warnBelow = 10;
log({
ko: '"refunded"처럼 정해 두지 않은 값은 글자 그대로, 빈 값은 비어 보여요.',
en: 'A value that is not defined, like "refunded", is shown as plain text, and an empty value shows nothing.',
});
}
export const actions = {
warn: (api: GridApi<Row>, value?: string) => {
warnBelow = Number(value ?? '10');
api.setColumns(columns());
say({
ko: `재고가 ${warnBelow}개보다 적으면 노란색으로 보여요.`,
en: `Stock below ${warnBelow} shows in yellow.`,
});
},
next: (api: GridApi<Row>) => {
const row = api.getRowByKey(1);
if (!row) return;
const after = order[(order.indexOf(row.status) + 1) % order.length] as string;
api.updateRows([{ ...row, status: after }]);
say({
ko: `1번 줄 상태가 바뀌었어요 → ${names[after]}`,
en: `Row 1 status changed → ${names[after]}`,
});
},
};버튼을 누르면 이 예제의 코드가 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.warn(api, ($event.target as HTMLSelectElement).value)"
>
<option value="5">5개 미만</option>
<option value="10" selected>10개 미만</option>
<option value="20">20개 미만</option>
</select>
<button type="button" :disabled="!api" @click="api && actions.next(api)">1번 줄 상태 바꾸기</button>
</div>
<Grid style="height: 280px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
order: string;
customer: string;
status: string;
stock: number;
}
// cellRenderer: 'badge'는 값을 색 배지로 그려요. map은 값마다 이름과 색(tone)을 정하고, map에 없는 값은 그대로 보여 줘요.
// tone에 함수를 주면 값에 따라 색을 골라요. 배지 뒤의 값은 그대로 글자·숫자라서 정렬·검색은 원래 값으로 해요.
const rows: Row[] = [
{ id: 1, order: 'SO-1001', customer: '한빛푸드', status: 'paid', stock: 42 },
{ id: 2, order: 'SO-1002', customer: '파란마켓', status: 'pending', stock: 4 },
{ id: 3, order: 'SO-1003', customer: '단풍상회', status: 'shipped', stock: 0 },
{ id: 4, order: 'SO-1004', customer: '바다유통', status: 'refunded', stock: 12 },
{ id: 5, order: 'SO-1005', customer: '새벽식자재', status: '', stock: 9 },
];
const order = ['paid', 'pending', 'shipped', 'failed'];
let warnBelow = 10;
const columns = (): NonNullable<GridOptions<Row>['columns']> => [
{ id: 'order', title: '주문', width: 100 },
{ id: 'customer', title: '거래처', width: 110 },
{
id: 'status',
title: '상태',
width: 110,
cellRenderer: 'badge',
cellRendererParams: {
map: {
paid: { label: '결제 완료', tone: 'success' },
pending: { label: '입금 대기', tone: 'warning' },
shipped: { label: '배송 중', tone: 'info' },
failed: { label: '결제 실패', tone: 'danger' },
},
},
},
{
id: 'stock',
title: '재고',
kind: 'number',
width: 120,
formatValue: (value) => (value === 0 ? '품절' : `${String(value)}개 남음`),
cellRenderer: 'badge',
cellRendererParams: {
tone: (value: unknown) =>
Number(value) === 0 ? 'danger' : Number(value) < warnBelow ? 'warning' : 'success',
},
},
];
export const options: GridOptions<Row> = {
columns: columns(),
rows,
rowKey: 'id',
saveMode: 'none',
};
let say: (message: LogText) => void = () => {};
const names: Record<string, string> = {
paid: '결제 완료',
pending: '입금 대기',
shipped: '배송 중',
failed: '결제 실패',
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
warnBelow = 10;
log({
ko: '"refunded"처럼 정해 두지 않은 값은 글자 그대로, 빈 값은 비어 보여요.',
en: 'A value that is not defined, like "refunded", is shown as plain text, and an empty value shows nothing.',
});
}
export const actions = {
warn: (api: GridApi<Row>, value?: string) => {
warnBelow = Number(value ?? '10');
api.setColumns(columns());
say({
ko: `재고가 ${warnBelow}개보다 적으면 노란색으로 보여요.`,
en: `Stock below ${warnBelow} shows in yellow.`,
});
},
next: (api: GridApi<Row>) => {
const row = api.getRowByKey(1);
if (!row) return;
const after = order[(order.indexOf(row.status) + 1) % order.length] as string;
api.updateRows([{ ...row, status: after }]);
say({
ko: `1번 줄 상태가 바뀌었어요 → ${names[after]}`,
en: `Row 1 status changed → ${names[after]}`,
});
},
};버튼을 누르면 이 예제의 코드가 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.warn(api, $any($event.target).value)"
>
<option value="5">5개 미만</option>
<option value="10" selected>10개 미만</option>
<option value="20">20개 미만</option>
</select>
<button
type="button"
[disabled]="!api"
(click)="api && actions.next(api)"
>1번 줄 상태 바꾸기</button>
</div>
<mx-grid style="height: 280px" [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 { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
order: string;
customer: string;
status: string;
stock: number;
}
// cellRenderer: 'badge'는 값을 색 배지로 그려요. map은 값마다 이름과 색(tone)을 정하고, map에 없는 값은 그대로 보여 줘요.
// tone에 함수를 주면 값에 따라 색을 골라요. 배지 뒤의 값은 그대로 글자·숫자라서 정렬·검색은 원래 값으로 해요.
const rows: Row[] = [
{ id: 1, order: 'SO-1001', customer: '한빛푸드', status: 'paid', stock: 42 },
{ id: 2, order: 'SO-1002', customer: '파란마켓', status: 'pending', stock: 4 },
{ id: 3, order: 'SO-1003', customer: '단풍상회', status: 'shipped', stock: 0 },
{ id: 4, order: 'SO-1004', customer: '바다유통', status: 'refunded', stock: 12 },
{ id: 5, order: 'SO-1005', customer: '새벽식자재', status: '', stock: 9 },
];
const order = ['paid', 'pending', 'shipped', 'failed'];
let warnBelow = 10;
const columns = (): NonNullable<GridOptions<Row>['columns']> => [
{ id: 'order', title: '주문', width: 100 },
{ id: 'customer', title: '거래처', width: 110 },
{
id: 'status',
title: '상태',
width: 110,
cellRenderer: 'badge',
cellRendererParams: {
map: {
paid: { label: '결제 완료', tone: 'success' },
pending: { label: '입금 대기', tone: 'warning' },
shipped: { label: '배송 중', tone: 'info' },
failed: { label: '결제 실패', tone: 'danger' },
},
},
},
{
id: 'stock',
title: '재고',
kind: 'number',
width: 120,
formatValue: (value) => (value === 0 ? '품절' : `${String(value)}개 남음`),
cellRenderer: 'badge',
cellRendererParams: {
tone: (value: unknown) =>
Number(value) === 0 ? 'danger' : Number(value) < warnBelow ? 'warning' : 'success',
},
},
];
export const options: GridOptions<Row> = {
columns: columns(),
rows,
rowKey: 'id',
saveMode: 'none',
};
let say: (message: LogText) => void = () => {};
const names: Record<string, string> = {
paid: '결제 완료',
pending: '입금 대기',
shipped: '배송 중',
failed: '결제 실패',
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
warnBelow = 10;
log({
ko: '"refunded"처럼 정해 두지 않은 값은 글자 그대로, 빈 값은 비어 보여요.',
en: 'A value that is not defined, like "refunded", is shown as plain text, and an empty value shows nothing.',
});
}
export const actions = {
warn: (api: GridApi<Row>, value?: string) => {
warnBelow = Number(value ?? '10');
api.setColumns(columns());
say({
ko: `재고가 ${warnBelow}개보다 적으면 노란색으로 보여요.`,
en: `Stock below ${warnBelow} shows in yellow.`,
});
},
next: (api: GridApi<Row>) => {
const row = api.getRowByKey(1);
if (!row) return;
const after = order[(order.indexOf(row.status) + 1) % order.length] as string;
api.updateRows([{ ...row, status: after }]);
say({
ko: `1번 줄 상태가 바뀌었어요 → ${names[after]}`,
en: `Row 1 status changed → ${names[after]}`,
});
},
};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="Yellow below" id="warn">
<option value="5">Below 5</option>
<option value="10" selected>Below 10</option>
<option value="20">Below 20</option>
</select>
<button type="button" id="next">Change status of row 1</button>
</div>
<div id="grid" style="height: 280px"></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('warn').addEventListener('change', (event) => actions.warn(api, event.target.value));
document.getElementById('next').addEventListener('click', () => actions.next(api));// cellRenderer: 'badge' draws the value as a colored badge. map sets a label and color (tone) per value; values not in map are shown as they are.
// If tone is a function, it picks the color from the value. The value behind the badge is still plain text/number, so sorting and search use the original value.
const rows = [
{ id: 1, order: 'SO-1001', customer: 'Harbor Foods', status: 'paid', stock: 42 },
{ id: 2, order: 'SO-1002', customer: 'Blue Market', status: 'pending', stock: 4 },
{ id: 3, order: 'SO-1003', customer: 'Maple Trading', status: 'shipped', stock: 0 },
{ id: 4, order: 'SO-1004', customer: 'Ocean Supply', status: 'refunded', stock: 12 },
{ id: 5, order: 'SO-1005', customer: 'Dawn Groceries', status: '', stock: 9 },
];
const order = ['paid', 'pending', 'shipped', 'failed'];
let warnBelow = 10;
const columns = () => [
{ id: 'order', title: 'Order', width: 100 },
{ id: 'customer', title: 'Customer', width: 130 },
{
id: 'status',
title: 'Status',
width: 110,
cellRenderer: 'badge',
cellRendererParams: {
map: {
paid: { label: 'Paid', tone: 'success' },
pending: { label: 'Awaiting payment', tone: 'warning' },
shipped: { label: 'Shipped', tone: 'info' },
failed: { label: 'Payment failed', tone: 'danger' },
},
},
},
{
id: 'stock',
title: 'Stock',
kind: 'number',
width: 120,
formatValue: (value) => (value === 0 ? 'Sold out' : `${String(value)} left`),
cellRenderer: 'badge',
cellRendererParams: {
tone: (value) => Number(value) === 0 ? 'danger' : Number(value) < warnBelow ? 'warning' : 'success',
},
},
];
export const options = {
columns: columns(),
rows,
rowKey: 'id',
saveMode: 'none',
};
let say = () => { };
const names = {
paid: 'Paid',
pending: 'Awaiting payment',
shipped: 'Shipped',
failed: 'Payment failed',
};
export function setup(_api, log) {
say = log;
warnBelow = 10;
log('A value that is not defined, like "refunded", is shown as plain text, and an empty value shows nothing.');
}
export const actions = {
warn: (api, value) => {
warnBelow = Number(value ?? '10');
api.setColumns(columns());
say(`Stock below ${warnBelow} shows in yellow.`);
},
next: (api) => {
const row = api.getRowByKey(1);
if (!row)
return;
const after = order[(order.indexOf(row.status) + 1) % order.length];
api.updateRows([{ ...row, status: after }]);
say(`Row 1 status changed → ${names[after]}`);
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Status badges: names and colors by value</title>
</head>
<body>
<div class="toolbar">
<select aria-label="Yellow below" id="warn">
<option value="5">Below 5</option>
<option value="10" selected>Below 10</option>
<option value="20">Below 20</option>
</select>
<button type="button" id="next">Change status of row 1</button>
</div>
<div id="grid" style="height: 280px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
// cellRenderer: 'badge' draws the value as a colored badge. map sets a label and color (tone) per value; values not in map are shown as they are.
// If tone is a function, it picks the color from the value. The value behind the badge is still plain text/number, so sorting and search use the original value.
const rows = [
{ id: 1, order: 'SO-1001', customer: 'Harbor Foods', status: 'paid', stock: 42 },
{ id: 2, order: 'SO-1002', customer: 'Blue Market', status: 'pending', stock: 4 },
{ id: 3, order: 'SO-1003', customer: 'Maple Trading', status: 'shipped', stock: 0 },
{ id: 4, order: 'SO-1004', customer: 'Ocean Supply', status: 'refunded', stock: 12 },
{ id: 5, order: 'SO-1005', customer: 'Dawn Groceries', status: '', stock: 9 },
];
const order = ['paid', 'pending', 'shipped', 'failed'];
let warnBelow = 10;
const columns = () => [
{ id: 'order', title: 'Order', width: 100 },
{ id: 'customer', title: 'Customer', width: 130 },
{
id: 'status',
title: 'Status',
width: 110,
cellRenderer: 'badge',
cellRendererParams: {
map: {
paid: { label: 'Paid', tone: 'success' },
pending: { label: 'Awaiting payment', tone: 'warning' },
shipped: { label: 'Shipped', tone: 'info' },
failed: { label: 'Payment failed', tone: 'danger' },
},
},
},
{
id: 'stock',
title: 'Stock',
kind: 'number',
width: 120,
formatValue: (value) => (value === 0 ? 'Sold out' : `${String(value)} left`),
cellRenderer: 'badge',
cellRendererParams: {
tone: (value) => Number(value) === 0 ? 'danger' : Number(value) < warnBelow ? 'warning' : 'success',
},
},
];
const options = {
columns: columns(),
rows,
rowKey: 'id',
saveMode: 'none',
};
let say = () => { };
const names = {
paid: 'Paid',
pending: 'Awaiting payment',
shipped: 'Shipped',
failed: 'Payment failed',
};
function setup(_api, log) {
say = log;
warnBelow = 10;
log('A value that is not defined, like "refunded", is shown as plain text, and an empty value shows nothing.');
}
const actions = {
warn: (api, value) => {
warnBelow = Number(value ?? '10');
api.setColumns(columns());
say(`Stock below ${warnBelow} shows in yellow.`);
},
next: (api) => {
const row = api.getRowByKey(1);
if (!row)
return;
const after = order[(order.indexOf(row.status) + 1) % order.length];
api.updateRows([{ ...row, status: after }]);
say(`Row 1 status changed → ${names[after]}`);
},
};
options.ariaLabel = "Status badges: names and colors by value";
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('warn').addEventListener('change', (event) => actions.warn(api, event.target.value));
document.getElementById('next').addEventListener('click', () => actions.next(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">
<select
aria-label="Yellow below"
disabled={!api}
onChange={(event) => api && actions.warn(api, event.target.value)}
>
<option value="5">Below 5</option>
<option value="10">Below 10</option>
<option value="20">Below 20</option>
</select>
<button
type="button"
disabled={!api}
onClick={() => api && actions.next(api)}
>Change status of row 1</button>
</div>
<Grid style={{ height: 280 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
order: string;
customer: string;
status: string;
stock: number;
}
// cellRenderer: 'badge' draws the value as a colored badge. map sets a label and color (tone) per value; values not in map are shown as they are.
// If tone is a function, it picks the color from the value. The value behind the badge is still plain text/number, so sorting and search use the original value.
const rows: Row[] = [
{ id: 1, order: 'SO-1001', customer: 'Harbor Foods', status: 'paid', stock: 42 },
{ id: 2, order: 'SO-1002', customer: 'Blue Market', status: 'pending', stock: 4 },
{ id: 3, order: 'SO-1003', customer: 'Maple Trading', status: 'shipped', stock: 0 },
{ id: 4, order: 'SO-1004', customer: 'Ocean Supply', status: 'refunded', stock: 12 },
{ id: 5, order: 'SO-1005', customer: 'Dawn Groceries', status: '', stock: 9 },
];
const order = ['paid', 'pending', 'shipped', 'failed'];
let warnBelow = 10;
const columns = (): NonNullable<GridOptions<Row>['columns']> => [
{ id: 'order', title: 'Order', width: 100 },
{ id: 'customer', title: 'Customer', width: 130 },
{
id: 'status',
title: 'Status',
width: 110,
cellRenderer: 'badge',
cellRendererParams: {
map: {
paid: { label: 'Paid', tone: 'success' },
pending: { label: 'Awaiting payment', tone: 'warning' },
shipped: { label: 'Shipped', tone: 'info' },
failed: { label: 'Payment failed', tone: 'danger' },
},
},
},
{
id: 'stock',
title: 'Stock',
kind: 'number',
width: 120,
formatValue: (value) => (value === 0 ? 'Sold out' : `${String(value)} left`),
cellRenderer: 'badge',
cellRendererParams: {
tone: (value: unknown) =>
Number(value) === 0 ? 'danger' : Number(value) < warnBelow ? 'warning' : 'success',
},
},
];
export const options: GridOptions<Row> = {
columns: columns(),
rows,
rowKey: 'id',
saveMode: 'none',
};
let say: (message: LogText) => void = () => {};
const names: Record<string, string> = {
paid: 'Paid',
pending: 'Awaiting payment',
shipped: 'Shipped',
failed: 'Payment failed',
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
warnBelow = 10;
log(
'A value that is not defined, like "refunded", is shown as plain text, and an empty value shows nothing.',
);
}
export const actions = {
warn: (api: GridApi<Row>, value?: string) => {
warnBelow = Number(value ?? '10');
api.setColumns(columns());
say(`Stock below ${warnBelow} shows in yellow.`);
},
next: (api: GridApi<Row>) => {
const row = api.getRowByKey(1);
if (!row) return;
const after = order[(order.indexOf(row.status) + 1) % order.length] as string;
api.updateRows([{ ...row, status: after }]);
say(`Row 1 status changed → ${names[after]}`);
},
};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="Yellow below"
:disabled="!api"
@change="api && actions.warn(api, ($event.target as HTMLSelectElement).value)"
>
<option value="5">Below 5</option>
<option value="10" selected>Below 10</option>
<option value="20">Below 20</option>
</select>
<button type="button" :disabled="!api" @click="api && actions.next(api)">Change status of row 1</button>
</div>
<Grid style="height: 280px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
order: string;
customer: string;
status: string;
stock: number;
}
// cellRenderer: 'badge' draws the value as a colored badge. map sets a label and color (tone) per value; values not in map are shown as they are.
// If tone is a function, it picks the color from the value. The value behind the badge is still plain text/number, so sorting and search use the original value.
const rows: Row[] = [
{ id: 1, order: 'SO-1001', customer: 'Harbor Foods', status: 'paid', stock: 42 },
{ id: 2, order: 'SO-1002', customer: 'Blue Market', status: 'pending', stock: 4 },
{ id: 3, order: 'SO-1003', customer: 'Maple Trading', status: 'shipped', stock: 0 },
{ id: 4, order: 'SO-1004', customer: 'Ocean Supply', status: 'refunded', stock: 12 },
{ id: 5, order: 'SO-1005', customer: 'Dawn Groceries', status: '', stock: 9 },
];
const order = ['paid', 'pending', 'shipped', 'failed'];
let warnBelow = 10;
const columns = (): NonNullable<GridOptions<Row>['columns']> => [
{ id: 'order', title: 'Order', width: 100 },
{ id: 'customer', title: 'Customer', width: 130 },
{
id: 'status',
title: 'Status',
width: 110,
cellRenderer: 'badge',
cellRendererParams: {
map: {
paid: { label: 'Paid', tone: 'success' },
pending: { label: 'Awaiting payment', tone: 'warning' },
shipped: { label: 'Shipped', tone: 'info' },
failed: { label: 'Payment failed', tone: 'danger' },
},
},
},
{
id: 'stock',
title: 'Stock',
kind: 'number',
width: 120,
formatValue: (value) => (value === 0 ? 'Sold out' : `${String(value)} left`),
cellRenderer: 'badge',
cellRendererParams: {
tone: (value: unknown) =>
Number(value) === 0 ? 'danger' : Number(value) < warnBelow ? 'warning' : 'success',
},
},
];
export const options: GridOptions<Row> = {
columns: columns(),
rows,
rowKey: 'id',
saveMode: 'none',
};
let say: (message: LogText) => void = () => {};
const names: Record<string, string> = {
paid: 'Paid',
pending: 'Awaiting payment',
shipped: 'Shipped',
failed: 'Payment failed',
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
warnBelow = 10;
log(
'A value that is not defined, like "refunded", is shown as plain text, and an empty value shows nothing.',
);
}
export const actions = {
warn: (api: GridApi<Row>, value?: string) => {
warnBelow = Number(value ?? '10');
api.setColumns(columns());
say(`Stock below ${warnBelow} shows in yellow.`);
},
next: (api: GridApi<Row>) => {
const row = api.getRowByKey(1);
if (!row) return;
const after = order[(order.indexOf(row.status) + 1) % order.length] as string;
api.updateRows([{ ...row, status: after }]);
say(`Row 1 status changed → ${names[after]}`);
},
};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="Yellow below"
[disabled]="!api"
(change)="api && actions.warn(api, $any($event.target).value)"
>
<option value="5">Below 5</option>
<option value="10" selected>Below 10</option>
<option value="20">Below 20</option>
</select>
<button
type="button"
[disabled]="!api"
(click)="api && actions.next(api)"
>Change status of row 1</button>
</div>
<mx-grid style="height: 280px" [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 { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string;
export interface Row {
id: number;
order: string;
customer: string;
status: string;
stock: number;
}
// cellRenderer: 'badge' draws the value as a colored badge. map sets a label and color (tone) per value; values not in map are shown as they are.
// If tone is a function, it picks the color from the value. The value behind the badge is still plain text/number, so sorting and search use the original value.
const rows: Row[] = [
{ id: 1, order: 'SO-1001', customer: 'Harbor Foods', status: 'paid', stock: 42 },
{ id: 2, order: 'SO-1002', customer: 'Blue Market', status: 'pending', stock: 4 },
{ id: 3, order: 'SO-1003', customer: 'Maple Trading', status: 'shipped', stock: 0 },
{ id: 4, order: 'SO-1004', customer: 'Ocean Supply', status: 'refunded', stock: 12 },
{ id: 5, order: 'SO-1005', customer: 'Dawn Groceries', status: '', stock: 9 },
];
const order = ['paid', 'pending', 'shipped', 'failed'];
let warnBelow = 10;
const columns = (): NonNullable<GridOptions<Row>['columns']> => [
{ id: 'order', title: 'Order', width: 100 },
{ id: 'customer', title: 'Customer', width: 130 },
{
id: 'status',
title: 'Status',
width: 110,
cellRenderer: 'badge',
cellRendererParams: {
map: {
paid: { label: 'Paid', tone: 'success' },
pending: { label: 'Awaiting payment', tone: 'warning' },
shipped: { label: 'Shipped', tone: 'info' },
failed: { label: 'Payment failed', tone: 'danger' },
},
},
},
{
id: 'stock',
title: 'Stock',
kind: 'number',
width: 120,
formatValue: (value) => (value === 0 ? 'Sold out' : `${String(value)} left`),
cellRenderer: 'badge',
cellRendererParams: {
tone: (value: unknown) =>
Number(value) === 0 ? 'danger' : Number(value) < warnBelow ? 'warning' : 'success',
},
},
];
export const options: GridOptions<Row> = {
columns: columns(),
rows,
rowKey: 'id',
saveMode: 'none',
};
let say: (message: LogText) => void = () => {};
const names: Record<string, string> = {
paid: 'Paid',
pending: 'Awaiting payment',
shipped: 'Shipped',
failed: 'Payment failed',
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
warnBelow = 10;
log(
'A value that is not defined, like "refunded", is shown as plain text, and an empty value shows nothing.',
);
}
export const actions = {
warn: (api: GridApi<Row>, value?: string) => {
warnBelow = Number(value ?? '10');
api.setColumns(columns());
say(`Stock below ${warnBelow} shows in yellow.`);
},
next: (api: GridApi<Row>) => {
const row = api.getRowByKey(1);
if (!row) return;
const after = order[(order.indexOf(row.status) + 1) % order.length] as string;
api.updateRows([{ ...row, status: after }]);
say(`Row 1 status changed → ${names[after]}`);
},
};