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

컬럼 정렬 방향과 세로 줄무늬Column alignment and vertical stripes

컬럼의 글자 방향(왼쪽·가운데·오른쪽)을 정하면 헤더와 셀이 함께 움직여요. 한 칸씩 건너뛰며 같은 모양을 주면 세로 줄무늬가 돼서 줄을 따라 읽기 쉬워요.A column's text direction moves header and cells together. Giving every other column the same look makes vertical stripes that help the eye follow a row.

입문Beginner
직접 써 보기Live preview컬럼 정렬 방향과 세로 줄무늬Column alignment and vertical stripes

코드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="align">
      <option value="type" selected>종류별 (기본)</option>
      <option value="start">모두 왼쪽</option>
      <option value="end">모두 오른쪽</option>
    </select>
    <select aria-label="세로 줄무늬" id="stripes">
      <option value="on" selected>켬 (기본)</option>
      <option value="off">끔</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);
document.getElementById('align').addEventListener('change', (event) => actions.align(api, event.target.value));
document.getElementById('stripes').addEventListener('change', (event) => actions.stripes(api, event.target.value));
example.js
const wonText = (value) => `${Number(value).toLocaleString('ko-KR')}원`;
// 컬럼 정의의 원본. align은 헤더 글자와 셀 글자를 함께 옮기고, cellClass는 그 컬럼의 모든 셀에 클래스를 붙여요.
// 한 칸씩 건너뛰며 같은 클래스를 주면 세로 줄무늬가 돼서 숫자가 많을 때 눈이 줄을 따라가기 쉬워요.
const BASE = [
  { id: 'code', title: '코드', width: 100 },
  { id: 'name', title: '품명', width: 170 },
  { id: 'qty', title: '수량', kind: 'number', width: 90 },
  { id: 'price', title: '단가', kind: 'number', width: 120, formatValue: wonText },
  {
    id: 'active',
    title: '판매 중',
    kind: 'boolean',
    width: 100,
    formatValue: (v) => (v ? '예' : '아니오'),
  },
];
const state = { align: 'type', stripes: true };
function alignOf(column) {
  if (state.align === 'start')
    return 'start';
  if (state.align === 'end')
    return 'end';
  // 종류별: 숫자는 오른쪽, 예/아니오와 짧은 코드는 가운데, 나머지는 왼쪽
  if (column.kind === 'number')
    return 'end';
  return column.kind === 'boolean' || column.id === 'code' ? 'center' : 'start';
}
const build = () => BASE.map((c, i) => ({
  ...c,
  align: alignOf(c),
  cellClass: state.stripes && i % 2 === 0 ? 'ca-stripe' : undefined,
}));
export const options = {
  columns: build(),
  rows: [
    { id: 1, code: 'A-100', name: '철제 선반', qty: 24, price: 128000, active: true },
    { id: 2, code: 'B-220', name: '핸드 파레트 트럭', qty: 6, price: 459000, active: true },
    { id: 3, code: 'C-031', name: '스트레치 필름', qty: 120, price: 38500, active: false },
    { id: 4, code: 'D-418', name: '라벨 프린터', qty: 9, price: 289000, active: true },
  ],
  rowKey: 'id',
};
// 줄무늬 색은 글자색을 배경에 7%만 섞어서, 밝은 테마와 어두운 테마 어디서나 같은 규칙으로 어울려요.
export function setup(_api) {
  state.align = 'type';
  state.stripes = true;
  const style = document.createElement('style');
  style.textContent =
    '.ca-stripe { background: color-mix(in srgb, currentColor 7%, transparent); }';
  document.head.append(style);
  return () => style.remove();
}
export const actions = {
  align: (api, value) => {
    state.align = value ?? 'type';
    api.setColumns(build());
  },
  stripes: (api, value) => {
    state.stripes = value !== 'off';
    api.setColumns(build());
  },
};

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="Text direction" id="align">
      <option value="type" selected>By type (default)</option>
      <option value="start">All left</option>
      <option value="end">All right</option>
    </select>
    <select aria-label="Stripes" id="stripes">
      <option value="on" selected>On (default)</option>
      <option value="off">Off</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);
document.getElementById('align').addEventListener('change', (event) => actions.align(api, event.target.value));
document.getElementById('stripes').addEventListener('change', (event) => actions.stripes(api, event.target.value));
example.js
const priceText = (value) => `$${Number(value).toLocaleString('en-US')}`;
// The source column definitions. align moves the header text and the cell text together, and cellClass adds a class to every cell of that column.
// Giving the same class to every other column makes vertical stripes, which helps the eye follow a column when there are many numbers.
const BASE = [
  { id: 'code', title: 'Code', width: 100 },
  { id: 'name', title: 'Product', width: 170 },
  { id: 'qty', title: 'Qty', kind: 'number', width: 90 },
  { id: 'price', title: 'Price', kind: 'number', width: 120, formatValue: priceText },
  {
    id: 'active',
    title: 'On sale',
    kind: 'boolean',
    width: 100,
    formatValue: (v) => (v ? 'Yes' : 'No'),
  },
];
const state = { align: 'type', stripes: true };
function alignOf(column) {
  if (state.align === 'start')
    return 'start';
  if (state.align === 'end')
    return 'end';
  // By type: numbers to the right, yes/no and short codes centered, the rest to the left
  if (column.kind === 'number')
    return 'end';
  return column.kind === 'boolean' || column.id === 'code' ? 'center' : 'start';
}
const build = () => BASE.map((c, i) => ({
  ...c,
  align: alignOf(c),
  cellClass: state.stripes && i % 2 === 0 ? 'ca-stripe' : undefined,
}));
export const options = {
  columns: build(),
  rows: [
    { id: 1, code: 'A-100', name: 'Steel Shelf', qty: 24, price: 128, active: true },
    { id: 2, code: 'B-220', name: 'Hand Pallet Truck', qty: 6, price: 459, active: true },
    { id: 3, code: 'C-031', name: 'Stretch Film', qty: 120, price: 39, active: false },
    { id: 4, code: 'D-418', name: 'Label Printer', qty: 9, price: 289, active: true },
  ],
  rowKey: 'id',
};
// The stripe color mixes only 7% of the text color into the background, so the same rule suits both the light and the dark theme.
export function setup(_api) {
  state.align = 'type';
  state.stripes = true;
  const style = document.createElement('style');
  style.textContent =
    '.ca-stripe { background: color-mix(in srgb, currentColor 7%, transparent); }';
  document.head.append(style);
  return () => style.remove();
}
export const actions = {
  align: (api, value) => {
    state.align = value ?? 'type';
    api.setColumns(build());
  },
  stripes: (api, value) => {
    state.stripes = value !== 'off';
    api.setColumns(build());
  },
};