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

10만 줄 트리: 펼치기·찾아가기·검색A 100,000-row tree: expand, jump and search

지역·거점·장비 10만 줄 트리를 열고 접고, 한 장비로 곧바로 찾아가고, 이름으로 검색해요. 보이는 줄만 그려서 많아도 가벼워요.Expand and collapse a 100,000-row region/site/device tree, jump to one device and search by name. Only visible rows are drawn.

심화Advanced
직접 써 보기Live preview10만 줄 트리: 펼치기·찾아가기·검색A 100,000-row tree: expand, jump and search

코드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="sites">거점까지 펼치기</button>
    <button type="button" id="expandAll">모두 펼치기</button>
    <button type="button" id="collapseAll">모두 접기</button>
    <button type="button" id="deep">50,000번째 줄로 가기</button>
    <input
      type="text"
      aria-label="검색"
      placeholder="이름 검색 (예: 장비 3-7)"
      id="find"
    />
  </div>
  <div id="grid" style="height: 330px"></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('sites').addEventListener('click', () => actions.sites(api));
document.getElementById('expandAll').addEventListener('click', () => actions.expandAll(api));
document.getElementById('collapseAll').addEventListener('click', () => actions.collapseAll(api));
document.getElementById('deep').addEventListener('click', () => actions.deep(api));
document.getElementById('find').addEventListener('input', (event) => actions.find(api, event.target.value));
example.js
// 데이터 값 뒤에 붙는 조사는 마지막 글자의 받침에 맞춰 골라요(숫자는 읽는 소리 기준).
const batchim = (word) => {
  const ch = word.trim().slice(-1);
  const digit = '0123456789'.indexOf(ch);
  if (digit >= 0)
    return [21, 8, 0, 16, 0, 0, 1, 8, 8, 0][digit] ?? 0;
  const code = ch.charCodeAt(0) - 0xac00;
  return code >= 0 && code <= 11171 ? code % 28 : 0;
};
const eulReul = (word) => (batchim(word) ? '을' : '를');
// 지역 20곳 × 거점 50곳 × 장비 99대 = 약 10만 줄이에요. 그리드는 보이는 줄만 그려서 많아도 가벼워요.
const regions = [
  '서울',
  '부산',
  '대구',
  '인천',
  '광주',
  '대전',
  '울산',
  '세종',
  '수원',
  '성남',
  '고양',
  '용인',
  '창원',
  '청주',
  '전주',
  '천안',
  '포항',
  '제주',
  '춘천',
  '목포',
];
const rows = [];
for (let r = 0; r < 20; r++) {
  const regionId = rows.length + 1;
  rows.push({ id: regionId, parentId: null, name: `${regions[r]} 지역`, kind: '지역', value: r });
  for (let s = 0; s < 50; s++) {
    const siteId = rows.length + 1;
    rows.push({
      id: siteId,
      parentId: regionId,
      name: `${regions[r]} ${s + 1}호 센터`,
      kind: '거점',
      value: s,
    });
    for (let d = 0; d < 99; d++) {
      rows.push({
        id: rows.length + 1,
        parentId: siteId,
        name: `장비 ${r + 1}-${s + 1}-${d + 1}`,
        kind: '장비',
        value: (r * 31 + s * 17 + d * 7) % 1000,
      });
    }
  }
}
export const options = {
  columns: [
    { id: 'name', title: '이름', width: 240 },
    { id: 'kind', title: '구분', width: 80 },
    { id: 'value', title: '측정값', kind: 'number', width: 90, align: 'end' },
  ],
  rows,
  rowKey: 'id',
  tree: { parentKey: 'parentId', column: 'name', expandDepth: 0 },
};
let say = () => { };
export function setup(_api, log) {
  say = log;
  log({
    ko: `${rows.length.toLocaleString('ko-KR')}줄을 불러왔어요. 지금은 지역 20곳만 보여요.`,
    en: `Loaded ${rows.length.toLocaleString('en-US')} rows. Only 20 regions show for now.`,
  });
}
export const actions = {
  expandAll: (api) => {
    const started = performance.now();
    api.expandAll();
    say({
      ko: `모두 펼쳤어요. 보이는 줄 ${api.getVisibleRowCount().toLocaleString('ko-KR')}개 (${Math.round(performance.now() - started)}ms)`,
      en: `Expanded all. ${api.getVisibleRowCount().toLocaleString('en-US')} rows visible (${Math.round(performance.now() - started)}ms)`,
    });
  },
  sites: (api) => {
    api.expandToDepth(1);
    say({
      ko: `거점까지 펼쳤어요. 보이는 줄 ${api.getVisibleRowCount().toLocaleString('ko-KR')}개`,
      en: `Expanded down to the sites. ${api.getVisibleRowCount().toLocaleString('en-US')} rows visible`,
    });
  },
  collapseAll: (api) => {
    api.collapseAll();
    say({ ko: '모두 접었어요.', en: 'Collapsed all.' });
  },
  // 위 단계를 모두 펼친 다음 그 줄로 스크롤해요.
  deep: (api) => {
    const row = api.getRowByKey(50_000);
    // 찾기로 가려져 있으면 그 줄로 갈 수 없어서(오류), 먼저 찾기를 지워요.
    if (api.getFilterModel() !== null)
      api.setFilterModel(null);
    api.revealRow(50_000);
    api.scrollToIndex(api.getVisibleIndexOfKey(50_000));
    if (row)
      say({ ko: `${row.name}${eulReul(row.name)} 찾아갔어요.`, en: `Went to ${row.name}.` });
  },
  find: (api, value) => {
    const text = (value ?? '').trim();
    api.setFilterModel(text
      ? { match: 'all', items: [{ columnId: 'name', operator: 'contains', value: text }] }
      : null);
    const found = api.getVisibleRowCount().toLocaleString('ko-KR');
    say({
      ko: text ? `"${text}" 찾기: ${found}개 줄이 보여요.` : '찾기를 지웠어요.',
      en: text ? `Search "${text}": ${found} rows visible.` : 'Search cleared.',
    });
  },
};

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="sites">Expand to sites</button>
    <button type="button" id="expandAll">Expand all</button>
    <button type="button" id="collapseAll">Collapse all</button>
    <button type="button" id="deep">Jump to row 50,000</button>
    <input
      type="text"
      aria-label="Search"
      placeholder="Search by name (e.g. Device 3-7)"
      id="find"
    />
  </div>
  <div id="grid" style="height: 330px"></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('sites').addEventListener('click', () => actions.sites(api));
