셀 안에 컴포넌트 그리기: 상태 뱃지와 버튼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.
실행 화면은 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)
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<div id="app">
<div id="grid" style="height: 380px"></div>
</div>
<script type="module" src="./main.js"></script>import { createGrid } from 'maxgrid';
import { options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);// 예제 틀이 알려 주는 페이지 언어(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 = () => { };
};
}<!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 id="grid" style="height: 380px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
// 예제 틀이 알려 주는 페이지 언어(ko/en)에 맞는 문구를 골라요.
const L = (ko, en) => document.documentElement.dataset.lang === 'en' ? en : ko;
/** 상태마다 쓰는 색. 컴포넌트가 이 색으로 뱃지를 칠해요. */
const TONE = {
진행: '#1a56c4',
완료: '#1a7f37',
지연: '#b42318',
};
const rows = [
{ id: 1, title: '견적서 보내기', owner: '김민준', status: '진행' },
{ id: 2, title: '계약서 검토', owner: '이서연', status: '지연' },
{ id: 3, title: '샘플 발송', owner: '박지호', status: '완료' },
{ id: 4, title: '재고 점검', owner: '최유나', status: '진행' },
];
/** 렌더러를 붙이기 전의 컬럼이에요. 프레임워크 탭에서는 상태·열기 컬럼에 컴포넌트를 붙여요. */
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) 안에 들어가고 다른 예제의 버튼과 같은 색·테두리를 써요. */
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() { },
});
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',
};
function setup(_api, log) {
say = log;
return () => {
say = () => { };
};
}
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);
});
</script>
</body>
</html>버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Grid, reactCell, type CellRenderProps, type GridOptions } from '@maxgrid/react';
import { useMemo, useState } from 'react';
import { OPEN_BUTTON_STYLE, TONE, baseColumns, rows, type Row } from './example';
// 버튼 모양은 예제 파일의 문자열을 CSS 객체로 바꿔 써요(줄 높이 안에 들어가는 작은 버튼).
const buttonStyle = Object.fromEntries(
OPEN_BUTTON_STYLE.split(';')
.filter((rule) => rule.trim())
.map((rule) => {
const [name, value] = rule.split(':').map((part) => part.trim());
return [(name ?? '').replace(/-([a-z])/g, (_, c: string) => c.toUpperCase()), value];
}),
);
// 셀에 그릴 컴포넌트예요. 표가 행마다 이 컴포넌트를 불러서 셀 안에 그려요.
function StatusBadge({ value }: CellRenderProps<Row>) {
const status = value as Row['status'];
return (
<span
style={{
padding: '1px 8px',
borderRadius: 10,
color: '#fff',
background: TONE[status],
fontSize: 12,
}}
>
{status}
</span>
);
}
export function ExampleGrid() {
const [opened, setOpened] = useState('');
// 버튼은 이 화면의 상태를 바꿔야 해서 안에서 만들어요. useMemo의 []로 한 번만 만들어야 해요.
// 렌더마다 새 컴포넌트를 만들면 모든 셀이 다시 만들어져요.
const options = useMemo<GridOptions<Row>>(
() => ({
columns: baseColumns.map((column) =>
column.id === 'status'
? { ...column, cellRenderer: reactCell(StatusBadge) }
: column.id === 'open'
? {
...column,
cellRenderer: reactCell(({ row }: CellRenderProps<Row>) => (
<div className="mxg-cell-center">
<button
type="button"
style={buttonStyle}
aria-label={`${row.title} 열기`}
onClick={() => setOpened(row.title)}
>
열기
</button>
</div>
)),
}
: column,
),
rows,
rowKey: 'id',
}),
[],
);
return (
<>
<div style={{ height: 220 }}>
<Grid<Row> options={options} />
</div>
<p>{opened ? `'${opened}' 업무를 열었어요` : '행의 [열기] 버튼을 눌러 보세요.'}</p>
</>
);
}import type { CellRenderContext, ColumnDef, GridApi, GridOptions } from '@maxgrid/react';
// 예제 틀이 알려 주는 페이지 언어(ko/en)에 맞는 문구를 골라요.
const L = (ko: string, en: string): string =>
document.documentElement.dataset.lang === 'en' ? en : ko;
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
title: string;
owner: string;
status: '진행' | '완료' | '지연';
}
/** 상태마다 쓰는 색. 컴포넌트가 이 색으로 뱃지를 칠해요. */
export const TONE: Record<Row['status'], string> = {
진행: '#1a56c4',
완료: '#1a7f37',
지연: '#b42318',
};
export const rows: Row[] = [
{ id: 1, title: '견적서 보내기', owner: '김민준', status: '진행' },
{ id: 2, title: '계약서 검토', owner: '이서연', status: '지연' },
{ id: 3, title: '샘플 발송', owner: '박지호', status: '완료' },
{ id: 4, title: '재고 점검', owner: '최유나', status: '진행' },
];
/** 렌더러를 붙이기 전의 컬럼이에요. 프레임워크 탭에서는 상태·열기 컬럼에 컴포넌트를 붙여요. */
export const baseColumns: ColumnDef<Row>[] = [
{ 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: (message: LogText) => void = () => {};
// 이 화면(JavaScript 버전)은 내장 뱃지와, 버튼을 직접 만드는 어댑터를 써요.
const openButton = () => ({
mount(el: HTMLElement, ctx: CellRenderContext<Row>) {
// 칸 안의 내용은 기본으로 세로 가운데가 아니에요. .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: GridOptions<Row> = {
columns: baseColumns.map((column) =>
column.id === 'status'
? {
...column,
cellRenderer: 'badge' as const,
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: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {
say = () => {};
};
}버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<script setup lang="ts">
import { Grid, vueCell, type GridOptions } from '@maxgrid/vue';
import { defineComponent, h, ref, type PropType } from 'vue';
import { OPEN_BUTTON_STYLE, TONE, baseColumns, rows, type Row } from './example';
// 셀에 그릴 컴포넌트는 value, row, rowKey, rowIndex, columnId, text, kind를 props로 받아요.
const StatusBadge = defineComponent({
props: { value: { type: String, default: '' } },
setup(props) {
return () =>
h(
'span',
{
style: {
padding: '1px 8px',
borderRadius: '10px',
fontSize: '12px',
color: '#fff',
background: TONE[props.value as Row['status']] ?? '#666',
},
},
props.value,
);
},
});
// 두 번째 인자의 props는 항상 같이 넘어가요. 여기서는 클릭을 알려 줄 함수를 넘겨요.
const OpenButton = defineComponent({
props: {
row: { type: Object as PropType<Row>, required: true },
onOpen: { type: Function as PropType<(row: Row) => void>, required: true },
},
setup(props) {
return () =>
h('div', { class: 'mxg-cell-center' }, [
h(
'button',
{
type: 'button',
style: OPEN_BUTTON_STYLE,
'aria-label': `${props.row.title} 열기`,
onClick: () => props.onOpen(props.row),
},
'열기',
),
]);
},
});
const opened = ref('');
const options: GridOptions<Row> = {
columns: baseColumns.map((column) =>
column.id === 'status'
? { ...column, cellRenderer: vueCell<Row>(StatusBadge) }
: column.id === 'open'
? {
...column,
cellRenderer: vueCell<Row>(OpenButton, {
props: { onOpen: (row: Row) => (opened.value = row.title) },
}),
}
: column,
),
rows,
rowKey: 'id',
};
</script>
<template>
<Grid style="height: 220px" :options="options" />
<p>{{ opened ? `'${opened}' 업무를 열었어요` : '행의 [열기] 버튼을 눌러 보세요.' }}</p>
</template>import type { CellRenderContext, ColumnDef, GridApi, GridOptions } from '@maxgrid/vue';
// 예제 틀이 알려 주는 페이지 언어(ko/en)에 맞는 문구를 골라요.
const L = (ko: string, en: string): string =>
document.documentElement.dataset.lang === 'en' ? en : ko;
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
title: string;
owner: string;
status: '진행' | '완료' | '지연';
}
/** 상태마다 쓰는 색. 컴포넌트가 이 색으로 뱃지를 칠해요. */
export const TONE: Record<Row['status'], string> = {
진행: '#1a56c4',
완료: '#1a7f37',
지연: '#b42318',
};
export const rows: Row[] = [
{ id: 1, title: '견적서 보내기', owner: '김민준', status: '진행' },
{ id: 2, title: '계약서 검토', owner: '이서연', status: '지연' },
{ id: 3, title: '샘플 발송', owner: '박지호', status: '완료' },
{ id: 4, title: '재고 점검', owner: '최유나', status: '진행' },
];
/** 렌더러를 붙이기 전의 컬럼이에요. 프레임워크 탭에서는 상태·열기 컬럼에 컴포넌트를 붙여요. */
export const baseColumns: ColumnDef<Row>[] = [
{ 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: (message: LogText) => void = () => {};
// 이 화면(JavaScript 버전)은 내장 뱃지와, 버튼을 직접 만드는 어댑터를 써요.
const openButton = () => ({
mount(el: HTMLElement, ctx: CellRenderContext<Row>) {
// 칸 안의 내용은 기본으로 세로 가운데가 아니에요. .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: GridOptions<Row> = {
columns: baseColumns.map((column) =>
column.id === 'status'
? {
...column,
cellRenderer: 'badge' as const,
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: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {
say = () => {};
};
}버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Component, EventEmitter, Input, Output, signal } from '@angular/core';
import { GridComponent, angularCell, type GridOptions } from '@maxgrid/angular';
import { OPEN_BUTTON_STYLE, TONE, baseColumns, rows, type Row } from './example';
// 셀에 그릴 컴포넌트예요. 입력 이름이 value, row 같은 셀 정보와 같으면 표가 알아서 채워 줘요.
@Component({
selector: 'app-status-badge',
standalone: true,
template: `<span
[style.background]="tone()"
style="padding: 1px 8px; border-radius: 10px; color: #fff; font-size: 12px"
>{{ value }}</span
>`,
})
export class StatusBadgeComponent {
@Input() value = '';
tone = () => TONE[this.value as Row['status']] ?? '#666';
}
// 출력(@Output)은 아래 outputs로 이 화면에 이어요.
@Component({
selector: 'app-open-button',
standalone: true,
template: `<div class="mxg-cell-center">
<button
type="button"
[style]="buttonStyle"
[attr.aria-label]="row.title + ' 열기'"
(click)="open.emit(row)"
>
열기
</button>
</div>`,
})
export class OpenButtonComponent {
@Input() row!: Row;
@Output() open = new EventEmitter<Row>();
readonly buttonStyle = OPEN_BUTTON_STYLE;
}
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<mx-grid style="height: 220px" [options]="options" />
<p>{{ opened() ? "'" + opened() + "' 업무를 열었어요" : '행의 [열기] 버튼을 눌러 보세요.' }}</p>
`,
})
export class ExampleGridComponent {
readonly opened = signal('');
readonly options: GridOptions<Row> = {
columns: baseColumns.map((column) =>
column.id === 'status'
? { ...column, cellRenderer: angularCell<Row>(StatusBadgeComponent) }
: column.id === 'open'
? {
...column,
cellRenderer: angularCell<Row>(OpenButtonComponent, {
outputs: { open: (row) => this.opened.set((row as Row).title) },
}),
}
: column,
),
rows,
rowKey: 'id',
};
}import type { CellRenderContext, ColumnDef, GridApi, GridOptions } from '@maxgrid/angular';
// 예제 틀이 알려 주는 페이지 언어(ko/en)에 맞는 문구를 골라요.
const L = (ko: string, en: string): string =>
document.documentElement.dataset.lang === 'en' ? en : ko;
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
title: string;
owner: string;
status: '진행' | '완료' | '지연';
}
/** 상태마다 쓰는 색. 컴포넌트가 이 색으로 뱃지를 칠해요. */
export const TONE: Record<Row['status'], string> = {
진행: '#1a56c4',
완료: '#1a7f37',
지연: '#b42318',
};
export const rows: Row[] = [
{ id: 1, title: '견적서 보내기', owner: '김민준', status: '진행' },
{ id: 2, title: '계약서 검토', owner: '이서연', status: '지연' },
{ id: 3, title: '샘플 발송', owner: '박지호', status: '완료' },
{ id: 4, title: '재고 점검', owner: '최유나', status: '진행' },
];
/** 렌더러를 붙이기 전의 컬럼이에요. 프레임워크 탭에서는 상태·열기 컬럼에 컴포넌트를 붙여요. */
export const baseColumns: ColumnDef<Row>[] = [
{ 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: (message: LogText) => void = () => {};
// 이 화면(JavaScript 버전)은 내장 뱃지와, 버튼을 직접 만드는 어댑터를 써요.
const openButton = () => ({
mount(el: HTMLElement, ctx: CellRenderContext<Row>) {
// 칸 안의 내용은 기본으로 세로 가운데가 아니에요. .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: GridOptions<Row> = {
columns: baseColumns.map((column) =>
column.id === 'status'
? {
...column,
cellRenderer: 'badge' as const,
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: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {
say = () => {};
};
}When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<div id="app">
<div id="grid" style="height: 380px"></div>
</div>
<script type="module" src="./main.js"></script>import { createGrid } from 'maxgrid';
import { options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);/** 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 = () => { };
};
}<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Draw components inside cells: a status badge and a button</title>
</head>
<body>
<div id="grid" style="height: 380px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
/** The color used for each status. The component paints the badge with this color. */
const TONE = {
Active: '#1a56c4',
Done: '#1a7f37',
Late: '#b42318',
};
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. */
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. */
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() { },
});
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',
};
function setup(_api, log) {
say = log;
return () => {
say = () => { };
};
}
options.ariaLabel = "Draw components inside cells: a status badge and a button";
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);
});
</script>
</body>
</html>When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Grid, reactCell, type CellRenderProps, type GridOptions } from '@maxgrid/react';
import { useMemo, useState } from 'react';
import { OPEN_BUTTON_STYLE, TONE, baseColumns, rows, type Row } from './example';
// 버튼 모양은 예제 파일의 문자열을 CSS 객체로 바꿔 써요(줄 높이 안에 들어가는 작은 버튼).
const buttonStyle = Object.fromEntries(
OPEN_BUTTON_STYLE.split(';')
.filter((rule) => rule.trim())
.map((rule) => {
const [name, value] = rule.split(':').map((part) => part.trim());
return [(name ?? '').replace(/-([a-z])/g, (_, c: string) => c.toUpperCase()), value];
}),
);
// 셀에 그릴 컴포넌트예요. 표가 행마다 이 컴포넌트를 불러서 셀 안에 그려요.
function StatusBadge({ value }: CellRenderProps<Row>) {
const status = value as Row['status'];
return (
<span
style={{
padding: '1px 8px',
borderRadius: 10,
color: '#fff',
background: TONE[status],
fontSize: 12,
}}
>
{status}
</span>
);
}
export function ExampleGrid() {
const [opened, setOpened] = useState('');
// 버튼은 이 화면의 상태를 바꿔야 해서 안에서 만들어요. useMemo의 []로 한 번만 만들어야 해요.
// 렌더마다 새 컴포넌트를 만들면 모든 셀이 다시 만들어져요.
const options = useMemo<GridOptions<Row>>(
() => ({
columns: baseColumns.map((column) =>
column.id === 'status'
? { ...column, cellRenderer: reactCell(StatusBadge) }
: column.id === 'open'
? {
...column,
cellRenderer: reactCell(({ row }: CellRenderProps<Row>) => (
<div className="mxg-cell-center">
<button
type="button"
style={buttonStyle}
aria-label={`${row.title} 열기`}
onClick={() => setOpened(row.title)}
>
열기
</button>
</div>
)),
}
: column,
),
rows,
rowKey: 'id',
}),
[],
);
return (
<>
<div style={{ height: 220 }}>
<Grid<Row> options={options} />
</div>
<p>{opened ? `'${opened}' 업무를 열었어요` : '행의 [열기] 버튼을 눌러 보세요.'}</p>
</>
);
}import type { CellRenderContext, ColumnDef, GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
title: string;
owner: string;
status: 'Active' | 'Done' | 'Late';
}
/** The color used for each status. The component paints the badge with this color. */
export const TONE: Record<Row['status'], string> = {
Active: '#1a56c4',
Done: '#1a7f37',
Late: '#b42318',
};
export const rows: Row[] = [
{ 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: ColumnDef<Row>[] = [
{ 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: (message: LogText) => void = () => {};
// This screen (JavaScript version) uses the built-in badge and an adapter that builds the button by hand.
const openButton = () => ({
mount(el: HTMLElement, ctx: CellRenderContext<Row>) {
// 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: GridOptions<Row> = {
columns: baseColumns.map((column) =>
column.id === 'status'
? {
...column,
cellRenderer: 'badge' as const,
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: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {
say = () => {};
};
}When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<script setup lang="ts">
import { Grid, vueCell, type GridOptions } from '@maxgrid/vue';
import { defineComponent, h, ref, type PropType } from 'vue';
import { OPEN_BUTTON_STYLE, TONE, baseColumns, rows, type Row } from './example';
// 셀에 그릴 컴포넌트는 value, row, rowKey, rowIndex, columnId, text, kind를 props로 받아요.
const StatusBadge = defineComponent({
props: { value: { type: String, default: '' } },
setup(props) {
return () =>
h(
'span',
{
style: {
padding: '1px 8px',
borderRadius: '10px',
fontSize: '12px',
color: '#fff',
background: TONE[props.value as Row['status']] ?? '#666',
},
},
props.value,
);
},
});
// 두 번째 인자의 props는 항상 같이 넘어가요. 여기서는 클릭을 알려 줄 함수를 넘겨요.
const OpenButton = defineComponent({
props: {
row: { type: Object as PropType<Row>, required: true },
onOpen: { type: Function as PropType<(row: Row) => void>, required: true },
},
setup(props) {
return () =>
h('div', { class: 'mxg-cell-center' }, [
h(
'button',
{
type: 'button',
style: OPEN_BUTTON_STYLE,
'aria-label': `${props.row.title} 열기`,
onClick: () => props.onOpen(props.row),
},
'열기',
),
]);
},
});
const opened = ref('');
const options: GridOptions<Row> = {
columns: baseColumns.map((column) =>
column.id === 'status'
? { ...column, cellRenderer: vueCell<Row>(StatusBadge) }
: column.id === 'open'
? {
...column,
cellRenderer: vueCell<Row>(OpenButton, {
props: { onOpen: (row: Row) => (opened.value = row.title) },
}),
}
: column,
),
rows,
rowKey: 'id',
};
</script>
<template>
<Grid style="height: 220px" :options="options" />
<p>{{ opened ? `'${opened}' 업무를 열었어요` : '행의 [열기] 버튼을 눌러 보세요.' }}</p>
</template>import type { CellRenderContext, ColumnDef, GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
title: string;
owner: string;
status: 'Active' | 'Done' | 'Late';
}
/** The color used for each status. The component paints the badge with this color. */
export const TONE: Record<Row['status'], string> = {
Active: '#1a56c4',
Done: '#1a7f37',
Late: '#b42318',
};
export const rows: Row[] = [
{ 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: ColumnDef<Row>[] = [
{ 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: (message: LogText) => void = () => {};
// This screen (JavaScript version) uses the built-in badge and an adapter that builds the button by hand.
const openButton = () => ({
mount(el: HTMLElement, ctx: CellRenderContext<Row>) {
// 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: GridOptions<Row> = {
columns: baseColumns.map((column) =>
column.id === 'status'
? {
...column,
cellRenderer: 'badge' as const,
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: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {
say = () => {};
};
}When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Component, EventEmitter, Input, Output, signal } from '@angular/core';
import { GridComponent, angularCell, type GridOptions } from '@maxgrid/angular';
import { OPEN_BUTTON_STYLE, TONE, baseColumns, rows, type Row } from './example';
// 셀에 그릴 컴포넌트예요. 입력 이름이 value, row 같은 셀 정보와 같으면 표가 알아서 채워 줘요.
@Component({
selector: 'app-status-badge',
standalone: true,
template: `<span
[style.background]="tone()"
style="padding: 1px 8px; border-radius: 10px; color: #fff; font-size: 12px"
>{{ value }}</span
>`,
})
export class StatusBadgeComponent {
@Input() value = '';
tone = () => TONE[this.value as Row['status']] ?? '#666';
}
// 출력(@Output)은 아래 outputs로 이 화면에 이어요.
@Component({
selector: 'app-open-button',
standalone: true,
template: `<div class="mxg-cell-center">
<button
type="button"
[style]="buttonStyle"
[attr.aria-label]="row.title + ' 열기'"
(click)="open.emit(row)"
>
열기
</button>
</div>`,
})
export class OpenButtonComponent {
@Input() row!: Row;
@Output() open = new EventEmitter<Row>();
readonly buttonStyle = OPEN_BUTTON_STYLE;
}
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<mx-grid style="height: 220px" [options]="options" />
<p>{{ opened() ? "'" + opened() + "' 업무를 열었어요" : '행의 [열기] 버튼을 눌러 보세요.' }}</p>
`,
})
export class ExampleGridComponent {
readonly opened = signal('');
readonly options: GridOptions<Row> = {
columns: baseColumns.map((column) =>
column.id === 'status'
? { ...column, cellRenderer: angularCell<Row>(StatusBadgeComponent) }
: column.id === 'open'
? {
...column,
cellRenderer: angularCell<Row>(OpenButtonComponent, {
outputs: { open: (row) => this.opened.set((row as Row).title) },
}),
}
: column,
),
rows,
rowKey: 'id',
};
}import type { CellRenderContext, ColumnDef, GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string;
export interface Row {
id: number;
title: string;
owner: string;
status: 'Active' | 'Done' | 'Late';
}
/** The color used for each status. The component paints the badge with this color. */
export const TONE: Record<Row['status'], string> = {
Active: '#1a56c4',
Done: '#1a7f37',
Late: '#b42318',
};
export const rows: Row[] = [
{ 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: ColumnDef<Row>[] = [
{ 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: (message: LogText) => void = () => {};
// This screen (JavaScript version) uses the built-in badge and an adapter that builds the button by hand.
const openButton = () => ({
mount(el: HTMLElement, ctx: CellRenderContext<Row>) {
// 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: GridOptions<Row> = {
columns: baseColumns.map((column) =>
column.id === 'status'
? {
...column,
cellRenderer: 'badge' as const,
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: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {
say = () => {};
};
}