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

행·칸에 조건부 스타일 주기Conditional styles on rows and cells

rowStyle, rowClassRules, cellStyle, cellClassRules, headerStyle로 값에 따라 행과 칸의 모양을 바꾸고, rowTestId로 테스트용 이름을 달아요.Change how rows and cells look by value with rowStyle, rowClassRules, cellStyle, cellClassRules and headerStyle, and tag rows for tests with rowTestId.

입문Beginner
직접 써 보기Live preview행·칸에 조건부 스타일 주기Conditional styles on rows and cells

코드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);
example.js
const items = ['연필', '노트', '지우개', '가위', '풀', '자', '필통', '스테이플러'];
export const options = {
  columns: [
    { id: 'item', title: '품목', width: 130, headerStyle: { fontWeight: 700 } },
    {
      id: 'price',
      title: '가격',
      width: 110,
      kind: 'number',
      cellStyle: (ctx) => (Number(ctx.value) >= 5000 ? { fontWeight: 700 } : null),
    },
    {
      id: 'stock',
      title: '재고',
      width: 100,
      kind: 'number',
      cellClassRules: { 'cell-low': (ctx) => Number(ctx.value) <= 5 },
    },
    { id: 'note', title: '비고', width: 140 },
  ],
  rows: Array.from({ length: 200 }, (_, i) => {
    const stock = (i * 7) % 23;
    return {
      id: i + 1,
      item: `${items[i % items.length]} ${i + 1}`,
      price: 500 + ((i * 337) % 9500),
      stock,
      note: stock === 0 ? '품절' : '',
    };
  }),
  rowKey: 'id',
  striped: false,
  // 재고가 적은 행의 배경. 값이 바뀌거나 스크롤로 줄이 재사용돼도 값을 따라가요.
  rowStyle: (row) => (row.stock <= 5 ? { backgroundColor: 'rgba(255, 200, 0, 0.18)' } : null),
  rowClassRules: { low: (row) => row.stock === 0 },
  rowTestId: (row, key) => `item-${String(key)}`,
};
const CSS = `
.mxg .cell-low { color: var(--mxg-tone-danger-fg, #b42318); font-weight: 700; }
.mxg .mxg-row.low { font-style: italic; }
`;
// 예제 전용 CSS. 실제 앱에서는 앱 스타일시트에 써요.
export function setup(_api) {
  const style = document.createElement('style');
  style.textContent = CSS;
  document.head.append(style);
  return () => style.remove();
}

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);
example.js
const items = [
  'Pencil',
  'Notebook',
  'Eraser',
  'Scissors',
  'Glue',
  'Ruler',
  'Pencil case',
  'Stapler',
];
export const options = {
  columns: [
    { id: 'item', title: 'Item', width: 150, headerStyle: { fontWeight: 700 } },
    {
      id: 'price',
      title: 'Price',
      width: 110,
      kind: 'number',
      cellStyle: (ctx) => (Number(ctx.value) >= 5000 ? { fontWeight: 700 } : null),
    },
    {
      id: 'stock',
      title: 'Stock',
      width: 100,
      kind: 'number',
      cellClassRules: { 'cell-low': (ctx) => Number(ctx.value) <= 5 },
    },
    { id: 'note', title: 'Note', width: 140 },
  ],
  rows: Array.from({ length: 200 }, (_, i) => {
    const stock = (i * 7) % 23;
    return {
      id: i + 1,
      item: `${items[i % items.length]} ${i + 1}`,
      price: 500 + ((i * 337) % 9500),
      stock,
      note: stock === 0 ? 'Sold out' : '',
    };
  }),
  rowKey: 'id',
  striped: false,
  // Background for low-stock rows. It follows the value when it changes or when scrolling reuses a line.
  rowStyle: (row) => (row.stock <= 5 ? { backgroundColor: 'rgba(255, 200, 0, 0.18)' } : null),
  rowClassRules: { low: (row) => row.stock === 0 },
  rowTestId: (row, key) => `item-${String(key)}`,
};
const CSS = `
.mxg .cell-low { color: var(--mxg-tone-danger-fg, #b42318); font-weight: 700; }
.mxg .mxg-row.low { font-style: italic; }
`;
// Example-only CSS. In a real app, put it in the app stylesheet.
export function setup(_api) {
  const style = document.createElement('style');
  style.textContent = CSS;
  document.head.append(style);
  return () => style.remove();
}