숫자 표시 형식 정하기Set the number display format
컬럼의 format.number로 소수 자릿수, 천 단위 구분, 접두·접미, 반올림 방식, 음수 모양, 빈 값 글자를 선언해요. 값은 그대로이고 보이는 글자만 바뀌어요.format.number on a column declares decimals, thousands separator, prefix and suffix, rounding mode, negative style and the text for empty values. The value is unchanged; only the shown text differs.
코드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">
<button type="button" id="round">내림 2자리</button>
<button type="button" id="reset">처음 형식</button>
</div>
<div id="grid" style="height: 230px"></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, console.log);
document.getElementById('round').addEventListener('click', () => actions.round(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));const rows = [
{ id: 1, item: '노트북', price: 1234567.891, delta: 12.345 },
{ id: 2, item: '마우스', price: 25900, delta: -1234.5 },
{ id: 3, item: '케이블', price: 1.005, delta: -0.004 },
{ id: 4, item: '서비스', price: 0, delta: null },
];
// 컬럼의 format.number 한 곳에 자릿수·천 단위·접두·접미·반올림·음수 모양을 모아 써요.
// 금액은 원 단위 천 단위 구분, 증감은 소수 1자리에 음수는 괄호, 값이 없으면 '-'를 보여요.
export const options = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'item', title: '품목', width: 110 },
{
id: 'price',
title: '금액',
kind: 'number',
width: 150,
format: { number: { decimals: 0, grouping: true, suffix: '원', rounding: 'round' } },
},
{
id: 'delta',
title: '증감',
kind: 'number',
width: 120,
format: { number: { decimals: 1, negative: 'parens', suffix: '%', emptyText: '-' } },
},
],
rows,
rowKey: 'id',
};
export function setup(_api, log) {
log({
ko: '1.005는 소수 자릿수를 줄여도 오차 없이 반올림해요. 음수는 괄호로 보여요.',
en: '1.005 rounds without float errors. Negative numbers show in parentheses.',
});
return () => undefined;
}
export const actions = {
round: (api) => api.setColumns(options.columns.map((c) => c.id === 'price'
? {
...c,
format: { number: { decimals: 2, grouping: true, rounding: 'floor' } },
}
: c)),
reset: (api) => api.setColumns(options.columns),
};<!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">
<button type="button" id="round">내림 2자리</button>
<button type="button" id="reset">처음 형식</button>
</div>
<div id="grid" style="height: 230px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const rows = [
{ id: 1, item: '노트북', price: 1234567.891, delta: 12.345 },
{ id: 2, item: '마우스', price: 25900, delta: -1234.5 },
{ id: 3, item: '케이블', price: 1.005, delta: -0.004 },
{ id: 4, item: '서비스', price: 0, delta: null },
];
// 컬럼의 format.number 한 곳에 자릿수·천 단위·접두·접미·반올림·음수 모양을 모아 써요.
// 금액은 원 단위 천 단위 구분, 증감은 소수 1자리에 음수는 괄호, 값이 없으면 '-'를 보여요.
const options = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'item', title: '품목', width: 110 },
{
id: 'price',
title: '금액',
kind: 'number',
width: 150,
format: { number: { decimals: 0, grouping: true, suffix: '원', rounding: 'round' } },
},
{
id: 'delta',
title: '증감',
kind: 'number',
width: 120,
format: { number: { decimals: 1, negative: 'parens', suffix: '%', emptyText: '-' } },
},
],
rows,
rowKey: 'id',
};
function setup(_api, log) {
log({
ko: '1.005는 소수 자릿수를 줄여도 오차 없이 반올림해요. 음수는 괄호로 보여요.',
en: '1.005 rounds without float errors. Negative numbers show in parentheses.',
});
return () => undefined;
}
const actions = {
round: (api) => api.setColumns(options.columns.map((c) => c.id === 'price'
? {
...c,
format: { number: { decimals: 2, grouping: true, rounding: 'floor' } },
}
: c)),
reset: (api) => api.setColumns(options.columns),
};
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);
});
document.getElementById('round').addEventListener('click', () => actions.round(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(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, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<button
type="button"
disabled={!api}
onClick={() => api && actions.round(api)}
>내림 2자리</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.reset(api)}
>처음 형식</button>
</div>
<Grid style={{ height: 230 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
item: string;
price: number;
delta: number | null;
}
const rows: Row[] = [
{ id: 1, item: '노트북', price: 1234567.891, delta: 12.345 },
{ id: 2, item: '마우스', price: 25900, delta: -1234.5 },
{ id: 3, item: '케이블', price: 1.005, delta: -0.004 },
{ id: 4, item: '서비스', price: 0, delta: null },
];
// 컬럼의 format.number 한 곳에 자릿수·천 단위·접두·접미·반올림·음수 모양을 모아 써요.
// 금액은 원 단위 천 단위 구분, 증감은 소수 1자리에 음수는 괄호, 값이 없으면 '-'를 보여요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'item', title: '품목', width: 110 },
{
id: 'price',
title: '금액',
kind: 'number',
width: 150,
format: { number: { decimals: 0, grouping: true, suffix: '원', rounding: 'round' } },
},
{
id: 'delta',
title: '증감',
kind: 'number',
width: 120,
format: { number: { decimals: 1, negative: 'parens', suffix: '%', emptyText: '-' } },
},
],
rows,
rowKey: 'id',
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
log({
ko: '1.005는 소수 자릿수를 줄여도 오차 없이 반올림해요. 음수는 괄호로 보여요.',
en: '1.005 rounds without float errors. Negative numbers show in parentheses.',
});
return () => undefined;
}
export const actions = {
round: (api: GridApi<Row>) =>
api.setColumns(
options.columns.map((c) =>
c.id === 'price'
? {
...c,
format: { number: { decimals: 2, grouping: true, rounding: 'floor' as const } },
}
: c,
),
),
reset: (api: GridApi<Row>) => api.setColumns(options.columns),
};버튼을 누르면 이 예제의 코드가 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, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<button type="button" :disabled="!api" @click="api && actions.round(api)">내림 2자리</button>
<button type="button" :disabled="!api" @click="api && actions.reset(api)">처음 형식</button>
</div>
<Grid style="height: 230px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
item: string;
price: number;
delta: number | null;
}
const rows: Row[] = [
{ id: 1, item: '노트북', price: 1234567.891, delta: 12.345 },
{ id: 2, item: '마우스', price: 25900, delta: -1234.5 },
{ id: 3, item: '케이블', price: 1.005, delta: -0.004 },
{ id: 4, item: '서비스', price: 0, delta: null },
];
// 컬럼의 format.number 한 곳에 자릿수·천 단위·접두·접미·반올림·음수 모양을 모아 써요.
// 금액은 원 단위 천 단위 구분, 증감은 소수 1자리에 음수는 괄호, 값이 없으면 '-'를 보여요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'item', title: '품목', width: 110 },
{
id: 'price',
title: '금액',
kind: 'number',
width: 150,
format: { number: { decimals: 0, grouping: true, suffix: '원', rounding: 'round' } },
},
{
id: 'delta',
title: '증감',
kind: 'number',
width: 120,
format: { number: { decimals: 1, negative: 'parens', suffix: '%', emptyText: '-' } },
},
],
rows,
rowKey: 'id',
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
log({
ko: '1.005는 소수 자릿수를 줄여도 오차 없이 반올림해요. 음수는 괄호로 보여요.',
en: '1.005 rounds without float errors. Negative numbers show in parentheses.',
});
return () => undefined;
}
export const actions = {
round: (api: GridApi<Row>) =>
api.setColumns(
options.columns.map((c) =>
c.id === 'price'
? {
...c,
format: { number: { decimals: 2, grouping: true, rounding: 'floor' as const } },
}
: c,
),
),
reset: (api: GridApi<Row>) => api.setColumns(options.columns),
};버튼을 누르면 이 예제의 코드가 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">
<button
type="button"
[disabled]="!api"
(click)="api && actions.round(api)"
>내림 2자리</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.reset(api)"
>처음 형식</button>
</div>
<mx-grid style="height: 230px" [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, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
item: string;
price: number;
delta: number | null;
}
const rows: Row[] = [
{ id: 1, item: '노트북', price: 1234567.891, delta: 12.345 },
{ id: 2, item: '마우스', price: 25900, delta: -1234.5 },
{ id: 3, item: '케이블', price: 1.005, delta: -0.004 },
{ id: 4, item: '서비스', price: 0, delta: null },
];
// 컬럼의 format.number 한 곳에 자릿수·천 단위·접두·접미·반올림·음수 모양을 모아 써요.
// 금액은 원 단위 천 단위 구분, 증감은 소수 1자리에 음수는 괄호, 값이 없으면 '-'를 보여요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'item', title: '품목', width: 110 },
{
id: 'price',
title: '금액',
kind: 'number',
width: 150,
format: { number: { decimals: 0, grouping: true, suffix: '원', rounding: 'round' } },
},
{
id: 'delta',
title: '증감',
kind: 'number',
width: 120,
format: { number: { decimals: 1, negative: 'parens', suffix: '%', emptyText: '-' } },
},
],
rows,
rowKey: 'id',
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
log({
ko: '1.005는 소수 자릿수를 줄여도 오차 없이 반올림해요. 음수는 괄호로 보여요.',
en: '1.005 rounds without float errors. Negative numbers show in parentheses.',
});
return () => undefined;
}
export const actions = {
round: (api: GridApi<Row>) =>
api.setColumns(
options.columns.map((c) =>
c.id === 'price'
? {
...c,
format: { number: { decimals: 2, grouping: true, rounding: 'floor' as const } },
}
: c,
),
),
reset: (api: GridApi<Row>) => api.setColumns(options.columns),
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<div id="app">
<div class="toolbar">
<button type="button" id="round">Floor, 2 decimals</button>
<button type="button" id="reset">Original format</button>
</div>
<div id="grid" style="height: 230px"></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, console.log);
document.getElementById('round').addEventListener('click', () => actions.round(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));const rows = [
{ id: 1, item: 'Laptop', price: 1234.568, delta: 12.345 },
{ id: 2, item: 'Mouse', price: 25.9, delta: -1234.5 },
{ id: 3, item: 'Cable', price: 1.005, delta: -0.004 },
{ id: 4, item: 'Service', price: 0, delta: null },
];
// format.number on a column gathers decimals, thousands separator, prefix, suffix, rounding and negative style in one place.
// Amounts show a $ prefix with thousands separators, changes show 1 decimal with negatives in parentheses, and an empty value shows '-'.
export const options = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'item', title: 'Item', width: 110 },
{
id: 'price',
title: 'Amount',
kind: 'number',
width: 150,
format: { number: { decimals: 0, grouping: true, prefix: '$', rounding: 'round' } },
},
{
id: 'delta',
title: 'Change',
kind: 'number',
width: 120,
format: { number: { decimals: 1, negative: 'parens', suffix: '%', emptyText: '-' } },
},
],
rows,
rowKey: 'id',
};
export function setup(_api, log) {
log('1.005 rounds without float errors. Negative numbers show in parentheses.');
return () => undefined;
}
export const actions = {
round: (api) => api.setColumns(options.columns.map((c) => c.id === 'price'
? {
...c,
format: {
number: { decimals: 2, grouping: true, prefix: '$', rounding: 'floor' },
},
}
: c)),
reset: (api) => api.setColumns(options.columns),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Set the number display format</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="round">Floor, 2 decimals</button>
<button type="button" id="reset">Original format</button>
</div>
<div id="grid" style="height: 230px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const rows = [
{ id: 1, item: 'Laptop', price: 1234.568, delta: 12.345 },
{ id: 2, item: 'Mouse', price: 25.9, delta: -1234.5 },
{ id: 3, item: 'Cable', price: 1.005, delta: -0.004 },
{ id: 4, item: 'Service', price: 0, delta: null },
];
// format.number on a column gathers decimals, thousands separator, prefix, suffix, rounding and negative style in one place.
// Amounts show a $ prefix with thousands separators, changes show 1 decimal with negatives in parentheses, and an empty value shows '-'.
const options = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'item', title: 'Item', width: 110 },
{
id: 'price',
title: 'Amount',
kind: 'number',
width: 150,
format: { number: { decimals: 0, grouping: true, prefix: '$', rounding: 'round' } },
},
{
id: 'delta',
title: 'Change',
kind: 'number',
width: 120,
format: { number: { decimals: 1, negative: 'parens', suffix: '%', emptyText: '-' } },
},
],
rows,
rowKey: 'id',
};
function setup(_api, log) {
log('1.005 rounds without float errors. Negative numbers show in parentheses.');
return () => undefined;
}
const actions = {
round: (api) => api.setColumns(options.columns.map((c) => c.id === 'price'
? {
...c,
format: {
number: { decimals: 2, grouping: true, prefix: '$', rounding: 'floor' },
},
}
: c)),
reset: (api) => api.setColumns(options.columns),
};
options.ariaLabel = "Set the number display format";
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);
});
document.getElementById('round').addEventListener('click', () => actions.round(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(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, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<button
type="button"
disabled={!api}
onClick={() => api && actions.round(api)}
>Floor, 2 decimals</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.reset(api)}
>Original format</button>
</div>
<Grid style={{ height: 230 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
item: string;
price: number;
delta: number | null;
}
const rows: Row[] = [
{ id: 1, item: 'Laptop', price: 1234.568, delta: 12.345 },
{ id: 2, item: 'Mouse', price: 25.9, delta: -1234.5 },
{ id: 3, item: 'Cable', price: 1.005, delta: -0.004 },
{ id: 4, item: 'Service', price: 0, delta: null },
];
// format.number on a column gathers decimals, thousands separator, prefix, suffix, rounding and negative style in one place.
// Amounts show a $ prefix with thousands separators, changes show 1 decimal with negatives in parentheses, and an empty value shows '-'.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'item', title: 'Item', width: 110 },
{
id: 'price',
title: 'Amount',
kind: 'number',
width: 150,
format: { number: { decimals: 0, grouping: true, prefix: '$', rounding: 'round' } },
},
{
id: 'delta',
title: 'Change',
kind: 'number',
width: 120,
format: { number: { decimals: 1, negative: 'parens', suffix: '%', emptyText: '-' } },
},
],
rows,
rowKey: 'id',
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
log('1.005 rounds without float errors. Negative numbers show in parentheses.');
return () => undefined;
}
export const actions = {
round: (api: GridApi<Row>) =>
api.setColumns(
options.columns.map((c) =>
c.id === 'price'
? {
...c,
format: {
number: { decimals: 2, grouping: true, prefix: '$', rounding: 'floor' as const },
},
}
: c,
),
),
reset: (api: GridApi<Row>) => api.setColumns(options.columns),
};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, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<button type="button" :disabled="!api" @click="api && actions.round(api)">Floor, 2 decimals</button>
<button type="button" :disabled="!api" @click="api && actions.reset(api)">Original format</button>
</div>
<Grid style="height: 230px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
item: string;
price: number;
delta: number | null;
}
const rows: Row[] = [
{ id: 1, item: 'Laptop', price: 1234.568, delta: 12.345 },
{ id: 2, item: 'Mouse', price: 25.9, delta: -1234.5 },
{ id: 3, item: 'Cable', price: 1.005, delta: -0.004 },
{ id: 4, item: 'Service', price: 0, delta: null },
];
// format.number on a column gathers decimals, thousands separator, prefix, suffix, rounding and negative style in one place.
// Amounts show a $ prefix with thousands separators, changes show 1 decimal with negatives in parentheses, and an empty value shows '-'.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'item', title: 'Item', width: 110 },
{
id: 'price',
title: 'Amount',
kind: 'number',
width: 150,
format: { number: { decimals: 0, grouping: true, prefix: '$', rounding: 'round' } },
},
{
id: 'delta',
title: 'Change',
kind: 'number',
width: 120,
format: { number: { decimals: 1, negative: 'parens', suffix: '%', emptyText: '-' } },
},
],
rows,
rowKey: 'id',
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
log('1.005 rounds without float errors. Negative numbers show in parentheses.');
return () => undefined;
}
export const actions = {
round: (api: GridApi<Row>) =>
api.setColumns(
options.columns.map((c) =>
c.id === 'price'
? {
...c,
format: {
number: { decimals: 2, grouping: true, prefix: '$', rounding: 'floor' as const },
},
}
: c,
),
),
reset: (api: GridApi<Row>) => api.setColumns(options.columns),
};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">
<button
type="button"
[disabled]="!api"
(click)="api && actions.round(api)"
>Floor, 2 decimals</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.reset(api)"
>Original format</button>
</div>
<mx-grid style="height: 230px" [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, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
item: string;
price: number;
delta: number | null;
}
const rows: Row[] = [
{ id: 1, item: 'Laptop', price: 1234.568, delta: 12.345 },
{ id: 2, item: 'Mouse', price: 25.9, delta: -1234.5 },
{ id: 3, item: 'Cable', price: 1.005, delta: -0.004 },
{ id: 4, item: 'Service', price: 0, delta: null },
];
// format.number on a column gathers decimals, thousands separator, prefix, suffix, rounding and negative style in one place.
// Amounts show a $ prefix with thousands separators, changes show 1 decimal with negatives in parentheses, and an empty value shows '-'.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'item', title: 'Item', width: 110 },
{
id: 'price',
title: 'Amount',
kind: 'number',
width: 150,
format: { number: { decimals: 0, grouping: true, prefix: '$', rounding: 'round' } },
},
{
id: 'delta',
title: 'Change',
kind: 'number',
width: 120,
format: { number: { decimals: 1, negative: 'parens', suffix: '%', emptyText: '-' } },
},
],
rows,
rowKey: 'id',
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
log('1.005 rounds without float errors. Negative numbers show in parentheses.');
return () => undefined;
}
export const actions = {
round: (api: GridApi<Row>) =>
api.setColumns(
options.columns.map((c) =>
c.id === 'price'
? {
...c,
format: {
number: { decimals: 2, grouping: true, prefix: '$', rounding: 'floor' as const },
},
}
: c,
),
),
reset: (api: GridApi<Row>) => api.setColumns(options.columns),
};