본문으로 건너뛰기Skip to content
예제 첫화면Examples home

상태 배지: 값에 따라 이름과 색 정하기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.

입문Beginner
직접 써 보기Live preview상태 배지: 값에 따라 이름과 색 정하기Status badges: names and colors by value

코드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)

빌드 도구(Vite 등)를 쓰는 프로젝트용 모듈 파일이에요. 설치 없이 바로 돌려 보려면 HTML 탭이나 직접 해보기를 쓰세요.

버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.

index.html
<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>
main.js
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));
example.js
// 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]}`,
    });
  },
};

Module files for a project with a bundler such as Vite. To run it with no setup, use the HTML tab or Try it.

When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.

index.html
<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>
main.js
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));
example.js
// 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]}`);
  },
};