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

색 칸 편집: 견본과 16진 입력Edit colors: swatches and hex input

cellEditor: 'color'는 견본 16개와 16진 입력칸을 열어요. 값은 소문자 #rrggbb이고, alpha를 켜면 #rrggbbaa예요. 칸에는 작은 견본과 글자가 보여요.cellEditor: 'color' opens 16 swatches and a hex input. The value is a lowercase #rrggbb, or #rrggbbaa with alpha. The cell shows a small swatch and the text.

활용Intermediate
직접 써 보기Live preview색 칸 편집: 견본과 16진 입력Edit colors: swatches and hex input

코드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="editColor">색 편집</button>
  </div>
  <div id="grid" style="height: 300px"></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('editColor').addEventListener('click', () => actions.editColor(api));
example.js
const en = document.documentElement.dataset.lang === 'en';
const rows = [
  { id: 1, label: en ? 'Default' : '기본 색', color: '#1a73e8', tint: '#1a73e8cc' },
  { id: 2, label: en ? 'Warning' : '경고', color: '#f9ab00', tint: '#f9ab0066' },
  { id: 3, label: en ? 'Danger' : '위험', color: '#d93025', tint: '#d9302533' },
  { id: 4, label: en ? 'Undecided' : '미정', color: null, tint: '#5f6368ff' },
];
// cellEditor: 'color'는 견본 격자와 16진 입력칸이 열려요. 값은 소문자 #rrggbb이고 alpha를 켜면 #rrggbbaa예요.
export const options = {
  columns: [
    { id: 'label', title: en ? 'Name' : '이름', width: 130 },
    { id: 'color', title: en ? 'Color' : '색', width: 150, editable: true, cellEditor: 'color' },
    {
      id: 'tint',
      title: en ? 'Color with alpha' : '투명도 있는 색',
      width: 190,
      editable: true,
      cellEditor: 'color',
      editorParams: {
        alpha: true,
        swatches: [
          '#1a73e8',
          '#188038',
          '#f9ab00',
          '#d93025',
          '#9334e6',
          '#5f6368',
          '#1a73e8cc',
          '#d9302533',
        ],
      },
    },
  ],
  rows,
  rowKey: 'id',
  ariaLabel: en ? 'Edit colors' : '색 편집',
};
export function setup(api, log) {
  log({
    ko: '색 칸을 더블클릭하세요. 방향키로 견본을 옮기고 Enter로 고르거나, #1a73e8처럼 직접 써요. 형식이 틀리면 열린 채로 알려요.',
    en: 'Double-click a color cell. Move over swatches with the arrows and press Enter, or type #1a73e8. A wrong format keeps the editor open with a message.',
  });
  const offs = [
    api.on('editCommitted', (e) => log({
      ko: `${e.rowKey}번 ${e.columnId} → ${String(e.newValue)}`,
      en: `Row ${e.rowKey} ${e.columnId} → ${String(e.newValue)}`,
    })),
    api.on('validationFailed', (e) => log({ ko: `확정하지 못했어요: ${e.message}`, en: `Could not confirm: ${e.message}` })),
  ];
  return () => offs.forEach((off) => off());
}
export const actions = {
  editColor: (api) => api.startEdit(1, 'color'),
};

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="editColor">Edit color</button>
  </div>
  <div id="grid" style="height: 300px"></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('editColor').addEventListener('click', () => actions.editColor(api));
example.js
const rows = [
  { id: 1, label: 'Default', color: '#1a73e8', tint: '#1a73e8cc' },
  { id: 2, label: 'Warning', color: '#f9ab00', tint: '#f9ab0066' },
  { id: 3, label: 'Danger', color: '#d93025', tint: '#d9302533' },
  { id: 4, label: 'Undecided', color: null, tint: '#5f6368ff' },
];
// cellEditor: 'color' opens a swatch grid and a hex input. The value is a lowercase #rrggbb, or #rrggbbaa with alpha on.
export const options = {
  columns: [
    { id: 'label', title: 'Name', width: 130 },
    { id: 'color', title: 'Color', width: 150, editable: true, cellEditor: 'color' },
    {
      id: 'tint',
      title: 'Color with alpha',
      width: 190,
      editable: true,
      cellEditor: 'color',
      editorParams: {
        alpha: true,
        swatches: [
          '#1a73e8',
          '#188038',
          '#f9ab00',
          '#d93025',
          '#9334e6',
          '#5f6368',
          '#1a73e8cc',
          '#d9302533',
        ],
      },
    },
  ],
  rows,
  rowKey: 'id',
  ariaLabel: 'Edit colors',
};
export function setup(api, log) {
  log('Double-click a color cell. Move over swatches with the arrows and press Enter, or type #1a73e8. A wrong format keeps the editor open with a message.');
  const offs = [
    api.on('editCommitted', (e) => log(`Row ${e.rowKey} ${e.columnId} → ${String(e.newValue)}`)),
    api.on('validationFailed', (e) => log(`Could not confirm: ${e.message}`)),
  ];
  return () => offs.forEach((off) => off());
}
export const actions = {
  editColor: (api) => api.startEdit(1, 'color'),
};