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

셀 안에 컴포넌트 그리기: 상태 뱃지와 버튼Draw components inside cells: a status badge and a button

프레임워크의 컴포넌트를 셀에 그릴 수 있어요. 상태 칸에는 뱃지를, 마지막 칸에는 버튼을 넣고, 버튼을 누르면 화면에 알려 줘요. React, Vue, Angular 탭에는 각 프레임워크의 컴포넌트로 쓴 같은 예제가 들어 있고, 위 화면은 같은 모양을 JavaScript로 보여 줘요.A framework component can be drawn inside a cell. The status column holds a badge, the last column a button that reports its click to the screen. The React, Vue and Angular tabs hold the same example as each framework's components; the preview shows the same look in JavaScript.

심화Advanced
직접 써 보기Live preview셀 안에 컴포넌트 그리기: 상태 뱃지와 버튼Draw components inside cells: a status badge and a button

실행 화면은 JavaScript 버전이에요. 아래 탭의 React·Vue·Angular 코드는 같은 예제를 각 프레임워크로 직접 쓴 코드예요.The preview runs the JavaScript version. The React, Vue, Angular tabs below hold the same example written for each framework.

코드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 id="grid" style="height: 380px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
example.js
// 예제 틀이 알려 주는 페이지 언어(ko/en)에 맞는 문구를 골라요.
const L = (ko, en) => document.documentElement.dataset.lang === 'en' ? en : ko;
/** 상태마다 쓰는 색. 컴포넌트가 이 색으로 뱃지를 칠해요. */
export const TONE = {
  진행: '#1a56c4',
  완료: '#1a7f37',
  지연: '#b42318',
};
export const rows = [
  { id: 1, title: '견적서 보내기', owner: '김민준', status: '진행' },
  { id: 2, title: '계약서 검토', owner: '이서연', status: '지연' },
  { id: 3, title: '샘플 발송', owner: '박지호', status: '완료' },
  { id: 4, title: '재고 점검', owner: '최유나', status: '진행' },
];
/** 렌더러를 붙이기 전의 컬럼이에요. 프레임워크 탭에서는 상태·열기 컬럼에 컴포넌트를 붙여요. */
export const baseColumns = [
  { id: 'title', title: '업무', width: 170 },
  { id: 'owner', title: '담당', width: 100 },
  { id: 'status', title: '상태', width: 100 },
  { id: 'open', title: '작업', width: 90, sortable: false },
];
/** 작은 버튼 모양. 줄 높이(32px) 안에 들어가고 다른 예제의 버튼과 같은 색·테두리를 써요. */
export const OPEN_BUTTON_STYLE = 'padding: 0 10px; height: 22px; font: inherit; font-size: 12px; border: 1px solid var(--mxg-border); border-radius: 4px; background: var(--mxg-bg); color: var(--mxg-fg); cursor: pointer;';
let say = () => { };
// 이 화면(JavaScript 버전)은 내장 뱃지와, 버튼을 직접 만드는 어댑터를 써요.
const openButton = () => ({
  mount(el, ctx) {
    // 칸 안의 내용은 기본으로 세로 가운데가 아니에요. .mxg-cell-center 상자에 넣으면 가운데에 와요.
    const box = document.createElement('div');
    box.className = 'mxg-cell-center';
    el.append(box);
    const button = document.createElement('button');
    button.type = 'button';
    button.textContent = L('열기', 'Open');
    button.style.cssText = OPEN_BUTTON_STYLE;
    button.setAttribute('aria-label', L(`${ctx.row.title} 열기`, `Open ${ctx.row.title}`)); // 읽어 주는 이름에 어느 행인지 넣어요
    button.addEventListener('click', () => say({ ko: `'${ctx.row.title}' 업무를 열었어요`, en: `Opened task '${ctx.row.title}'` }));
    box.append(button);
  },
  update() { },
  destroy() { },
});
export const options = {
  columns: baseColumns.map((column) => column.id === 'status'
    ? {
      ...column,
      cellRenderer: 'badge',
      cellRendererParams: {
        map: {
          진행: { label: '진행', tone: 'info' },
          완료: { label: '완료', tone: 'success' },
          지연: { label: '지연', tone: 'danger' },
        },
      },
    }
    : column.id === 'open'
      ? { ...column, cellRenderer: openButton }
      : column),
  rows,
  rowKey: 'id',
};
export function setup(_api, log) {
  say = log;
  return () => {
    say = () => { };
  };
}

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 id="grid" style="height: 380px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
example.js
/** The color used for each status. The component paints the badge with this color. */
export const TONE = {
  Active: '#1a56c4',
  Done: '#1a7f37',
  Late: '#b42318',
};
export const rows = [
  { id: 1, title: 'Send the quote', owner: 'Liam Carter', status: 'Active' },
  { id: 2, title: 'Review the contract', owner: 'Emma Stone', status: 'Late' },
  { id: 3, title: 'Ship samples', owner: 'Noah Reed', status: 'Done' },
  { id: 4, title: 'Check inventory', owner: 'Olivia Hayes', status: 'Active' },
];
/** Columns before the renderer is attached. In the framework tabs components are attached to the status and open columns. */
export const baseColumns = [
  { id: 'title', title: 'Task', width: 170 },
  { id: 'owner', title: 'Owner', width: 110 },
  { id: 'status', title: 'Status', width: 100 },
  { id: 'open', title: 'Action', width: 90, sortable: false },
];
/** A small button look. It fits in the row height (32px) and uses the same color and border as the buttons in other examples. */
export const OPEN_BUTTON_STYLE = 'padding: 0 10px; height: 22px; font: inherit; font-size: 12px; border: 1px solid var(--mxg-border); border-radius: 4px; background: var(--mxg-bg); color: var(--mxg-fg); cursor: pointer;';
let say = () => { };
// This screen (JavaScript version) uses the built-in badge and an adapter that builds the button by hand.
const openButton = () => ({
  mount(el, ctx) {
    // Content inside a cell is not vertically centered by default. Put it in a .mxg-cell-center box to center it.
    const box = document.createElement('div');
    box.className = 'mxg-cell-center';
    el.append(box);
    const button = document.createElement('button');
    button.type = 'button';
    button.textContent = 'Open';
    button.style.cssText = OPEN_BUTTON_STYLE;
    button.setAttribute('aria-label', `Open ${ctx.row.title}`); // put the row in the name that screen readers read
    button.addEventListener('click', () => say(`Opened task '${ctx.row.title}'`));
    box.append(button);
  },
  update() { },
  destroy() { },
});
export const options = {
  columns: baseColumns.map((column) => column.id === 'status'
    ? {
      ...column,
      cellRenderer: 'badge',
      cellRendererParams: {
        map: {
          Active: { label: 'Active', tone: 'info' },
          Done: { label: 'Done', tone: 'success' },
          Late: { label: 'Late', tone: 'danger' },
        },
      },
    }
    : column.id === 'open'
      ? { ...column, cellRenderer: openButton }
      : column),
  rows,
  rowKey: 'id',
};
export function setup(_api, log) {
  say = log;
  return () => {
    say = () => { };
  };
}