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

행을 옮기면 순서 번호가 따라와요Order numbers follow moved rows

orderIndex 옵션을 켜면 행을 옮길 때 옮긴 행의 seq 칸에 앞뒤 번호 사이 값이 자동으로 들어가요. 끌어서 옮기거나 버튼으로 옮겨 보세요.With the orderIndex option, a moved row gets a number between its new neighbors in the seq field automatically. Drag a row or use the buttons.

입문Beginner
직접 써 보기Live preview행을 옮기면 순서 번호가 따라와요Order numbers follow moved rows

코드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="moveFirstToEnd">첫 행을 맨 끝으로</button>
    <button type="button" id="moveLastToTop">마지막 행을 맨 위로</button>
    <button type="button" id="undo">되돌리기</button>
  </div>
  <div id="grid" style="height: 360px"></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('moveFirstToEnd').addEventListener('click', () => actions.moveFirstToEnd(api));
document.getElementById('moveLastToTop').addEventListener('click', () => actions.moveLastToTop(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));
example.js
const owners = ['김서연', '박지훈', '이도윤', '최하은'];
const rows = [
  '요구사항 정리',
  '화면 설계',
  '데이터 모델',
  'API 구현',
  '테스트 작성',
  '문서 보완',
  '배포 준비',
  '회고',
].map((task, i) => ({ id: i + 1, seq: (i + 1) * 10, task, owner: owners[i % 4] }));
// orderIndex를 켜면 행을 옮긴 뒤 옮긴 행의 seq 칸에 앞뒤 번호의 가운데 값을 써요.
export const options = {
  columns: [
    { id: 'seq', title: '순서 번호', width: 90, align: 'end' },
    { id: 'task', title: '할 일', width: 170 },
    { id: 'owner', title: '담당', width: 100 },
  ],
  rows,
  rowKey: 'id',
  rowDrag: true,
  orderIndex: { field: 'seq' },
};
export function setup(api, log) {
  log({
    ko: '손잡이를 끌어 행을 옮기면 순서 번호가 바뀌어요.',
    en: 'Drag a handle to move a row and its order number changes.',
  });
  const off = api.on('orderIndexChanged', (e) => {
    const seqs = e.rowKeys.map((k) => `${k}번=${api.getRowByKey(k)?.seq}`).join(', ');
    log({
      ko: `번호 바뀜: ${seqs} (${e.renumbered ? '전체를 다시 매김' : '옮긴 행만'})`,
      en: `Order changed: ${seqs} (${e.renumbered ? 'renumbered all' : 'moved rows only'})`,
    });
  });
  return () => off();
}
export const actions = {
  moveFirstToEnd: (api) => {
    const all = api.getRows();
    api.moveRows([all[0].id], { after: all[all.length - 1].id });
  },
  moveLastToTop: (api) => {
    const all = api.getRows();
    api.moveRows([all[all.length - 1].id], { before: all[0].id });
  },
  undo: (api) => {
    api.undo();
  },
};

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="moveFirstToEnd">First row to the end</button>
    <button type="button" id="moveLastToTop">Last row to the top</button>
    <button type="button" id="undo">Undo</button>
  </div>
  <div id="grid" style="height: 360px"></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('moveFirstToEnd').addEventListener('click', () => actions.moveFirstToEnd(api));
document.getElementById('moveLastToTop').addEventListener('click', () => actions.moveLastToTop(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));
example.js
const owners = ['Liam Carter', 'Emma Stone', 'Noah Reed', 'Olivia Hayes'];
const rows = [
  'Gather requirements',
  'Design screens',
  'Data model',
  'Build API',
  'Write tests',
  'Polish docs',
  'Prepare release',
  'Retrospective',
].map((task, i) => ({ id: i + 1, seq: (i + 1) * 10, task, owner: owners[i % 4] }));
// With orderIndex on, a moved row gets a value between its new neighbors in the seq field.
export const options = {
  columns: [
    { id: 'seq', title: 'Order', width: 80, align: 'end' },
    { id: 'task', title: 'Task', width: 170 },
    { id: 'owner', title: 'Owner', width: 120 },
  ],
  rows,
  rowKey: 'id',
  rowDrag: true,
  orderIndex: { field: 'seq' },
};
export function setup(api, log) {
  log('Drag a handle to move a row and its order number changes.');
  const off = api.on('orderIndexChanged', (e) => {
    const seqs = e.rowKeys.map((k) => `row ${k}=${api.getRowByKey(k)?.seq}`).join(', ');
    log(`Order changed: ${seqs} (${e.renumbered ? 'renumbered all' : 'moved rows only'})`);
  });
  return () => off();
}
export const actions = {
  moveFirstToEnd: (api) => {
    const all = api.getRows();
    api.moveRows([all[0].id], { after: all[all.length - 1].id });
  },
  moveLastToTop: (api) => {
    const all = api.getRows();
    api.moveRows([all[all.length - 1].id], { before: all[0].id });
  },
  undo: (api) => {
    api.undo();
  },
};