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

내장 배지와 직접 만든 칸 비교하기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.

활용Intermediate
직접 써 보기Live preview내장 배지와 직접 만든 칸 비교하기Built-in badge versus a custom cell

코드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="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>
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('mark').addEventListener('change', (event) => actions.mark(api, event.target.value));
example.js
// 칸 안의 내용은 기본으로 세로 가운데가 아니에요. .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 ★.',
    });
  },
};

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="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>
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('mark').addEventListener('change', (event) => actions.mark(api, event.target.value));
example.js
// 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 ★.');
  },
};