연체 줄·마이너스 잔액·줄무늬를 조건으로 꾸미기Style overdue rows, credits and stripes by condition
줄과 칸에 조건을 주면 줄무늬, 연체 줄, 마이너스 잔액 같은 모양이 자동으로 붙어요. 상태는 글자로도 적어서 색만으로 구분하지 않고, 성과 이름을 합친 고객 컬럼은 다른 값으로 계산해서 보여 줘요.Conditions on rows and cells add stripes, overdue rows and negative balances automatically. The status is also written as text so color is not the only cue, and the customer column is computed from other fields.
코드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="stripes">
<option value="on" selected>켬 (기본)</option>
<option value="off">끔</option>
</select>
<button type="button" id="sortBalance">잔액 큰 순</button>
<button type="button" id="clearSort">정렬 해제</button>
</div>
<div id="grid" style="height: 280px"></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('stripes').addEventListener('change', (event) => actions.stripes(api, event.target.value));
document.getElementById('sortBalance').addEventListener('click', () => actions.sortBalance(api));
document.getElementById('clearSort').addEventListener('click', () => actions.clearSort(api));let striped = true;
// 줄무늬와 상태 색은 rowClass·cellClass가 돌려주는 CSS 클래스예요. 상태는 글자('상태' 컬럼)로도 적어서
// 색만으로 구분하지 않게 했고, 고객 이름은 getValue로 성과 이름을 합친 계산 컬럼이에요.
export const options = {
columns: [
{ id: 'invoice', title: '청구서', width: 110 },
{ id: 'customer', title: '고객', width: 130, getValue: (row) => `${row.last}${row.first}` },
{ id: 'status', title: '상태', width: 90 },
{
id: 'balance',
title: '잔액(원)',
kind: 'number',
width: 130,
align: 'end',
cellClass: (ctx) => (Number(ctx.value) < 0 ? 'cf-credit' : undefined),
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
},
],
rows: [
{ id: 1, invoice: 'INV-2401', first: '민준', last: '김', status: '완납', balance: 0 },
{ id: 2, invoice: 'INV-2402', first: '서연', last: '이', status: '연체', balance: 1280000 },
{ id: 3, invoice: 'INV-2403', first: '지호', last: '박', status: '대기', balance: 455000 },
{ id: 4, invoice: 'INV-2404', first: '유나', last: '최', status: '환불', balance: -98000 },
{ id: 5, invoice: 'INV-2405', first: '도윤', last: '정', status: '연체', balance: 760000 },
{ id: 6, invoice: 'INV-2406', first: '소희', last: '한', status: '대기', balance: 320000 },
],
rowKey: 'id',
// 내장 줄무늬는 끄고, 아래 rowClass의 줄무늬만 켜고 끄게 해요(안 끄면 선택 상자를 '끔'으로 해도 줄무늬가 남아요).
striped: false,
rowClass: (row, index) => {
const classes = [];
if (striped && index % 2 === 1)
classes.push('cf-stripe');
if (row.status === '연체')
classes.push('cf-overdue');
return classes.join(' ') || undefined;
},
};
const CSS = `
.mxg-row.cf-stripe { background: var(--mxg-bg-row-stripe); }
.mxg-row.cf-overdue { background: var(--mxg-bg-invalid); box-shadow: inset 3px 0 0 var(--mxg-danger); }
.mxg-cell.cf-credit { color: var(--mxg-mark-added); font-weight: 600; }
`;
// 예제가 자기 스타일 규칙을 넣고, 정리 함수에서 지워요. 규칙이 생긴 뒤에 줄을 다시 그리게 해요.
export function setup(api) {
striped = true;
const style = document.createElement('style');
style.textContent = CSS;
document.head.append(style);
api.refresh();
return () => style.remove();
}
export const actions = {
stripes: (api, value) => {
striped = value !== 'off';
api.refresh();
},
sortBalance: (api) => api.setSortModel([{ columnId: 'balance', direction: 'desc' }]),
clearSort: (api) => api.setSortModel([]),
};<!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="stripes">
<option value="on" selected>켬 (기본)</option>
<option value="off">끔</option>
</select>
<button type="button" id="sortBalance">잔액 큰 순</button>
<button type="button" id="clearSort">정렬 해제</button>
</div>
<div id="grid" style="height: 280px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
let striped = true;
// 줄무늬와 상태 색은 rowClass·cellClass가 돌려주는 CSS 클래스예요. 상태는 글자('상태' 컬럼)로도 적어서
// 색만으로 구분하지 않게 했고, 고객 이름은 getValue로 성과 이름을 합친 계산 컬럼이에요.
const options = {
columns: [
{ id: 'invoice', title: '청구서', width: 110 },
{ id: 'customer', title: '고객', width: 130, getValue: (row) => `${row.last}${row.first}` },
{ id: 'status', title: '상태', width: 90 },
{
id: 'balance',
title: '잔액(원)',
kind: 'number',
width: 130,
align: 'end',
cellClass: (ctx) => (Number(ctx.value) < 0 ? 'cf-credit' : undefined),
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
},
],
rows: [
{ id: 1, invoice: 'INV-2401', first: '민준', last: '김', status: '완납', balance: 0 },
{ id: 2, invoice: 'INV-2402', first: '서연', last: '이', status: '연체', balance: 1280000 },
{ id: 3, invoice: 'INV-2403', first: '지호', last: '박', status: '대기', balance: 455000 },
{ id: 4, invoice: 'INV-2404', first: '유나', last: '최', status: '환불', balance: -98000 },
{ id: 5, invoice: 'INV-2405', first: '도윤', last: '정', status: '연체', balance: 760000 },
{ id: 6, invoice: 'INV-2406', first: '소희', last: '한', status: '대기', balance: 320000 },
],
rowKey: 'id',
// 내장 줄무늬는 끄고, 아래 rowClass의 줄무늬만 켜고 끄게 해요(안 끄면 선택 상자를 '끔'으로 해도 줄무늬가 남아요).
striped: false,
rowClass: (row, index) => {
const classes = [];
if (striped && index % 2 === 1)
classes.push('cf-stripe');
if (row.status === '연체')
classes.push('cf-overdue');
return classes.join(' ') || undefined;
},
};
const CSS = `
.mxg-row.cf-stripe { background: var(--mxg-bg-row-stripe); }
.mxg-row.cf-overdue { background: var(--mxg-bg-invalid); box-shadow: inset 3px 0 0 var(--mxg-danger); }
.mxg-cell.cf-credit { color: var(--mxg-mark-added); font-weight: 600; }
`;
// 예제가 자기 스타일 규칙을 넣고, 정리 함수에서 지워요. 규칙이 생긴 뒤에 줄을 다시 그리게 해요.
function setup(api) {
striped = true;
const style = document.createElement('style');
style.textContent = CSS;
document.head.append(style);
api.refresh();
return () => style.remove();
}
const actions = {
stripes: (api, value) => {
striped = value !== 'off';
api.refresh();
},
sortBalance: (api) => api.setSortModel([{ columnId: 'balance', direction: 'desc' }]),
clearSort: (api) => api.setSortModel([]),
};
options.ariaLabel = "연체 줄·마이너스 잔액·줄무늬를 조건으로 꾸미기";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api);
document.getElementById('stripes').addEventListener('change', (event) => actions.stripes(api, event.target.value));
document.getElementById('sortBalance').addEventListener('click', () => actions.sortBalance(api));
document.getElementById('clearSort').addEventListener('click', () => actions.clearSort(api));
</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.stripes(api, event.target.value)}
>
<option value="on">켬 (기본)</option>
<option value="off">끔</option>
</select>
<button
type="button"
disabled={!api}
onClick={() => api && actions.sortBalance(api)}
>잔액 큰 순</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.clearSort(api)}
>정렬 해제</button>
</div>
<Grid style={{ height: 280 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
invoice: string;
first: string;
last: string;
status: string;
balance: number;
}
let striped = true;
// 줄무늬와 상태 색은 rowClass·cellClass가 돌려주는 CSS 클래스예요. 상태는 글자('상태' 컬럼)로도 적어서
// 색만으로 구분하지 않게 했고, 고객 이름은 getValue로 성과 이름을 합친 계산 컬럼이에요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'invoice', title: '청구서', width: 110 },
{ id: 'customer', title: '고객', width: 130, getValue: (row) => `${row.last}${row.first}` },
{ id: 'status', title: '상태', width: 90 },
{
id: 'balance',
title: '잔액(원)',
kind: 'number',
width: 130,
align: 'end',
cellClass: (ctx) => (Number(ctx.value) < 0 ? 'cf-credit' : undefined),
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
},
],
rows: [
{ id: 1, invoice: 'INV-2401', first: '민준', last: '김', status: '완납', balance: 0 },
{ id: 2, invoice: 'INV-2402', first: '서연', last: '이', status: '연체', balance: 1280000 },
{ id: 3, invoice: 'INV-2403', first: '지호', last: '박', status: '대기', balance: 455000 },
{ id: 4, invoice: 'INV-2404', first: '유나', last: '최', status: '환불', balance: -98000 },
{ id: 5, invoice: 'INV-2405', first: '도윤', last: '정', status: '연체', balance: 760000 },
{ id: 6, invoice: 'INV-2406', first: '소희', last: '한', status: '대기', balance: 320000 },
],
rowKey: 'id',
// 내장 줄무늬는 끄고, 아래 rowClass의 줄무늬만 켜고 끄게 해요(안 끄면 선택 상자를 '끔'으로 해도 줄무늬가 남아요).
striped: false,
rowClass: (row, index) => {
const classes: string[] = [];
if (striped && index % 2 === 1) classes.push('cf-stripe');
if (row.status === '연체') classes.push('cf-overdue');
return classes.join(' ') || undefined;
},
};
const CSS = `
.mxg-row.cf-stripe { background: var(--mxg-bg-row-stripe); }
.mxg-row.cf-overdue { background: var(--mxg-bg-invalid); box-shadow: inset 3px 0 0 var(--mxg-danger); }
.mxg-cell.cf-credit { color: var(--mxg-mark-added); font-weight: 600; }
`;
// 예제가 자기 스타일 규칙을 넣고, 정리 함수에서 지워요. 규칙이 생긴 뒤에 줄을 다시 그리게 해요.
export function setup(api: GridApi<Row>) {
striped = true;
const style = document.createElement('style');
style.textContent = CSS;
document.head.append(style);
api.refresh();
return () => style.remove();
}
export const actions = {
stripes: (api: GridApi<Row>, value?: string) => {
striped = value !== 'off';
api.refresh();
},
sortBalance: (api: GridApi<Row>) =>
api.setSortModel([{ columnId: 'balance', direction: 'desc' }]),
clearSort: (api: GridApi<Row>) => api.setSortModel([]),
};버튼을 누르면 이 예제의 코드가 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.stripes(api, ($event.target as HTMLSelectElement).value)"
>
<option value="on" selected>켬 (기본)</option>
<option value="off">끔</option>
</select>
<button
type="button"
:disabled="!api"
@click="api && actions.sortBalance(api)"
>잔액 큰 순</button>
<button
type="button"
:disabled="!api"
@click="api && actions.clearSort(api)"
>정렬 해제</button>
</div>
<Grid style="height: 280px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
invoice: string;
first: string;
last: string;
status: string;
balance: number;
}
let striped = true;
// 줄무늬와 상태 색은 rowClass·cellClass가 돌려주는 CSS 클래스예요. 상태는 글자('상태' 컬럼)로도 적어서
// 색만으로 구분하지 않게 했고, 고객 이름은 getValue로 성과 이름을 합친 계산 컬럼이에요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'invoice', title: '청구서', width: 110 },
{ id: 'customer', title: '고객', width: 130, getValue: (row) => `${row.last}${row.first}` },
{ id: 'status', title: '상태', width: 90 },
{
id: 'balance',
title: '잔액(원)',
kind: 'number',
width: 130,
align: 'end',
cellClass: (ctx) => (Number(ctx.value) < 0 ? 'cf-credit' : undefined),
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
},
],
rows: [
{ id: 1, invoice: 'INV-2401', first: '민준', last: '김', status: '완납', balance: 0 },
{ id: 2, invoice: 'INV-2402', first: '서연', last: '이', status: '연체', balance: 1280000 },
{ id: 3, invoice: 'INV-2403', first: '지호', last: '박', status: '대기', balance: 455000 },
{ id: 4, invoice: 'INV-2404', first: '유나', last: '최', status: '환불', balance: -98000 },
{ id: 5, invoice: 'INV-2405', first: '도윤', last: '정', status: '연체', balance: 760000 },
{ id: 6, invoice: 'INV-2406', first: '소희', last: '한', status: '대기', balance: 320000 },
],
rowKey: 'id',
// 내장 줄무늬는 끄고, 아래 rowClass의 줄무늬만 켜고 끄게 해요(안 끄면 선택 상자를 '끔'으로 해도 줄무늬가 남아요).
striped: false,
rowClass: (row, index) => {
const classes: string[] = [];
if (striped && index % 2 === 1) classes.push('cf-stripe');
if (row.status === '연체') classes.push('cf-overdue');
return classes.join(' ') || undefined;
},
};
const CSS = `
.mxg-row.cf-stripe { background: var(--mxg-bg-row-stripe); }
.mxg-row.cf-overdue { background: var(--mxg-bg-invalid); box-shadow: inset 3px 0 0 var(--mxg-danger); }
.mxg-cell.cf-credit { color: var(--mxg-mark-added); font-weight: 600; }
`;
// 예제가 자기 스타일 규칙을 넣고, 정리 함수에서 지워요. 규칙이 생긴 뒤에 줄을 다시 그리게 해요.
export function setup(api: GridApi<Row>) {
striped = true;
const style = document.createElement('style');
style.textContent = CSS;
document.head.append(style);
api.refresh();
return () => style.remove();
}
export const actions = {
stripes: (api: GridApi<Row>, value?: string) => {
striped = value !== 'off';
api.refresh();
},
sortBalance: (api: GridApi<Row>) =>
api.setSortModel([{ columnId: 'balance', direction: 'desc' }]),
clearSort: (api: GridApi<Row>) => api.setSortModel([]),
};버튼을 누르면 이 예제의 코드가 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.stripes(api, $any($event.target).value)"
>
<option value="on" selected>켬 (기본)</option>
<option value="off">끔</option>
</select>
<button
type="button"
[disabled]="!api"
(click)="api && actions.sortBalance(api)"
>잔액 큰 순</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.clearSort(api)"
>정렬 해제</button>
</div>
<mx-grid style="height: 280px" [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 { GridApi, GridOptions } from '@maxgrid/angular';
export interface Row {
id: number;
invoice: string;
first: string;
last: string;
status: string;
balance: number;
}
let striped = true;
// 줄무늬와 상태 색은 rowClass·cellClass가 돌려주는 CSS 클래스예요. 상태는 글자('상태' 컬럼)로도 적어서
// 색만으로 구분하지 않게 했고, 고객 이름은 getValue로 성과 이름을 합친 계산 컬럼이에요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'invoice', title: '청구서', width: 110 },
{ id: 'customer', title: '고객', width: 130, getValue: (row) => `${row.last}${row.first}` },
{ id: 'status', title: '상태', width: 90 },
{
id: 'balance',
title: '잔액(원)',
kind: 'number',
width: 130,
align: 'end',
cellClass: (ctx) => (Number(ctx.value) < 0 ? 'cf-credit' : undefined),
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
},
],
rows: [
{ id: 1, invoice: 'INV-2401', first: '민준', last: '김', status: '완납', balance: 0 },
{ id: 2, invoice: 'INV-2402', first: '서연', last: '이', status: '연체', balance: 1280000 },
{ id: 3, invoice: 'INV-2403', first: '지호', last: '박', status: '대기', balance: 455000 },
{ id: 4, invoice: 'INV-2404', first: '유나', last: '최', status: '환불', balance: -98000 },
{ id: 5, invoice: 'INV-2405', first: '도윤', last: '정', status: '연체', balance: 760000 },
{ id: 6, invoice: 'INV-2406', first: '소희', last: '한', status: '대기', balance: 320000 },
],
rowKey: 'id',
// 내장 줄무늬는 끄고, 아래 rowClass의 줄무늬만 켜고 끄게 해요(안 끄면 선택 상자를 '끔'으로 해도 줄무늬가 남아요).
striped: false,
rowClass: (row, index) => {
const classes: string[] = [];
if (striped && index % 2 === 1) classes.push('cf-stripe');
if (row.status === '연체') classes.push('cf-overdue');
return classes.join(' ') || undefined;
},
};
const CSS = `
.mxg-row.cf-stripe { background: var(--mxg-bg-row-stripe); }
.mxg-row.cf-overdue { background: var(--mxg-bg-invalid); box-shadow: inset 3px 0 0 var(--mxg-danger); }
.mxg-cell.cf-credit { color: var(--mxg-mark-added); font-weight: 600; }
`;
// 예제가 자기 스타일 규칙을 넣고, 정리 함수에서 지워요. 규칙이 생긴 뒤에 줄을 다시 그리게 해요.
export function setup(api: GridApi<Row>) {
striped = true;
const style = document.createElement('style');
style.textContent = CSS;
document.head.append(style);
api.refresh();
return () => style.remove();
}
export const actions = {
stripes: (api: GridApi<Row>, value?: string) => {
striped = value !== 'off';
api.refresh();
},
sortBalance: (api: GridApi<Row>) =>
api.setSortModel([{ columnId: 'balance', direction: 'desc' }]),
clearSort: (api: GridApi<Row>) => api.setSortModel([]),
};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="Stripes" id="stripes">
<option value="on" selected>On (default)</option>
<option value="off">Off</option>
</select>
<button type="button" id="sortBalance">Balance high to low</button>
<button type="button" id="clearSort">Clear sort</button>
</div>
<div id="grid" style="height: 280px"></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('stripes').addEventListener('change', (event) => actions.stripes(api, event.target.value));
document.getElementById('sortBalance').addEventListener('click', () => actions.sortBalance(api));
document.getElementById('clearSort').addEventListener('click', () => actions.clearSort(api));let striped = true;
// Stripes and status colors are CSS classes returned by rowClass and cellClass. The status is also written as text (the 'Status' column)
// so it is not shown by color alone, and the customer name is a computed column that joins first and last name with getValue.
export const options = {
columns: [
{ id: 'invoice', title: 'Invoice', width: 110 },
{
id: 'customer',
title: 'Customer',
width: 150,
getValue: (row) => `${row.first} ${row.last}`,
},
{ id: 'status', title: 'Status', width: 90 },
{
id: 'balance',
title: 'Balance ($)',
kind: 'number',
width: 130,
align: 'end',
cellClass: (ctx) => (Number(ctx.value) < 0 ? 'cf-credit' : undefined),
formatValue: (value) => Number(value).toLocaleString('en-US'),
},
],
rows: [
{ id: 1, invoice: 'INV-2401', first: 'Liam', last: 'Carter', status: 'Paid', balance: 0 },
{
id: 2,
invoice: 'INV-2402',
first: 'Emma',
last: 'Stone',
status: 'Overdue',
balance: 1280,
},
{
id: 3,
invoice: 'INV-2403',
first: 'Noah',
last: 'Parker',
status: 'Waiting',
balance: 455,
},
{
id: 4,
invoice: 'INV-2404',
first: 'Olivia',
last: 'Reed',
status: 'Refunded',
balance: -98,
},
{
id: 5,
invoice: 'INV-2405',
first: 'Ethan',
last: 'Brooks',
status: 'Overdue',
balance: 760,
},
{
id: 6,
invoice: 'INV-2406',
first: 'Sophia',
last: 'Hayes',
status: 'Waiting',
balance: 320,
},
],
rowKey: 'id',
// Turn off the built-in stripes and toggle only the stripes from rowClass below (otherwise stripes stay even when the select box says 'off').
striped: false,
rowClass: (row, index) => {
const classes = [];
if (striped && index % 2 === 1)
classes.push('cf-stripe');
if (row.status === 'Overdue')
classes.push('cf-overdue');
return classes.join(' ') || undefined;
},
};
const CSS = `
.mxg-row.cf-stripe { background: var(--mxg-bg-row-stripe); }
.mxg-row.cf-overdue { background: var(--mxg-bg-invalid); box-shadow: inset 3px 0 0 var(--mxg-danger); }
.mxg-cell.cf-credit { color: var(--mxg-mark-added); font-weight: 600; }
`;
// The example adds its own style rules and removes them in the cleanup function. Rows are redrawn after the rules exist.
export function setup(api) {
striped = true;
const style = document.createElement('style');
style.textContent = CSS;
document.head.append(style);
api.refresh();
return () => style.remove();
}
export const actions = {
stripes: (api, value) => {
striped = value !== 'off';
api.refresh();
},
sortBalance: (api) => api.setSortModel([{ columnId: 'balance', direction: 'desc' }]),
clearSort: (api) => api.setSortModel([]),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Style overdue rows, credits and stripes by condition</title>
</head>
<body>
<div class="toolbar">
<select aria-label="Stripes" id="stripes">
<option value="on" selected>On (default)</option>
<option value="off">Off</option>
</select>
<button type="button" id="sortBalance">Balance high to low</button>
<button type="button" id="clearSort">Clear sort</button>
</div>
<div id="grid" style="height: 280px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
let striped = true;
// Stripes and status colors are CSS classes returned by rowClass and cellClass. The status is also written as text (the 'Status' column)
// so it is not shown by color alone, and the customer name is a computed column that joins first and last name with getValue.
const options = {
columns: [
{ id: 'invoice', title: 'Invoice', width: 110 },
{
id: 'customer',
title: 'Customer',
width: 150,
getValue: (row) => `${row.first} ${row.last}`,
},
{ id: 'status', title: 'Status', width: 90 },
{
id: 'balance',
title: 'Balance ($)',
kind: 'number',
width: 130,
align: 'end',
cellClass: (ctx) => (Number(ctx.value) < 0 ? 'cf-credit' : undefined),
formatValue: (value) => Number(value).toLocaleString('en-US'),
},
],
rows: [
{ id: 1, invoice: 'INV-2401', first: 'Liam', last: 'Carter', status: 'Paid', balance: 0 },
{
id: 2,
invoice: 'INV-2402',
first: 'Emma',
last: 'Stone',
status: 'Overdue',
balance: 1280,
},
{
id: 3,
invoice: 'INV-2403',
first: 'Noah',
last: 'Parker',
status: 'Waiting',
balance: 455,
},
{
id: 4,
invoice: 'INV-2404',
first: 'Olivia',
last: 'Reed',
status: 'Refunded',
balance: -98,
},
{
id: 5,
invoice: 'INV-2405',
first: 'Ethan',
last: 'Brooks',
status: 'Overdue',
balance: 760,
},
{
id: 6,
invoice: 'INV-2406',
first: 'Sophia',
last: 'Hayes',
status: 'Waiting',
balance: 320,
},
],
rowKey: 'id',
// Turn off the built-in stripes and toggle only the stripes from rowClass below (otherwise stripes stay even when the select box says 'off').
striped: false,
rowClass: (row, index) => {
const classes = [];
if (striped && index % 2 === 1)
classes.push('cf-stripe');
if (row.status === 'Overdue')
classes.push('cf-overdue');
return classes.join(' ') || undefined;
},
};
const CSS = `
.mxg-row.cf-stripe { background: var(--mxg-bg-row-stripe); }
.mxg-row.cf-overdue { background: var(--mxg-bg-invalid); box-shadow: inset 3px 0 0 var(--mxg-danger); }
.mxg-cell.cf-credit { color: var(--mxg-mark-added); font-weight: 600; }
`;
// The example adds its own style rules and removes them in the cleanup function. Rows are redrawn after the rules exist.
function setup(api) {
striped = true;
const style = document.createElement('style');
style.textContent = CSS;
document.head.append(style);
api.refresh();
return () => style.remove();
}
const actions = {
stripes: (api, value) => {
striped = value !== 'off';
api.refresh();
},
sortBalance: (api) => api.setSortModel([{ columnId: 'balance', direction: 'desc' }]),
clearSort: (api) => api.setSortModel([]),
};
options.ariaLabel = "Style overdue rows, credits and stripes by condition";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api);
document.getElementById('stripes').addEventListener('change', (event) => actions.stripes(api, event.target.value));
document.getElementById('sortBalance').addEventListener('click', () => actions.sortBalance(api));
document.getElementById('clearSort').addEventListener('click', () => actions.clearSort(api));
</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="Stripes"
disabled={!api}
onChange={(event) => api && actions.stripes(api, event.target.value)}
>
<option value="on">On (default)</option>
<option value="off">Off</option>
</select>
<button
type="button"
disabled={!api}
onClick={() => api && actions.sortBalance(api)}
>Balance high to low</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.clearSort(api)}
>Clear sort</button>
</div>
<Grid style={{ height: 280 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
invoice: string;
first: string;
last: string;
status: string;
balance: number;
}
let striped = true;
// Stripes and status colors are CSS classes returned by rowClass and cellClass. The status is also written as text (the 'Status' column)
// so it is not shown by color alone, and the customer name is a computed column that joins first and last name with getValue.
export const options: GridOptions<Row> = {
columns: [
{ id: 'invoice', title: 'Invoice', width: 110 },
{
id: 'customer',
title: 'Customer',
width: 150,
getValue: (row) => `${row.first} ${row.last}`,
},
{ id: 'status', title: 'Status', width: 90 },
{
id: 'balance',
title: 'Balance ($)',
kind: 'number',
width: 130,
align: 'end',
cellClass: (ctx) => (Number(ctx.value) < 0 ? 'cf-credit' : undefined),
formatValue: (value) => Number(value).toLocaleString('en-US'),
},
],
rows: [
{ id: 1, invoice: 'INV-2401', first: 'Liam', last: 'Carter', status: 'Paid', balance: 0 },
{
id: 2,
invoice: 'INV-2402',
first: 'Emma',
last: 'Stone',
status: 'Overdue',
balance: 1280,
},
{
id: 3,
invoice: 'INV-2403',
first: 'Noah',
last: 'Parker',
status: 'Waiting',
balance: 455,
},
{
id: 4,
invoice: 'INV-2404',
first: 'Olivia',
last: 'Reed',
status: 'Refunded',
balance: -98,
},
{
id: 5,
invoice: 'INV-2405',
first: 'Ethan',
last: 'Brooks',
status: 'Overdue',
balance: 760,
},
{
id: 6,
invoice: 'INV-2406',
first: 'Sophia',
last: 'Hayes',
status: 'Waiting',
balance: 320,
},
],
rowKey: 'id',
// Turn off the built-in stripes and toggle only the stripes from rowClass below (otherwise stripes stay even when the select box says 'off').
striped: false,
rowClass: (row, index) => {
const classes: string[] = [];
if (striped && index % 2 === 1) classes.push('cf-stripe');
if (row.status === 'Overdue') classes.push('cf-overdue');
return classes.join(' ') || undefined;
},
};
const CSS = `
.mxg-row.cf-stripe { background: var(--mxg-bg-row-stripe); }
.mxg-row.cf-overdue { background: var(--mxg-bg-invalid); box-shadow: inset 3px 0 0 var(--mxg-danger); }
.mxg-cell.cf-credit { color: var(--mxg-mark-added); font-weight: 600; }
`;
// The example adds its own style rules and removes them in the cleanup function. Rows are redrawn after the rules exist.
export function setup(api: GridApi<Row>) {
striped = true;
const style = document.createElement('style');
style.textContent = CSS;
document.head.append(style);
api.refresh();
return () => style.remove();
}
export const actions = {
stripes: (api: GridApi<Row>, value?: string) => {
striped = value !== 'off';
api.refresh();
},
sortBalance: (api: GridApi<Row>) =>
api.setSortModel([{ columnId: 'balance', direction: 'desc' }]),
clearSort: (api: GridApi<Row>) => api.setSortModel([]),
};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="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>
<button
type="button"
:disabled="!api"
@click="api && actions.sortBalance(api)"
>Balance high to low</button>
<button
type="button"
:disabled="!api"
@click="api && actions.clearSort(api)"
>Clear sort</button>
</div>
<Grid style="height: 280px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
invoice: string;
first: string;
last: string;
status: string;
balance: number;
}
let striped = true;
// Stripes and status colors are CSS classes returned by rowClass and cellClass. The status is also written as text (the 'Status' column)
// so it is not shown by color alone, and the customer name is a computed column that joins first and last name with getValue.
export const options: GridOptions<Row> = {
columns: [
{ id: 'invoice', title: 'Invoice', width: 110 },
{
id: 'customer',
title: 'Customer',
width: 150,
getValue: (row) => `${row.first} ${row.last}`,
},
{ id: 'status', title: 'Status', width: 90 },
{
id: 'balance',
title: 'Balance ($)',
kind: 'number',
width: 130,
align: 'end',
cellClass: (ctx) => (Number(ctx.value) < 0 ? 'cf-credit' : undefined),
formatValue: (value) => Number(value).toLocaleString('en-US'),
},
],
rows: [
{ id: 1, invoice: 'INV-2401', first: 'Liam', last: 'Carter', status: 'Paid', balance: 0 },
{
id: 2,
invoice: 'INV-2402',
first: 'Emma',
last: 'Stone',
status: 'Overdue',
balance: 1280,
},
{
id: 3,
invoice: 'INV-2403',
first: 'Noah',
last: 'Parker',
status: 'Waiting',
balance: 455,
},
{
id: 4,
invoice: 'INV-2404',
first: 'Olivia',
last: 'Reed',
status: 'Refunded',
balance: -98,
},
{
id: 5,
invoice: 'INV-2405',
first: 'Ethan',
last: 'Brooks',
status: 'Overdue',
balance: 760,
},
{
id: 6,
invoice: 'INV-2406',
first: 'Sophia',
last: 'Hayes',
status: 'Waiting',
balance: 320,
},
],
rowKey: 'id',
// Turn off the built-in stripes and toggle only the stripes from rowClass below (otherwise stripes stay even when the select box says 'off').
striped: false,
rowClass: (row, index) => {
const classes: string[] = [];
if (striped && index % 2 === 1) classes.push('cf-stripe');
if (row.status === 'Overdue') classes.push('cf-overdue');
return classes.join(' ') || undefined;
},
};
const CSS = `
.mxg-row.cf-stripe { background: var(--mxg-bg-row-stripe); }
.mxg-row.cf-overdue { background: var(--mxg-bg-invalid); box-shadow: inset 3px 0 0 var(--mxg-danger); }
.mxg-cell.cf-credit { color: var(--mxg-mark-added); font-weight: 600; }
`;
// The example adds its own style rules and removes them in the cleanup function. Rows are redrawn after the rules exist.
export function setup(api: GridApi<Row>) {
striped = true;
const style = document.createElement('style');
style.textContent = CSS;
document.head.append(style);
api.refresh();
return () => style.remove();
}
export const actions = {
stripes: (api: GridApi<Row>, value?: string) => {
striped = value !== 'off';
api.refresh();
},
sortBalance: (api: GridApi<Row>) =>
api.setSortModel([{ columnId: 'balance', direction: 'desc' }]),
clearSort: (api: GridApi<Row>) => api.setSortModel([]),
};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="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>
<button
type="button"
[disabled]="!api"
(click)="api && actions.sortBalance(api)"
>Balance high to low</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.clearSort(api)"
>Clear sort</button>
</div>
<mx-grid style="height: 280px" [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 { GridApi, GridOptions } from '@maxgrid/angular';
export interface Row {
id: number;
invoice: string;
first: string;
last: string;
status: string;
balance: number;
}
let striped = true;
// Stripes and status colors are CSS classes returned by rowClass and cellClass. The status is also written as text (the 'Status' column)
// so it is not shown by color alone, and the customer name is a computed column that joins first and last name with getValue.
export const options: GridOptions<Row> = {
columns: [
{ id: 'invoice', title: 'Invoice', width: 110 },
{
id: 'customer',
title: 'Customer',
width: 150,
getValue: (row) => `${row.first} ${row.last}`,
},
{ id: 'status', title: 'Status', width: 90 },
{
id: 'balance',
title: 'Balance ($)',
kind: 'number',
width: 130,
align: 'end',
cellClass: (ctx) => (Number(ctx.value) < 0 ? 'cf-credit' : undefined),
formatValue: (value) => Number(value).toLocaleString('en-US'),
},
],
rows: [
{ id: 1, invoice: 'INV-2401', first: 'Liam', last: 'Carter', status: 'Paid', balance: 0 },
{
id: 2,
invoice: 'INV-2402',
first: 'Emma',
last: 'Stone',
status: 'Overdue',
balance: 1280,
},
{
id: 3,
invoice: 'INV-2403',
first: 'Noah',
last: 'Parker',
status: 'Waiting',
balance: 455,
},
{
id: 4,
invoice: 'INV-2404',
first: 'Olivia',
last: 'Reed',
status: 'Refunded',
balance: -98,
},
{
id: 5,
invoice: 'INV-2405',
first: 'Ethan',
last: 'Brooks',
status: 'Overdue',
balance: 760,
},
{
id: 6,
invoice: 'INV-2406',
first: 'Sophia',
last: 'Hayes',
status: 'Waiting',
balance: 320,
},
],
rowKey: 'id',
// Turn off the built-in stripes and toggle only the stripes from rowClass below (otherwise stripes stay even when the select box says 'off').
striped: false,
rowClass: (row, index) => {
const classes: string[] = [];
if (striped && index % 2 === 1) classes.push('cf-stripe');
if (row.status === 'Overdue') classes.push('cf-overdue');
return classes.join(' ') || undefined;
},
};
const CSS = `
.mxg-row.cf-stripe { background: var(--mxg-bg-row-stripe); }
.mxg-row.cf-overdue { background: var(--mxg-bg-invalid); box-shadow: inset 3px 0 0 var(--mxg-danger); }
.mxg-cell.cf-credit { color: var(--mxg-mark-added); font-weight: 600; }
`;
// The example adds its own style rules and removes them in the cleanup function. Rows are redrawn after the rules exist.
export function setup(api: GridApi<Row>) {
striped = true;
const style = document.createElement('style');
style.textContent = CSS;
document.head.append(style);
api.refresh();
return () => style.remove();
}
export const actions = {
stripes: (api: GridApi<Row>, value?: string) => {
striped = value !== 'off';
api.refresh();
},
sortBalance: (api: GridApi<Row>) =>
api.setSortModel([{ columnId: 'balance', direction: 'desc' }]),
clearSort: (api: GridApi<Row>) => api.setSortModel([]),
};