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

아이콘: 값에 따라 바꾸기Icons: switch by value

cellRenderer 'icon'은 값에 맞는 글자·이모지·SVG 아이콘을 그려요. map으로 값과 아이콘을 짝지우고, position으로 셀 글자 앞·뒤에 놓고, tone으로 색을 정해요. 허용되지 않는 SVG는 그리지 않아요.cellRenderer 'icon' draws a text, emoji or SVG icon that fits the value. map pairs values with icons, position puts it before or after the cell text, and tone sets the color. SVG that is not allowed is not drawn.

입문Beginner
직접 써 보기Live preview아이콘: 값에 따라 바꾸기Icons: switch 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="position">
      <option value="before" selected>글자 앞</option>
      <option value="after">글자 뒤</option>
      <option value="only">아이콘만</option>
    </select>
  </div>
  <div id="grid" style="height: 200px"></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('position').addEventListener('change', (event) => actions.position(api, event.target.value));
example.js
const rows = [
  { id: 1, task: '배포 점검', status: 'ok', level: 'low' },
  { id: 2, task: '백업 확인', status: 'warn', level: 'mid' },
  { id: 3, task: '로그 정리', status: 'fail', level: 'high' },
  { id: 4, task: '알 수 없는 값', status: 'unknown', level: 'bad' },
];
// icon의 글자·이모지는 4자 이하, SVG는 허용 목록(path·circle 등)만 받아요. 스크립트나 이벤트 속성이 있으면 그리지 않고 error 이벤트로 알려요.
const CHECK = '<svg viewBox="0 0 16 16"><path d="M3 8.5l3.2 3.2L13 5" fill="none" stroke="currentColor" stroke-width="2"/></svg>';
const DOT = (c) => `<svg viewBox="0 0 16 16"><circle cx="8" cy="8" r="6" fill="${c}"/></svg>`;
const columns = (position) => [
  { id: 'task', title: '작업', width: 150 },
  {
    id: 'status',
    title: '상태',
    width: 150,
    cellRenderer: 'icon',
    cellRendererParams: {
      map: { ok: '✔', warn: '⚠', fail: '✘' },
      position,
      tone: 'info',
      label: (c) => `상태: ${String(c.value)}`,
    },
  },
  {
    id: 'level',
    title: '표시',
    width: 150,
    cellRenderer: 'icon',
    cellRendererParams: {
      map: { low: CHECK, mid: DOT('currentColor'), high: DOT('#b42318') },
      position,
      tone: 'success',
      size: 18,
    },
  },
];
export const options = { columns: columns('before'), rows, rowKey: 'id' };
export function setup(_api, log) {
  log({
    ko: '"배치"를 바꿔 아이콘의 위치를 비교해 보세요. 알 수 없는 값(마지막 줄)은 비어 있어요.',
    en: 'Change "Position" to compare where the icon sits. An unknown value (the last row) stays empty.',
  });
}
export const actions = {
  position: (api, value) => api.setColumns(columns(value === 'after' ? 'after' : value === 'only' ? 'only' : 'before')),
};

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="Position" id="position">
      <option value="before" selected>Before text</option>
      <option value="after">After text</option>
      <option value="only">Icon only</option>
    </select>
  </div>
  <div id="grid" style="height: 200px"></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('position').addEventListener('change', (event) => actions.position(api, event.target.value));
example.js
const rows = [
  { id: 1, task: 'Deploy check', status: 'ok', level: 'low' },
  { id: 2, task: 'Verify backup', status: 'warn', level: 'mid' },
  { id: 3, task: 'Clean up logs', status: 'fail', level: 'high' },
  { id: 4, task: 'Unknown value', status: 'unknown', level: 'bad' },
];
// Icon text/emoji is at most 4 characters; SVG accepts only an allow list (path, circle, ...). Scripts or event attributes are not drawn and are reported through the error event.
const CHECK = '<svg viewBox="0 0 16 16"><path d="M3 8.5l3.2 3.2L13 5" fill="none" stroke="currentColor" stroke-width="2"/></svg>';
const DOT = (c) => `<svg viewBox="0 0 16 16"><circle cx="8" cy="8" r="6" fill="${c}"/></svg>`;
const columns = (position) => [
  { id: 'task', title: 'Task', width: 150 },
  {
    id: 'status',
    title: 'Status',
    width: 150,
    cellRenderer: 'icon',
    cellRendererParams: {
      map: { ok: '✔', warn: '⚠', fail: '✘' },
      position,
      tone: 'info',
      label: (c) => `Status: ${String(c.value)}`,
    },
  },
  {
    id: 'level',
    title: 'Indicator',
    width: 150,
    cellRenderer: 'icon',
    cellRendererParams: {
      map: { low: CHECK, mid: DOT('currentColor'), high: DOT('#b42318') },
      position,
      tone: 'success',
      size: 18,
    },
  },
];
export const options = { columns: columns('before'), rows, rowKey: 'id' };
export function setup(_api, log) {
  log('Change "Position" to compare where the icon sits. An unknown value (the last row) stays empty.');
}
export const actions = {
  position: (api, value) => api.setColumns(columns(value === 'after' ? 'after' : value === 'only' ? 'only' : 'before')),
};