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

숫자 입력 규칙과 백분율 편집기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.

활용Intermediate
직접 써 보기Live preview숫자 입력 규칙과 백분율 편집기Number input rules and the percent editor

코드Code

탭을 골라 코드를 복사하세요. 위 화면은 이 코드를 그대로 실행한 결과예요.Pick a tab and copy the code. The preview above runs exactly this code.

이 화면은 새 모양 옵션을 켠 모양이에요: density: 'spacious', iconStyle: 'soft', motion: true (코드에 없으면 기본 모양으로 보여요)This preview has the new-look options on: density: 'spacious', iconStyle: 'soft', motion: true (without them in your code you get the default look)

빌드 도구(Vite 등)를 쓰는 프로젝트용 모듈 파일이에요. 설치 없이 바로 돌려 보려면 HTML 탭이나 직접 해보기를 쓰세요.

버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.

index.html
<div id="app">
  <div class="toolbar">
    <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>
main.js
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));
example.js
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'),
};

Module files for a project with a bundler such as Vite. To run it with no setup, use the HTML tab or Try it.

When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.

index.html
<div id="app">
  <div class="toolbar">
    <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>
main.js
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));
example.js
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'),
};