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

번호 간격이 모자라면 전체를 다시 매겨요Renumber when the gaps run out

이웃한 번호 차이가 2보다 작아 끼울 자리가 없으면 같은 부모끼리 전체를 10, 20, 30 …으로 다시 매겨요. getOrderGaps로 간격을 보고 renumberOrder로 직접 다시 매길 수도 있어요.When neighboring numbers differ by less than 2 there is no room, so the whole list is renumbered 10, 20, 30 and so on. getOrderGaps shows the gaps and renumberOrder renumbers on demand.

활용Intermediate
직접 써 보기Live preview번호 간격이 모자라면 전체를 다시 매겨요Renumber when the gaps run out

코드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="showGaps">간격 보기</button>
    <button type="button" id="moveIntoTight">빽빽한 곳에 끼우기</button>
    <button type="button" id="renumber">다시 매기기</button>
    <select aria-label="다시 매기는 범위" id="mode">
      <option value="moved" selected>옮긴 행만 (기본)</option>
      <option value="all">모두</option>
    </select>
  </div>
  <div id="grid" style="height: 380px"></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('showGaps').addEventListener('click', () => actions.showGaps(api));
document.getElementById('moveIntoTight').addEventListener('click', () => actions.moveIntoTight(api));
document.getElementById('renumber').addEventListener('click', () => actions.renumber(api));
document.getElementById('mode').addEventListener('change', (event) => actions.mode(api, event.target.value));
example.js
const tasks = ['기획', '설계', '개발', '리뷰', '테스트', '수정', '배포', '정리'];
const seqs = [10, 11, 12, 13, 14, 15, 40, 80];
// 앞쪽 여섯 행은 번호가 붙어 있어서(10~15) 사이에 끼울 자리가 없어요.
export const options = {
  columns: [
    { id: 'seq', title: '순서 번호', width: 90, align: 'end' },
    { id: 'task', title: '할 일', width: 180 },
  ],
  rows: tasks.map((task, i) => ({ id: i + 1, seq: seqs[i], task })),
  rowKey: 'id',
  rowDrag: true,
  orderIndex: { field: 'seq', step: 10 },
};
let say = () => { };
export function setup(api, log) {
  say = log;
  log({
    ko: '번호 사이가 비좁은 곳으로 행을 옮기면 전체를 다시 매겨요.',
    en: 'Moving a row into a tight spot renumbers the whole list.',
  });
  const off = api.on('orderIndexChanged', (e) => log({
    ko: `번호 바뀜: ${e.rowKeys.length}개 행, 전체 다시 매김 ${e.renumbered}`,
    en: `Order changed: ${e.rowKeys.length} rows, renumbered ${e.renumbered}`,
  }));
  return () => off();
}
export const actions = {
  showGaps: (api) => {
    const g = api.getOrderGaps();
    say({
      ko: `최소 ${g.min}, 최대 ${g.max}, 가장 좁은 간격 ${g.minGap}, 다시 매기기 필요 ${g.needsRenumber}`,
      en: `min ${g.min}, max ${g.max}, smallest gap ${g.minGap}, needs renumber ${g.needsRenumber}`,
    });
  },
  moveIntoTight: (api) => {
    const all = api.getRows();
    const last = all[all.length - 1];
    api.moveRows([last.id], { after: all[1].id });
  },
  renumber: (api) => {
    const n = api.renumberOrder();
    say({ ko: `${n}개 행의 번호를 다시 매겼어요`, en: `Renumbered ${n} rows` });
  },
  mode: (api, value) => api.setOptions({
    orderIndex: { field: 'seq', step: 10, renumber: value === 'all' ? 'all' : 'moved' },
  }),
};

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="showGaps">Show gaps</button>
    <button type="button" id="moveIntoTight">Move into tight spot</button>
    <button type="button" id="renumber">Renumber</button>
    <select aria-label="Renumber scope" id="mode">
      <option value="moved" selected>Moved rows (default)</option>
      <option value="all">All rows</option>
    </select>
  </div>
  <div id="grid" style="height: 380px"></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('showGaps').addEventListener('click', () => actions.showGaps(api));
document.getElementById('moveIntoTight').addEventListener('click', () => actions.moveIntoTight(api));
document.getElementById('renumber').addEventListener('click', () => actions.renumber(api));
document.getElementById('mode').addEventListener('change', (event) => actions.mode(api, event.target.value));
example.js
const tasks = ['Plan', 'Design', 'Build', 'Review', 'Test', 'Fix', 'Release', 'Wrap up'];
const seqs = [10, 11, 12, 13, 14, 15, 40, 80];
// The first six rows are numbered 10 to 15, so there is no room between them.
export const options = {
  columns: [
    { id: 'seq', title: 'Order', width: 80, align: 'end' },
    { id: 'task', title: 'Task', width: 180 },
  ],
  rows: tasks.map((task, i) => ({ id: i + 1, seq: seqs[i], task })),
  rowKey: 'id',
  rowDrag: true,
  orderIndex: { field: 'seq', step: 10 },
};
let say = () => { };
export function setup(api, log) {
  say = log;
  log('Moving a row into a tight spot renumbers the whole list.');
  const off = api.on('orderIndexChanged', (e) => log(`Order changed: ${e.rowKeys.length} rows, renumbered ${e.renumbered}`));
  return () => off();
}
export const actions = {
  showGaps: (api) => {
    const g = api.getOrderGaps();
    say(`min ${g.min}, max ${g.max}, smallest gap ${g.minGap}, needs renumber ${g.needsRenumber}`);
  },
  moveIntoTight: (api) => {
    const all = api.getRows();
    const last = all[all.length - 1];
    api.moveRows([last.id], { after: all[1].id });
  },
  renumber: (api) => {
    const n = api.renumberOrder();
    say(`Renumbered ${n} rows`);
  },
  mode: (api, value) => api.setOptions({
    orderIndex: { field: 'seq', step: 10, renumber: value === 'all' ? 'all' : 'moved' },
  }),
};