컬럼 정렬 방향과 세로 줄무늬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.
코드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 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>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));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());
},
};<!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 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>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
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,
}));
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%만 섞어서, 밝은 테마와 어두운 테마 어디서나 같은 규칙으로 어울려요.
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();
}
const actions = {
align: (api, value) => {
state.align = value ?? 'type';
api.setColumns(build());
},
stripes: (api, value) => {
state.stripes = value !== 'off';
api.setColumns(build());
},
};
options.ariaLabel = "컬럼 정렬 방향과 세로 줄무늬";
const api = MaxGrid.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));
</script>
</body>
</html>버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api) : undefined), [api]);
return (
<>
<div className="toolbar">
<select
aria-label="글자 방향"
disabled={!api}
onChange={(event) => api && actions.align(api, event.target.value)}
>
<option value="type">종류별 (기본)</option>
<option value="start">모두 왼쪽</option>
<option value="end">모두 오른쪽</option>
</select>
<select defaultValue="type"
aria-label="세로 줄무늬"
disabled={!api}
onChange={(event) => api && actions.stripes(api, event.target.value)}
>
<option value="on">켬 (기본)</option>
<option value="off">끔</option>
</select>
</div>
<Grid style={{ height: 200 }} ref={gridRef} options={options} />
</>
);
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
code: string;
name: string;
qty: number;
price: number;
active: boolean;
}
const wonText = (value: unknown): string => `${Number(value).toLocaleString('ko-KR')}원`;
// 컬럼 정의의 원본. align은 헤더 글자와 셀 글자를 함께 옮기고, cellClass는 그 컬럼의 모든 셀에 클래스를 붙여요.
// 한 칸씩 건너뛰며 같은 클래스를 주면 세로 줄무늬가 돼서 숫자가 많을 때 눈이 줄을 따라가기 쉬워요.
const BASE: ColumnDef<Row>[] = [
{ 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 ? '예' : '아니오'),
},
];
type Align = 'type' | 'start' | 'end';
const state: { align: Align; stripes: boolean } = { align: 'type', stripes: true };
function alignOf(column: ColumnDef<Row>): 'start' | 'center' | 'end' {
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 = (): ColumnDef<Row>[] =>
BASE.map((c, i) => ({
...c,
align: alignOf(c),
cellClass: state.stripes && i % 2 === 0 ? 'ca-stripe' : undefined,
}));
export const options: GridOptions<Row> = {
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: GridApi<Row>) {
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: GridApi<Row>, value?: string) => {
state.align = (value as Align) ?? 'type';
api.setColumns(build());
},
stripes: (api: GridApi<Row>, value?: string) => {
state.stripes = value !== 'off';
api.setColumns(build());
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<select
aria-label="글자 방향"
:disabled="!api"
@change="api && actions.align(api, ($event.target as HTMLSelectElement).value)"
>
<option value="type" selected>종류별 (기본)</option>
<option value="start">모두 왼쪽</option>
<option value="end">모두 오른쪽</option>
</select>
<select
aria-label="세로 줄무늬"
:disabled="!api"
@change="api && actions.stripes(api, ($event.target as HTMLSelectElement).value)"
>
<option value="on" selected>켬 (기본)</option>
<option value="off">끔</option>
</select>
</div>
<Grid style="height: 200px" :options="options" @ready="onReady" />
</template>import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
code: string;
name: string;
qty: number;
price: number;
active: boolean;
}
const wonText = (value: unknown): string => `${Number(value).toLocaleString('ko-KR')}원`;
// 컬럼 정의의 원본. align은 헤더 글자와 셀 글자를 함께 옮기고, cellClass는 그 컬럼의 모든 셀에 클래스를 붙여요.
// 한 칸씩 건너뛰며 같은 클래스를 주면 세로 줄무늬가 돼서 숫자가 많을 때 눈이 줄을 따라가기 쉬워요.
const BASE: ColumnDef<Row>[] = [
{ 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 ? '예' : '아니오'),
},
];
type Align = 'type' | 'start' | 'end';
const state: { align: Align; stripes: boolean } = { align: 'type', stripes: true };
function alignOf(column: ColumnDef<Row>): 'start' | 'center' | 'end' {
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 = (): ColumnDef<Row>[] =>
BASE.map((c, i) => ({
...c,
align: alignOf(c),
cellClass: state.stripes && i % 2 === 0 ? 'ca-stripe' : undefined,
}));
export const options: GridOptions<Row> = {
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: GridApi<Row>) {
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: GridApi<Row>, value?: string) => {
state.align = (value as Align) ?? 'type';
api.setColumns(build());
},
stripes: (api: GridApi<Row>, value?: string) => {
state.stripes = value !== 'off';
api.setColumns(build());
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<select
aria-label="글자 방향"
[disabled]="!api"
(change)="api && actions.align(api, $any($event.target).value)"
>
<option value="type" selected>종류별 (기본)</option>
<option value="start">모두 왼쪽</option>
<option value="end">모두 오른쪽</option>
</select>
<select
aria-label="세로 줄무늬"
[disabled]="!api"
(change)="api && actions.stripes(api, $any($event.target).value)"
>
<option value="on" selected>켬 (기본)</option>
<option value="off">끔</option>
</select>
</div>
<mx-grid style="height: 200px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/angular';
export interface Row {
id: number;
code: string;
name: string;
qty: number;
price: number;
active: boolean;
}
const wonText = (value: unknown): string => `${Number(value).toLocaleString('ko-KR')}원`;
// 컬럼 정의의 원본. align은 헤더 글자와 셀 글자를 함께 옮기고, cellClass는 그 컬럼의 모든 셀에 클래스를 붙여요.
// 한 칸씩 건너뛰며 같은 클래스를 주면 세로 줄무늬가 돼서 숫자가 많을 때 눈이 줄을 따라가기 쉬워요.
const BASE: ColumnDef<Row>[] = [
{ 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 ? '예' : '아니오'),
},
];
type Align = 'type' | 'start' | 'end';
const state: { align: Align; stripes: boolean } = { align: 'type', stripes: true };
function alignOf(column: ColumnDef<Row>): 'start' | 'center' | 'end' {
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 = (): ColumnDef<Row>[] =>
BASE.map((c, i) => ({
...c,
align: alignOf(c),
cellClass: state.stripes && i % 2 === 0 ? 'ca-stripe' : undefined,
}));
export const options: GridOptions<Row> = {
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: GridApi<Row>) {
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: GridApi<Row>, value?: string) => {
state.align = (value as Align) ?? 'type';
api.setColumns(build());
},
stripes: (api: GridApi<Row>, value?: string) => {
state.stripes = value !== 'off';
api.setColumns(build());
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<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>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));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());
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Column alignment and vertical stripes</title>
</head>
<body>
<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>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
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,
}));
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.
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();
}
const actions = {
align: (api, value) => {
state.align = value ?? 'type';
api.setColumns(build());
},
stripes: (api, value) => {
state.stripes = value !== 'off';
api.setColumns(build());
},
};
options.ariaLabel = "Column alignment and vertical stripes";
const api = MaxGrid.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));
</script>
</body>
</html>When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api) : undefined), [api]);
return (
<>
<div className="toolbar">
<select
aria-label="Text direction"
disabled={!api}
onChange={(event) => api && actions.align(api, event.target.value)}
>
<option value="type">By type (default)</option>
<option value="start">All left</option>
<option value="end">All right</option>
</select>
<select defaultValue="type"
aria-label="Stripes"
disabled={!api}
onChange={(event) => api && actions.stripes(api, event.target.value)}
>
<option value="on">On (default)</option>
<option value="off">Off</option>
</select>
</div>
<Grid style={{ height: 200 }} ref={gridRef} options={options} />
</>
);
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
code: string;
name: string;
qty: number;
price: number;
active: boolean;
}
const priceText = (value: unknown): string => `$${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: ColumnDef<Row>[] = [
{ 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'),
},
];
type Align = 'type' | 'start' | 'end';
const state: { align: Align; stripes: boolean } = { align: 'type', stripes: true };
function alignOf(column: ColumnDef<Row>): 'start' | 'center' | 'end' {
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 = (): ColumnDef<Row>[] =>
BASE.map((c, i) => ({
...c,
align: alignOf(c),
cellClass: state.stripes && i % 2 === 0 ? 'ca-stripe' : undefined,
}));
export const options: GridOptions<Row> = {
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: GridApi<Row>) {
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: GridApi<Row>, value?: string) => {
state.align = (value as Align) ?? 'type';
api.setColumns(build());
},
stripes: (api: GridApi<Row>, value?: string) => {
state.stripes = value !== 'off';
api.setColumns(build());
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<select
aria-label="Text direction"
:disabled="!api"
@change="api && actions.align(api, ($event.target as HTMLSelectElement).value)"
>
<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"
:disabled="!api"
@change="api && actions.stripes(api, ($event.target as HTMLSelectElement).value)"
>
<option value="on" selected>On (default)</option>
<option value="off">Off</option>
</select>
</div>
<Grid style="height: 200px" :options="options" @ready="onReady" />
</template>import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
code: string;
name: string;
qty: number;
price: number;
active: boolean;
}
const priceText = (value: unknown): string => `$${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: ColumnDef<Row>[] = [
{ 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'),
},
];
type Align = 'type' | 'start' | 'end';
const state: { align: Align; stripes: boolean } = { align: 'type', stripes: true };
function alignOf(column: ColumnDef<Row>): 'start' | 'center' | 'end' {
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 = (): ColumnDef<Row>[] =>
BASE.map((c, i) => ({
...c,
align: alignOf(c),
cellClass: state.stripes && i % 2 === 0 ? 'ca-stripe' : undefined,
}));
export const options: GridOptions<Row> = {
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: GridApi<Row>) {
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: GridApi<Row>, value?: string) => {
state.align = (value as Align) ?? 'type';
api.setColumns(build());
},
stripes: (api: GridApi<Row>, value?: string) => {
state.stripes = value !== 'off';
api.setColumns(build());
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<select
aria-label="Text direction"
[disabled]="!api"
(change)="api && actions.align(api, $any($event.target).value)"
>
<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"
[disabled]="!api"
(change)="api && actions.stripes(api, $any($event.target).value)"
>
<option value="on" selected>On (default)</option>
<option value="off">Off</option>
</select>
</div>
<mx-grid style="height: 200px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/angular';
export interface Row {
id: number;
code: string;
name: string;
qty: number;
price: number;
active: boolean;
}
const priceText = (value: unknown): string => `$${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: ColumnDef<Row>[] = [
{ 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'),
},
];
type Align = 'type' | 'start' | 'end';
const state: { align: Align; stripes: boolean } = { align: 'type', stripes: true };
function alignOf(column: ColumnDef<Row>): 'start' | 'center' | 'end' {
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 = (): ColumnDef<Row>[] =>
BASE.map((c, i) => ({
...c,
align: alignOf(c),
cellClass: state.stripes && i % 2 === 0 ? 'ca-stripe' : undefined,
}));
export const options: GridOptions<Row> = {
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: GridApi<Row>) {
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: GridApi<Row>, value?: string) => {
state.align = (value as Align) ?? 'type';
api.setColumns(build());
},
stripes: (api: GridApi<Row>, value?: string) => {
state.stripes = value !== 'off';
api.setColumns(build());
},
};