숫자 입력 규칙과 백분율 편집기Number input rules and the percent editor
editorParams의 decimals·negative·group·scale로 숫자 입력을 키를 누르는 순간 걸러요. cellEditor 'percent'는 0.25를 25%로 보여 주고 저장은 0.25로 해요.editorParams decimals, negative, group and scale filter number input as you type. cellEditor 'percent' shows 0.25 as 25% and stores 0.25.
코드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="editAmount">금액 편집</button>
<button type="button" id="editRate">할인율 편집</button>
</div>
<div id="grid" style="height: 250px"></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('editAmount').addEventListener('click', () => actions.editAmount(api));
document.getElementById('editRate').addEventListener('click', () => actions.editRate(api));const rows = [
{ id: 1, item: '노트북', amount: 1250000.5, qty: 3, rate: 0.25 },
{ id: 2, item: '모니터', amount: 342000, qty: 10, rate: 0.1 },
{ id: 3, item: '키보드', amount: 89000, qty: 25, rate: 0 },
];
// 숫자 입력 규칙은 editorParams에 써요. 규칙을 주지 않은 칸은 지금까지와 똑같아요.
// 금액: 소수 2자리까지, 입력하는 동안 쉼표를 넣어요. 수량: 정수만, 음수 불가.
// 할인율: 'percent' 편집기. 저장 값 0.25가 25로 보이고, 범위(0~1)는 저장 값 단위로 검사해요.
export const options = {
columns: [
{ id: 'item', title: '품목', width: 110 },
{
id: 'amount',
title: '금액',
kind: 'number',
width: 150,
align: 'end',
editable: true,
editorParams: { decimals: 2, group: true },
},
{
id: 'qty',
title: '수량',
kind: 'number',
width: 100,
align: 'end',
editable: true,
editorParams: { decimals: 0, negative: false },
},
{
id: 'rate',
title: '할인율',
kind: 'number',
width: 110,
align: 'end',
editable: true,
cellEditor: 'percent',
validation: { min: 0, max: 1 },
formatValue: (value) => typeof value === 'number' ? `${Math.round(value * 10000) / 100}%` : '',
},
],
rows,
rowKey: 'id',
};
export function setup(api, log) {
log({
ko: '"금액"을 더블클릭하고 숫자를 써 보세요. 소수는 2자리까지, 쉼표는 자동으로 들어가요.',
en: 'Double-click "Amount" and type a number. Decimals stop at 2 and commas are inserted for you.',
});
const offs = [
api.on('editCommitted', (e) => log(`${e.columnId}: ${String(e.value)}`)),
api.on('validationFailed', (e) => log(`${e.columnId}: ${e.message}`)),
];
return () => offs.forEach((off) => off());
}
export const actions = {
editAmount: (api) => api.startEdit(1, 'amount'),
editRate: (api) => api.startEdit(1, 'rate'),
};<!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="editAmount">금액 편집</button>
<button type="button" id="editRate">할인율 편집</button>
</div>
<div id="grid" style="height: 250px"></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: '노트북', amount: 1250000.5, qty: 3, rate: 0.25 },
{ id: 2, item: '모니터', amount: 342000, qty: 10, rate: 0.1 },
{ id: 3, item: '키보드', amount: 89000, qty: 25, rate: 0 },
];
// 숫자 입력 규칙은 editorParams에 써요. 규칙을 주지 않은 칸은 지금까지와 똑같아요.
// 금액: 소수 2자리까지, 입력하는 동안 쉼표를 넣어요. 수량: 정수만, 음수 불가.
// 할인율: 'percent' 편집기. 저장 값 0.25가 25로 보이고, 범위(0~1)는 저장 값 단위로 검사해요.
const options = {
columns: [
{ id: 'item', title: '품목', width: 110 },
{
id: 'amount',
title: '금액',
kind: 'number',
width: 150,
align: 'end',
editable: true,
editorParams: { decimals: 2, group: true },
},
{
id: 'qty',
title: '수량',
kind: 'number',
width: 100,
align: 'end',
editable: true,
editorParams: { decimals: 0, negative: false },
},
{
id: 'rate',
title: '할인율',
kind: 'number',
width: 110,
align: 'end',
editable: true,
cellEditor: 'percent',
validation: { min: 0, max: 1 },
formatValue: (value) => typeof value === 'number' ? `${Math.round(value * 10000) / 100}%` : '',
},
],
rows,
rowKey: 'id',
};
function setup(api, log) {
log({
ko: '"금액"을 더블클릭하고 숫자를 써 보세요. 소수는 2자리까지, 쉼표는 자동으로 들어가요.',
en: 'Double-click "Amount" and type a number. Decimals stop at 2 and commas are inserted for you.',
});
const offs = [
api.on('editCommitted', (e) => log(`${e.columnId}: ${String(e.value)}`)),
api.on('validationFailed', (e) => log(`${e.columnId}: ${e.message}`)),
];
return () => offs.forEach((off) => off());
}
const actions = {
editAmount: (api) => api.startEdit(1, 'amount'),
editRate: (api) => api.startEdit(1, 'rate'),
};
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('editAmount').addEventListener('click', () => actions.editAmount(api));
document.getElementById('editRate').addEventListener('click', () => actions.editRate(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.editAmount(api)}
>금액 편집</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.editRate(api)}
>할인율 편집</button>
</div>
<Grid style={{ height: 250 }} 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;
amount: number;
qty: number;
rate: number;
}
const rows: Row[] = [
{ id: 1, item: '노트북', amount: 1250000.5, qty: 3, rate: 0.25 },
{ id: 2, item: '모니터', amount: 342000, qty: 10, rate: 0.1 },
{ id: 3, item: '키보드', amount: 89000, qty: 25, rate: 0 },
];
// 숫자 입력 규칙은 editorParams에 써요. 규칙을 주지 않은 칸은 지금까지와 똑같아요.
// 금액: 소수 2자리까지, 입력하는 동안 쉼표를 넣어요. 수량: 정수만, 음수 불가.
// 할인율: 'percent' 편집기. 저장 값 0.25가 25로 보이고, 범위(0~1)는 저장 값 단위로 검사해요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: '품목', width: 110 },
{
id: 'amount',
title: '금액',
kind: 'number',
width: 150,
align: 'end',
editable: true,
editorParams: { decimals: 2, group: true },
},
{
id: 'qty',
title: '수량',
kind: 'number',
width: 100,
align: 'end',
editable: true,
editorParams: { decimals: 0, negative: false },
},
{
id: 'rate',
title: '할인율',
kind: 'number',
width: 110,
align: 'end',
editable: true,
cellEditor: 'percent',
validation: { min: 0, max: 1 },
formatValue: (value) =>
typeof value === 'number' ? `${Math.round(value * 10000) / 100}%` : '',
},
],
rows,
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
log({
ko: '"금액"을 더블클릭하고 숫자를 써 보세요. 소수는 2자리까지, 쉼표는 자동으로 들어가요.',
en: 'Double-click "Amount" and type a number. Decimals stop at 2 and commas are inserted for you.',
});
const offs = [
api.on('editCommitted', (e) => log(`${e.columnId}: ${String(e.value)}`)),
api.on('validationFailed', (e) => log(`${e.columnId}: ${e.message}`)),
];
return () => offs.forEach((off) => off());
}
export const actions = {
editAmount: (api: GridApi<Row>) => api.startEdit(1, 'amount'),
editRate: (api: GridApi<Row>) => api.startEdit(1, 'rate'),
};버튼을 누르면 이 예제의 코드가 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.editAmount(api)"
>금액 편집</button>
<button
type="button"
:disabled="!api"
@click="api && actions.editRate(api)"
>할인율 편집</button>
</div>
<Grid style="height: 250px" :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;
amount: number;
qty: number;
rate: number;
}
const rows: Row[] = [
{ id: 1, item: '노트북', amount: 1250000.5, qty: 3, rate: 0.25 },
{ id: 2, item: '모니터', amount: 342000, qty: 10, rate: 0.1 },
{ id: 3, item: '키보드', amount: 89000, qty: 25, rate: 0 },
];
// 숫자 입력 규칙은 editorParams에 써요. 규칙을 주지 않은 칸은 지금까지와 똑같아요.
// 금액: 소수 2자리까지, 입력하는 동안 쉼표를 넣어요. 수량: 정수만, 음수 불가.
// 할인율: 'percent' 편집기. 저장 값 0.25가 25로 보이고, 범위(0~1)는 저장 값 단위로 검사해요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: '품목', width: 110 },
{
id: 'amount',
title: '금액',
kind: 'number',
width: 150,
align: 'end',
editable: true,
editorParams: { decimals: 2, group: true },
},
{
id: 'qty',
title: '수량',
kind: 'number',
width: 100,
align: 'end',
editable: true,
editorParams: { decimals: 0, negative: false },
},
{
id: 'rate',
title: '할인율',
kind: 'number',
width: 110,
align: 'end',
editable: true,
cellEditor: 'percent',
validation: { min: 0, max: 1 },
formatValue: (value) =>
typeof value === 'number' ? `${Math.round(value * 10000) / 100}%` : '',
},
],
rows,
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
log({
ko: '"금액"을 더블클릭하고 숫자를 써 보세요. 소수는 2자리까지, 쉼표는 자동으로 들어가요.',
en: 'Double-click "Amount" and type a number. Decimals stop at 2 and commas are inserted for you.',
});
const offs = [
api.on('editCommitted', (e) => log(`${e.columnId}: ${String(e.value)}`)),
api.on('validationFailed', (e) => log(`${e.columnId}: ${e.message}`)),
];
return () => offs.forEach((off) => off());
}
export const actions = {
editAmount: (api: GridApi<Row>) => api.startEdit(1, 'amount'),
editRate: (api: GridApi<Row>) => api.startEdit(1, 'rate'),
};버튼을 누르면 이 예제의 코드가 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.editAmount(api)"
>금액 편집</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.editRate(api)"
>할인율 편집</button>
</div>
<mx-grid style="height: 250px" [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;
amount: number;
qty: number;
rate: number;
}
const rows: Row[] = [
{ id: 1, item: '노트북', amount: 1250000.5, qty: 3, rate: 0.25 },
{ id: 2, item: '모니터', amount: 342000, qty: 10, rate: 0.1 },
{ id: 3, item: '키보드', amount: 89000, qty: 25, rate: 0 },
];
// 숫자 입력 규칙은 editorParams에 써요. 규칙을 주지 않은 칸은 지금까지와 똑같아요.
// 금액: 소수 2자리까지, 입력하는 동안 쉼표를 넣어요. 수량: 정수만, 음수 불가.
// 할인율: 'percent' 편집기. 저장 값 0.25가 25로 보이고, 범위(0~1)는 저장 값 단위로 검사해요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: '품목', width: 110 },
{
id: 'amount',
title: '금액',
kind: 'number',
width: 150,
align: 'end',
editable: true,
editorParams: { decimals: 2, group: true },
},
{
id: 'qty',
title: '수량',
kind: 'number',
width: 100,
align: 'end',
editable: true,
editorParams: { decimals: 0, negative: false },
},
{
id: 'rate',
title: '할인율',
kind: 'number',
width: 110,
align: 'end',
editable: true,
cellEditor: 'percent',
validation: { min: 0, max: 1 },
formatValue: (value) =>
typeof value === 'number' ? `${Math.round(value * 10000) / 100}%` : '',
},
],
rows,
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
log({
ko: '"금액"을 더블클릭하고 숫자를 써 보세요. 소수는 2자리까지, 쉼표는 자동으로 들어가요.',
en: 'Double-click "Amount" and type a number. Decimals stop at 2 and commas are inserted for you.',
});
const offs = [
api.on('editCommitted', (e) => log(`${e.columnId}: ${String(e.value)}`)),
api.on('validationFailed', (e) => log(`${e.columnId}: ${e.message}`)),
];
return () => offs.forEach((off) => off());
}
export const actions = {
editAmount: (api: GridApi<Row>) => api.startEdit(1, 'amount'),
editRate: (api: GridApi<Row>) => api.startEdit(1, 'rate'),
};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="editAmount">Edit amount</button>
<button type="button" id="editRate">Edit discount</button>
</div>
<div id="grid" style="height: 250px"></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('editAmount').addEventListener('click', () => actions.editAmount(api));
document.getElementById('editRate').addEventListener('click', () => actions.editRate(api));const rows = [
{ id: 1, item: 'Laptop', amount: 1250.5, qty: 3, rate: 0.25 },
{ id: 2, item: 'Monitor', amount: 342, qty: 10, rate: 0.1 },
{ id: 3, item: 'Keyboard', amount: 89, qty: 25, rate: 0 },
];
// Number input rules go in editorParams. A cell with no rules behaves as before.
// Amount: up to 2 decimals, commas are inserted while typing. Quantity: integers only, no negatives.
// Discount: the 'percent' editor. The stored value 0.25 is shown as 25, and the range (0-1) is checked in stored-value units.
export const options = {
columns: [
{ id: 'item', title: 'Item', width: 110 },
{
id: 'amount',
title: 'Amount',
kind: 'number',
width: 150,
align: 'end',
editable: true,
editorParams: { decimals: 2, group: true },
},
{
id: 'qty',
title: 'Qty',
kind: 'number',
width: 100,
align: 'end',
editable: true,
editorParams: { decimals: 0, negative: false },
},
{
id: 'rate',
title: 'Discount',
kind: 'number',
width: 110,
align: 'end',
editable: true,
cellEditor: 'percent',
validation: { min: 0, max: 1 },
formatValue: (value) => typeof value === 'number' ? `${Math.round(value * 10000) / 100}%` : '',
},
],
rows,
rowKey: 'id',
};
export function setup(api, log) {
log('Double-click "Amount" and type a number. Decimals stop at 2 and commas are inserted for you.');
const offs = [
api.on('editCommitted', (e) => log(`${e.columnId}: ${String(e.value)}`)),
api.on('validationFailed', (e) => log(`${e.columnId}: ${e.message}`)),
];
return () => offs.forEach((off) => off());
}
export const actions = {
editAmount: (api) => api.startEdit(1, 'amount'),
editRate: (api) => api.startEdit(1, 'rate'),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Number input rules and the percent editor</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="editAmount">Edit amount</button>
<button type="button" id="editRate">Edit discount</button>
</div>
<div id="grid" style="height: 250px"></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', amount: 1250.5, qty: 3, rate: 0.25 },
{ id: 2, item: 'Monitor', amount: 342, qty: 10, rate: 0.1 },
{ id: 3, item: 'Keyboard', amount: 89, qty: 25, rate: 0 },
];
// Number input rules go in editorParams. A cell with no rules behaves as before.
// Amount: up to 2 decimals, commas are inserted while typing. Quantity: integers only, no negatives.
// Discount: the 'percent' editor. The stored value 0.25 is shown as 25, and the range (0-1) is checked in stored-value units.
const options = {
columns: [
{ id: 'item', title: 'Item', width: 110 },
{
id: 'amount',
title: 'Amount',
kind: 'number',
width: 150,
align: 'end',
editable: true,
editorParams: { decimals: 2, group: true },
},
{
id: 'qty',
title: 'Qty',
kind: 'number',
width: 100,
align: 'end',
editable: true,
editorParams: { decimals: 0, negative: false },
},
{
id: 'rate',
title: 'Discount',
kind: 'number',
width: 110,
align: 'end',
editable: true,
cellEditor: 'percent',
validation: { min: 0, max: 1 },
formatValue: (value) => typeof value === 'number' ? `${Math.round(value * 10000) / 100}%` : '',
},
],
rows,
rowKey: 'id',
};
function setup(api, log) {
log('Double-click "Amount" and type a number. Decimals stop at 2 and commas are inserted for you.');
const offs = [
api.on('editCommitted', (e) => log(`${e.columnId}: ${String(e.value)}`)),
api.on('validationFailed', (e) => log(`${e.columnId}: ${e.message}`)),
];
return () => offs.forEach((off) => off());
}
const actions = {
editAmount: (api) => api.startEdit(1, 'amount'),
editRate: (api) => api.startEdit(1, 'rate'),
};
options.ariaLabel = "Number input rules and the percent editor";
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('editAmount').addEventListener('click', () => actions.editAmount(api));
document.getElementById('editRate').addEventListener('click', () => actions.editRate(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.editAmount(api)}
>Edit amount</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.editRate(api)}
>Edit discount</button>
</div>
<Grid style={{ height: 250 }} 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;
amount: number;
qty: number;
rate: number;
}
const rows: Row[] = [
{ id: 1, item: 'Laptop', amount: 1250.5, qty: 3, rate: 0.25 },
{ id: 2, item: 'Monitor', amount: 342, qty: 10, rate: 0.1 },
{ id: 3, item: 'Keyboard', amount: 89, qty: 25, rate: 0 },
];
// Number input rules go in editorParams. A cell with no rules behaves as before.
// Amount: up to 2 decimals, commas are inserted while typing. Quantity: integers only, no negatives.
// Discount: the 'percent' editor. The stored value 0.25 is shown as 25, and the range (0-1) is checked in stored-value units.
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: 'Item', width: 110 },
{
id: 'amount',
title: 'Amount',
kind: 'number',
width: 150,
align: 'end',
editable: true,
editorParams: { decimals: 2, group: true },
},
{
id: 'qty',
title: 'Qty',
kind: 'number',
width: 100,
align: 'end',
editable: true,
editorParams: { decimals: 0, negative: false },
},
{
id: 'rate',
title: 'Discount',
kind: 'number',
width: 110,
align: 'end',
editable: true,
cellEditor: 'percent',
validation: { min: 0, max: 1 },
formatValue: (value) =>
typeof value === 'number' ? `${Math.round(value * 10000) / 100}%` : '',
},
],
rows,
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
log(
'Double-click "Amount" and type a number. Decimals stop at 2 and commas are inserted for you.',
);
const offs = [
api.on('editCommitted', (e) => log(`${e.columnId}: ${String(e.value)}`)),
api.on('validationFailed', (e) => log(`${e.columnId}: ${e.message}`)),
];
return () => offs.forEach((off) => off());
}
export const actions = {
editAmount: (api: GridApi<Row>) => api.startEdit(1, 'amount'),
editRate: (api: GridApi<Row>) => api.startEdit(1, 'rate'),
};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.editAmount(api)"
>Edit amount</button>
<button
type="button"
:disabled="!api"
@click="api && actions.editRate(api)"
>Edit discount</button>
</div>
<Grid style="height: 250px" :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;
amount: number;
qty: number;
rate: number;
}
const rows: Row[] = [
{ id: 1, item: 'Laptop', amount: 1250.5, qty: 3, rate: 0.25 },
{ id: 2, item: 'Monitor', amount: 342, qty: 10, rate: 0.1 },
{ id: 3, item: 'Keyboard', amount: 89, qty: 25, rate: 0 },
];
// Number input rules go in editorParams. A cell with no rules behaves as before.
// Amount: up to 2 decimals, commas are inserted while typing. Quantity: integers only, no negatives.
// Discount: the 'percent' editor. The stored value 0.25 is shown as 25, and the range (0-1) is checked in stored-value units.
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: 'Item', width: 110 },
{
id: 'amount',
title: 'Amount',
kind: 'number',
width: 150,
align: 'end',
editable: true,
editorParams: { decimals: 2, group: true },
},
{
id: 'qty',
title: 'Qty',
kind: 'number',
width: 100,
align: 'end',
editable: true,
editorParams: { decimals: 0, negative: false },
},
{
id: 'rate',
title: 'Discount',
kind: 'number',
width: 110,
align: 'end',
editable: true,
cellEditor: 'percent',
validation: { min: 0, max: 1 },
formatValue: (value) =>
typeof value === 'number' ? `${Math.round(value * 10000) / 100}%` : '',
},
],
rows,
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
log(
'Double-click "Amount" and type a number. Decimals stop at 2 and commas are inserted for you.',
);
const offs = [
api.on('editCommitted', (e) => log(`${e.columnId}: ${String(e.value)}`)),
api.on('validationFailed', (e) => log(`${e.columnId}: ${e.message}`)),
];
return () => offs.forEach((off) => off());
}
export const actions = {
editAmount: (api: GridApi<Row>) => api.startEdit(1, 'amount'),
editRate: (api: GridApi<Row>) => api.startEdit(1, 'rate'),
};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.editAmount(api)"
>Edit amount</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.editRate(api)"
>Edit discount</button>
</div>
<mx-grid style="height: 250px" [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;
amount: number;
qty: number;
rate: number;
}
const rows: Row[] = [
{ id: 1, item: 'Laptop', amount: 1250.5, qty: 3, rate: 0.25 },
{ id: 2, item: 'Monitor', amount: 342, qty: 10, rate: 0.1 },
{ id: 3, item: 'Keyboard', amount: 89, qty: 25, rate: 0 },
];
// Number input rules go in editorParams. A cell with no rules behaves as before.
// Amount: up to 2 decimals, commas are inserted while typing. Quantity: integers only, no negatives.
// Discount: the 'percent' editor. The stored value 0.25 is shown as 25, and the range (0-1) is checked in stored-value units.
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: 'Item', width: 110 },
{
id: 'amount',
title: 'Amount',
kind: 'number',
width: 150,
align: 'end',
editable: true,
editorParams: { decimals: 2, group: true },
},
{
id: 'qty',
title: 'Qty',
kind: 'number',
width: 100,
align: 'end',
editable: true,
editorParams: { decimals: 0, negative: false },
},
{
id: 'rate',
title: 'Discount',
kind: 'number',
width: 110,
align: 'end',
editable: true,
cellEditor: 'percent',
validation: { min: 0, max: 1 },
formatValue: (value) =>
typeof value === 'number' ? `${Math.round(value * 10000) / 100}%` : '',
},
],
rows,
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
log(
'Double-click "Amount" and type a number. Decimals stop at 2 and commas are inserted for you.',
);
const offs = [
api.on('editCommitted', (e) => log(`${e.columnId}: ${String(e.value)}`)),
api.on('validationFailed', (e) => log(`${e.columnId}: ${e.message}`)),
];
return () => offs.forEach((off) => off());
}
export const actions = {
editAmount: (api: GridApi<Row>) => api.startEdit(1, 'amount'),
editRate: (api: GridApi<Row>) => api.startEdit(1, 'rate'),
};