document.getElementById('expandAll').addEventListener('click', () => actions.expandAll(api));
document.getElementById('collapseAll').addEventListener('click', () => actions.collapseAll(api));
document.getElementById('deep').addEventListener('click', () => actions.deep(api));
document.getElementById('find').addEventListener('input', (event) => actions.find(api, event.target.value));
example.js
// 20 regions x 50 sites x 99 devices = about 100,000 rows. The grid only draws the visible rows, so it stays light even with this many.
const regions = [
  'New York',
  'Boston',
  'Chicago',
  'Houston',
  'Phoenix',
  'Denver',
  'Seattle',
  'Portland',
  'Austin',
  'Dallas',
  'Atlanta',
  'Miami',
  'Detroit',
  'Memphis',
  'Orlando',
  'Nashville',
  'Raleigh',
  'Honolulu',
  'Madison',
  'Tucson',
];
const rows = [];
for (let r = 0; r < 20; r++) {
  const regionId = rows.length + 1;
  rows.push({
    id: regionId,
    parentId: null,
    name: `${regions[r]} Region`,
    kind: 'Region',
    value: r,
  });
  for (let s = 0; s < 50; s++) {
    const siteId = rows.length + 1;
    rows.push({
      id: siteId,
      parentId: regionId,
      name: `${regions[r]} Center ${s + 1}`,
      kind: 'Site',
      value: s,
    });
    for (let d = 0; d < 99; d++) {
      rows.push({
        id: rows.length + 1,
        parentId: siteId,
        name: `Device ${r + 1}-${s + 1}-${d + 1}`,
        kind: 'Device',
        value: (r * 31 + s * 17 + d * 7) % 1000,
      });
    }
  }
}
export const options = {
  columns: [
    { id: 'name', title: 'Name', width: 240 },
    { id: 'kind', title: 'Type', width: 80 },
    { id: 'value', title: 'Reading', kind: 'number', width: 90, align: 'end' },
  ],
  rows,
  rowKey: 'id',
  tree: { parentKey: 'parentId', column: 'name', expandDepth: 0 },
};
let say = () => { };
export function setup(_api, log) {
  say = log;
  log(`Loaded ${rows.length.toLocaleString('en-US')} rows. Only 20 regions show for now.`);
}
export const actions = {
  expandAll: (api) => {
    const started = performance.now();
    api.expandAll();
    say(`Expanded all. ${api.getVisibleRowCount().toLocaleString('en-US')} rows visible (${Math.round(performance.now() - started)}ms)`);
  },
  sites: (api) => {
    api.expandToDepth(1);
    say(`Expanded down to the sites. ${api.getVisibleRowCount().toLocaleString('en-US')} rows visible`);
  },
  collapseAll: (api) => {
    api.collapseAll();
    say('Collapsed all.');
  },
  // Open all the ancestors, then scroll to that row.
  deep: (api) => {
    const row = api.getRowByKey(50_000);
    // A row hidden by the search cannot be reached (it throws), so clear the search first.
    if (api.getFilterModel() !== null)
      api.setFilterModel(null);
    api.revealRow(50_000);
    api.scrollToIndex(api.getVisibleIndexOfKey(50_000));
    if (row)
      say(`Went to ${row.name}.`);
  },
  find: (api, value) => {
    const text = (value ?? '').trim();
    api.setFilterModel(text
      ? { match: 'all', items: [{ columnId: 'name', operator: 'contains', value: text }] }
      : null);
    const found = api.getVisibleRowCount().toLocaleString('en-US');
    say(text ? `Search "${text}": ${found} rows visible.` : 'Search cleared.');
  },
};