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

슬롯을 직접 정하는 마스크와 applyMaskCustom slots and applyMask

slot으로 슬롯 코드를 더하면 16진수 색 코드 같은 모양도 만들 수 있어요. overwrite는 글자를 끼워 넣지 않고 덮어쓰고, placeholderChar로 빈 칸 글자를 바꿔요. applyMask와 stripMask는 편집기 밖에서도 같은 규칙으로 쓸 수 있어요.A slot code added with slot makes shapes such as a hex color code possible. overwrite replaces characters instead of inserting, and placeholderChar changes the blank mark. applyMask and stripMask apply the same rules outside the editor.

활용Intermediate
직접 써 보기Live preview슬롯을 직접 정하는 마스크와 applyMaskCustom slots and applyMask

코드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 id="grid" style="height: 200px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
example.js
const rows = [
  { id: 1, color: 'ff-80-00', serial: 'AB12-CD34', raw: '01012345678' },
  { id: 2, color: '', serial: '', raw: '0212345678' },
];
// slot: 슬롯 코드를 더해요. 값은 글자 하나에 맞는 정규식 원문이에요(H = 16진수 한 자리).
// overwrite: 글자를 끼워 넣지 않고 캐럿 자리를 바꿔요. placeholderChar: 입력하는 동안 빈 칸을 보여 주는 글자예요.
export const options = {
  columns: [
    {
      id: 'color',
      title: '색(16진수)',
      width: 150,
      editable: true,
      cellEditor: 'mask',
      editorParams: { pattern: 'HH-HH-HH', slot: { H: '[0-9a-fA-F]' } },
    },
    {
      id: 'serial',
      title: '시리얼(덮어쓰기)',
      width: 170,
      editable: true,
      cellEditor: 'mask',
      editorParams: { pattern: '**\\*-**\\*', overwrite: true, placeholderChar: '·' },
    },
    { id: 'raw', title: '숫자(저장)', width: 130 },
    {
      id: 'shown',
      title: '표시',
      width: 150,
      // 저장된 숫자만 applyMask로 전화 모양으로 보여 줘요.
      getValue: (row) => row.raw,
      formatValue: (value) => MaxGrid.applyMask(MaxGrid.stripMask(String(value ?? ''), '###########'), '###-####-####'),
    },
  ],
  rows,
  rowKey: 'id',
};
export function setup(api, log) {
  log({
    ko: '"색"과 "시리얼" 칸을 두 번 눌러 보세요. "표시"는 applyMask로 만든 글자예요.',
    en: 'Double-click a "색" (color) or "시리얼" (serial) cell. "표시" (shown) is text made by applyMask.',
  });
  const off = api.on('editCommitted', (e) => log({
    ko: `행 ${e.rowKey}: ${e.columnId} = "${String(e.newValue)}"`,
    en: `Row ${e.rowKey}: ${e.columnId} = "${String(e.newValue)}"`,
  }));
  return () => off();
}

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 id="grid" style="height: 200px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
example.js
const rows = [
  { id: 1, color: 'ff-80-00', serial: 'AB12-CD34', raw: '01012345678' },
  { id: 2, color: '', serial: '', raw: '0212345678' },
];
// slot: adds a slot code. The value is the source of a regular expression that matches one character (H = one hex digit).
// overwrite: replaces the character at the caret instead of inserting. placeholderChar: the character that marks blanks while typing.
export const options = {
  columns: [
    {
      id: 'color',
      title: 'Color (hex)',
      width: 150,
      editable: true,
      cellEditor: 'mask',
      editorParams: { pattern: 'HH-HH-HH', slot: { H: '[0-9a-fA-F]' } },
    },
    {
      id: 'serial',
      title: 'Serial (overwrite)',
      width: 170,
      editable: true,
      cellEditor: 'mask',
      editorParams: { pattern: '**\\*-**\\*', overwrite: true, placeholderChar: '·' },
    },
    { id: 'raw', title: 'Digits (stored)', width: 130 },
    {
      id: 'shown',
      title: 'Shown',
      width: 150,
      // Shows only the stored digits in a phone shape with applyMask.
      getValue: (row) => row.raw,
      formatValue: (value) => MaxGrid.applyMask(MaxGrid.stripMask(String(value ?? ''), '###########'), '###-####-####'),
    },
  ],
  rows,
  rowKey: 'id',
};
export function setup(api, log) {
  log('Double-click a "Color" or "Serial" cell. "Shown" is text made by applyMask.');
  const off = api.on('editCommitted', (e) => log(`Row ${e.rowKey}: ${e.columnId} = "${String(e.newValue)}"`));
  return () => off();
}