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

서버에 물어 목록 채우기Fill the list by asking a server

cellEditor 'remote'는 글자를 치면 debounceMs 뒤에 source를 한 번 불러 목록을 채워요. 새 글자가 오면 이전 요청은 취소되고 늦게 온 응답은 버려요. 저장되는 값은 고른 항목의 value이고, 칸에 보일 글자는 formatValue가 정해요.cellEditor 'remote' calls source once after debounceMs to fill the list. A new key cancels the previous request and a late response is dropped. The stored value is the picked item's value, and formatValue decides what the cell shows.

활용Intermediate
직접 써 보기Live preview서버에 물어 목록 채우기Fill the list by asking a server

코드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: 280px"></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
// 서버를 흉내 내는 고객 목록이에요. 실제로는 source 안에서 fetch로 서버에 물어요.
const CUSTOMERS = [
  '김하늘',
  '이도윤',
  '박서연',
  '최지우',
  '정민준',
  '강서윤',
  '조현우',
  '윤아린',
  '장도현',
  '임수아',
  '한지민',
  '오세훈',
  '서예린',
  '신준서',
  '권나연',
  '황민석',
  '안유진',
  '송지호',
  '류하은',
  '전태오',
].map((name, i) => ({ value: i + 1, label: `${name} (C-${String(i + 1).padStart(3, '0')})` }));
const rows = [
  { id: 1, order: 'A-1001', customer: 3 },
  { id: 2, order: 'A-1002', customer: null },
  { id: 3, order: 'A-1003', customer: 12 },
];
/** 느린 서버 흉내: 300ms 뒤에 글자가 든 고객을 돌려주고, 취소되면 멈춰요. */
function search(query, signal) {
  return new Promise((resolve, reject) => {
    const timer = setTimeout(() => resolve(CUSTOMERS.filter((c) => c.label.includes(query))), 300);
    signal.addEventListener('abort', () => {
      clearTimeout(timer);
      reject(new DOMException('aborted', 'AbortError'));
    });
  });
}
// cellEditor: 'remote'는 editors 묶음이 필요해요(전체 번들에는 들어 있어요).
// 글자를 치면 debounceMs 뒤에 source를 한 번 불러요. 새 글자가 오면 이전 요청은 signal로 취소돼요.
export const options = {
  columns: [
    { id: 'order', title: '주문', width: 110 },
    {
      id: 'customer',
      title: '고객(서버 검색)',
      width: 260,
      editable: true,
      cellEditor: 'remote',
      // 저장 값은 고객 번호뿐이라 칸에 보일 글자는 formatValue가 정해요.
      formatValue: (id) => CUSTOMERS.find((c) => c.value === id)?.label ?? '',
      editorParams: {
        source: (query, { signal }) => search(query, signal),
        debounceMs: 250,
        minChars: 1,
        placeholder: '이름 검색',
      },
    },
  ],
  rows,
  rowKey: 'id',
};
export function setup(api, log) {
  log({
    ko: '"고객" 칸을 두 번 누르고 "김"이나 "서"를 쳐 보세요. 잠깐 "불러오는 중…"이 보인 뒤 목록이 나와요.',
    en: 'Double-click a "고객" (customer) cell and type "김" or "서". "Loading…" shows briefly and then the list appears.',
  });
  const offs = [
    api.on('editCommitted', (e) => log({
      ko: `행 ${e.rowKey}: ${e.columnId} = ${String(e.newValue)}`,
      en: `Row ${e.rowKey}: ${e.columnId} = ${String(e.newValue)}`,
    })),
  ];
  return () => offs.forEach((off) => 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: 280px"></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
// A customer list that imitates a server. In real use, source asks the server with fetch.
const CUSTOMERS = [
  'Liam Carter',
  'Emma Stone',
  'Noah Kim',
  'Olivia Park',
  'Ethan Brooks',
  'Ava Morgan',
  'Mason Reed',
  'Sophia Hayes',
  'Lucas Bennett',
  'Mia Foster',
  'Logan Price',
  'Isabella Ward',
  'James Cooper',
  'Charlotte Gray',
  'Benjamin Ross',
  'Amelia Hughes',
  'Henry Parker',
  'Harper Collins',
  'Jack Murphy',
  'Evelyn Wood',
].map((name, i) => ({ value: i + 1, label: `${name} (C-${String(i + 1).padStart(3, '0')})` }));
const rows = [
  { id: 1, order: 'A-1001', customer: 3 },
  { id: 2, order: 'A-1002', customer: null },
  { id: 3, order: 'A-1003', customer: 12 },
];
/** A slow server imitation: after 300ms it returns the customers containing the text, and it stops when cancelled. */
function search(query, signal) {
  return new Promise((resolve, reject) => {
    const timer = setTimeout(() => resolve(CUSTOMERS.filter((c) => c.label.includes(query))), 300);
    signal.addEventListener('abort', () => {
      clearTimeout(timer);
      reject(new DOMException('aborted', 'AbortError'));
    });
  });
}
// cellEditor: 'remote' needs the editors bundle (the full bundle includes it).
// Typing calls source once after debounceMs. When new text arrives, the previous request is cancelled through signal.
export const options = {
  columns: [
    { id: 'order', title: 'Order', width: 110 },
    {
      id: 'customer',
      title: 'Customer (server search)',
      width: 280,
      editable: true,
      cellEditor: 'remote',
      // The stored value is only the customer number, so formatValue decides the text shown in the cell.
      formatValue: (id) => CUSTOMERS.find((c) => c.value === id)?.label ?? '',
      editorParams: {
        source: (query, { signal }) => search(query, signal),
        debounceMs: 250,
        minChars: 1,
        placeholder: 'Search name',
      },
    },
  ],
  rows,
  rowKey: 'id',
};
export function setup(api, log) {
  log('Double-click a "Customer" cell and type "Li" or "Ma". "Loading…" shows briefly and then the list appears.');
  const offs = [
    api.on('editCommitted', (e) => log(`Row ${e.rowKey}: ${e.columnId} = ${String(e.newValue)}`)),
  ];
  return () => offs.forEach((off) => off());
}