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

실패·빈 결과·느린 응답Failures, empty results and slow responses

remote 편집기의 요청이 실패하면 목록 안에 오류 글자가 나오고 error 이벤트가 한 번 가요. 결과가 비면 noResultsText가, 기다리는 동안 loadingText가 보여요. 느린 응답 도중 새 글자를 치면 이전 요청이 취소되고 그 응답은 버려져요.When a remote editor request fails, an error text appears in the list and one error event is sent. An empty result shows noResultsText and waiting shows loadingText. Typing during a slow response cancels the old request and drops its answer.

심화Advanced
직접 써 보기Live preview실패·빈 결과·느린 응답Failures, empty results and slow responses

코드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: 260px"></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
// 예제 틀이 알려 주는 페이지 언어(ko/en)에 맞는 문구를 골라요.
const L = (ko, en) => document.documentElement.dataset.lang === 'en' ? en : ko;
const rows = [
  { id: 1, code: 'P-01', item: null },
  { id: 2, code: 'P-02', item: 'apple' },
];
// 글자에 따라 다르게 동작하는 서버 흉내예요: "err"는 실패, "slow"는 3초 뒤 응답, "none"은 빈 결과, 나머지는 정상이에요.
function makeSource(log) {
  return (query, { signal }) => new Promise((resolve, reject) => {
    const wait = query.includes('slow') ? 3000 : 200;
    const timer = setTimeout(() => {
      if (query.includes('err'))
        return reject(new Error('server error'));
      if (query.includes('none'))
        return resolve([]);
      resolve([1, 2, 3].map((n) => ({ value: `${query}-${n}`, label: `${query} #${n}` })));
    }, wait);
    signal.addEventListener('abort', () => {
      clearTimeout(timer);
      log({ ko: `요청 취소: "${query}"`, en: `Request aborted: "${query}"` });
      reject(new DOMException('aborted', 'AbortError'));
    });
  });
}
export const options = {
  columns: [
    { id: 'code', title: '코드', width: 100 },
    {
      id: 'item',
      title: '품목(원격)',
      width: 260,
      editable: true,
      cellEditor: 'remote',
      editorParams: {
        source: makeSource(() => undefined),
        debounceMs: 200,
        loadingText: L('서버에 물어 보는 중…', 'Asking the server…'),
        noResultsText: L('맞는 품목이 없어요', 'No matching items'),
      },
    },
  ],
  rows,
  rowKey: 'id',
};
export function setup(api, log) {
  // 로그를 남기도록 source를 다시 만들어 컬럼에 걸어요.
  api.setColumns([
    options.columns[0],
    {
      ...options.columns[1],
      editorParams: {
        ...options.columns[1].editorParams,
        source: makeSource(log),
      },
    },
  ]);
  log({
    ko: '"품목" 칸을 두 번 누르고 "err", "none", "slow", 아무 글자를 차례로 쳐 보세요.',
    en: 'Double-click an "품목" (item) cell and type "err", "none", "slow" and any other text in turn.',
  });
  const offs = [
    api.on('error', (e) => log({
      ko: `오류 이벤트(${e.code}): ${e.where}`,
      en: `Error event (${e.code}): ${e.where}`,
    })),
    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: 260px"></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, code: 'P-01', item: null },
  { id: 2, code: 'P-02', item: 'apple' },
];
// A fake server that behaves differently depending on the text: "err" fails, "slow" answers after 3 seconds, "none" returns nothing, anything else is normal.
function makeSource(log) {
  return (query, { signal }) => new Promise((resolve, reject) => {
    const wait = query.includes('slow') ? 3000 : 200;
    const timer = setTimeout(() => {
      if (query.includes('err'))
        return reject(new Error('server error'));
      if (query.includes('none'))
        return resolve([]);
      resolve([1, 2, 3].map((n) => ({ value: `${query}-${n}`, label: `${query} #${n}` })));
    }, wait);
    signal.addEventListener('abort', () => {
      clearTimeout(timer);
      log(`Request aborted: "${query}"`);
      reject(new DOMException('aborted', 'AbortError'));
    });
  });
}
export const options = {
  columns: [
    { id: 'code', title: 'Code', width: 100 },
    {
      id: 'item',
      title: 'Item (remote)',
      width: 260,
      editable: true,
      cellEditor: 'remote',
      editorParams: {
        source: makeSource(() => undefined),
        debounceMs: 200,
        loadingText: 'Asking the server…',
        noResultsText: 'No matching items',
      },
    },
  ],
  rows,
  rowKey: 'id',
};
export function setup(api, log) {
  // Rebuild the source so it logs, and attach it to the column.
  api.setColumns([
    options.columns[0],
    {
      ...options.columns[1],
      editorParams: {
        ...options.columns[1].editorParams,
        source: makeSource(log),
      },
    },
  ]);
  log('Double-click an "Item" cell and type "err", "none", "slow" and any other text in turn.');
  const offs = [
    api.on('error', (e) => log(`Error event (${e.code}): ${e.where}`)),
    api.on('editCommitted', (e) => log(`Row ${e.rowKey}: ${e.columnId} = ${String(e.newValue)}`)),
  ];
  return () => offs.forEach((off) => off());
}