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

입력한 글자를 값으로 바꾸는 parseTurn typed text into a value with parse

컬럼의 parse(raw, row)가 직접 입력과 붙여넣기의 글자를 값으로 바꿔요. 값을 돌려주면 쓰이고, 예외를 던지거나 값을 돌려주지 않으면 값이 올바르지 않다고 막혀요.The column parse(raw, row) turns typed and pasted text into a value. A returned value is used; throwing or returning nothing refuses the input as invalid.

활용Intermediate
직접 써 보기Live preview입력한 글자를 값으로 바꾸는 parseTurn typed text into a value with parse

코드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="edit">가격 편집</button>
    <button type="button" id="paste">가격 붙여넣기</button>
  </div>
  <div id="grid" style="height: 210px"></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('edit').addEventListener('click', () => actions.edit(api));
document.getElementById('paste').addEventListener('click', () => actions.paste(api));
example.js
const rows = [
  { id: 1, item: '커피', price: 4500 },
  { id: 2, item: '샌드위치', price: 6800 },
  { id: 3, item: '주스', price: 5200 },
];
// parse는 입력한 글자를 값으로 바꿔요. '4,500원'이나 '5천'을 숫자로 읽고, 읽을 수 없으면 값을 돌려주지 않아 거절해요.
const readWon = (raw) => {
  const text = raw.replace(/[,\s원]/g, '');
  const thousand = /^(\d+(?:\.\d+)?)천$/.exec(text);
  const value = thousand ? Number(thousand[1]) * 1000 : Number(text);
  return Number.isFinite(value) ? value : undefined;
};
export const options = {
  columns: [
    { id: 'id', title: '번호', kind: 'number', width: 70 },
    { id: 'item', title: '품목', width: 120 },
    {
      id: 'price',
      title: '가격',
      kind: 'number',
      width: 140,
      editable: true,
      parse: readWon,
      format: { number: { grouping: true, suffix: '원' } },
    },
  ],
  rows,
  rowKey: 'id',
  cellSelection: 'multiple',
};
export function setup(api, log) {
  const off = [
    api.on('editCommitted', (e) => log({ ko: `확정: ${String(e.newValue)}`, en: `Committed: ${String(e.newValue)}` })),
    api.on('validationFailed', (e) => log(`${e.columnId}: ${e.message}`)),
  ];
  return () => off.forEach((fn) => fn());
}
export const actions = {
  edit: (api) => api.startEdit(1, 'price'),
  paste: (api) => {
    api.selectCells({ rowKey: 2, columnId: 'price' });
    api.pasteCells('7,300원\n5천\n한글');
  },
};

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="edit">Edit price</button>
    <button type="button" id="paste">Paste prices</button>
  </div>
  <div id="grid" style="height: 210px"></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('edit').addEventListener('click', () => actions.edit(api));
document.getElementById('paste').addEventListener('click', () => actions.paste(api));
example.js
const rows = [
  { id: 1, item: 'Coffee', price: 4.5 },
  { id: 2, item: 'Sandwich', price: 6.8 },
  { id: 3, item: 'Juice', price: 5.2 },
];
// parse turns typed text into a value. It reads '$4.50' or '7.3' as numbers, and refuses by returning nothing when the text cannot be read.
const readPrice = (raw) => {
  const text = raw.replace(/[,\s$]/g, '');
  const value = Number(text);
  return Number.isFinite(value) ? value : undefined;
};
export const options = {
  columns: [
    { id: 'id', title: 'No.', kind: 'number', width: 70 },
    { id: 'item', title: 'Item', width: 120 },
    {
      id: 'price',
      title: 'Price',
      kind: 'number',
      width: 140,
      editable: true,
      parse: readPrice,
      format: { number: { grouping: true, prefix: '$', decimals: 2 } },
    },
  ],
  rows,
  rowKey: 'id',
  cellSelection: 'multiple',
};
export function setup(api, log) {
  const off = [
    api.on('editCommitted', (e) => log(`Committed: ${String(e.newValue)}`)),
    api.on('validationFailed', (e) => log(`${e.columnId}: ${e.message}`)),
  ];
  return () => off.forEach((fn) => fn());
}
export const actions = {
  edit: (api) => api.startEdit(1, 'price'),
  paste: (api) => {
    api.selectCells({ rowKey: 2, columnId: 'price' });
    api.pasteCells('$7.30\n5\nabc');
  },
};