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

끌어서 행 순서 바꾸기Reorder rows by dragging

rowDrag를 켜면 맨 앞에 끌기 손잡이 칸이 생겨요. 손잡이를 끌어 행을 옮기고, 선택한 행은 함께 움직여요. 되돌리기는 한 단계이고 정렬·필터 중에는 끌 수 없어요.With rowDrag on, a drag handle column appears at the front. Drag a handle to move a row; selected rows move together. Undo is one step, and rows cannot be dragged while sorting or filtering.

입문Beginner
직접 써 보기Live preview끌어서 행 순서 바꾸기Reorder rows by dragging

코드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="select">2·3번 행 선택</button>
    <button type="button" id="sortOn">정렬 켜기</button>
    <button type="button" id="sortOff">정렬 끄기</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('select').addEventListener('click', () => actions.select(api));
document.getElementById('sortOn').addEventListener('click', () => actions.sortOn(api));
document.getElementById('sortOff').addEventListener('click', () => actions.sortOff(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));
example.js
const owners = ['김서연', '박지훈', '이도윤', '최하은'];
const rows = [
  '요구사항 정리',
  '화면 설계',
  '데이터 모델',
  'API 구현',
  '테스트 작성',
  '문서 보완',
  '배포 준비',
  '회고',
].map((task, i) => ({ id: i + 1, task, owner: owners[i % 4] }));
// rowDrag: true는 맨 앞에 끌기 손잡이 칸을 두고, 손잡이를 끌어서 행 순서를 바꿔요(행 이동 API moveRows를 써요).
// 행을 골라 둔 채 선택된 행의 손잡이를 끌면 선택한 행이 모두 함께 움직여요.
export const options = {
  columns: [
    { id: 'task', title: '할 일', width: 180 },
    { id: 'owner', title: '담당', width: 100 },
  ],
  selection: 'multiple',
  rowDrag: true,
  rows,
  rowKey: 'id',
};
export function setup(api, log) {
  log({
    ko: '맨 왼쪽 손잡이(⠿)를 끌어 행을 옮겨 보세요. 행 몇 개를 고른 뒤 그중 하나의 손잡이를 끌면 함께 움직여요.',
    en: 'Drag the handle (⠿) on the left to move a row. Select a few rows and drag one of their handles to move them together.',
  });
  const offs = [
    api.on('rowDragStarted', (e) => log({
      ko: `끌기 시작: ${e.keys.length}개 행`,
      en: `Drag started: ${e.keys.length} ${e.keys.length === 1 ? 'row' : 'rows'}`,
    })),
    api.on('rowDropped', (e) => log({
      ko: `놓았어요: ${e.keys.join(', ')} → ${e.targetKey === null ? '맨 끝' : `${e.targetKey}번 ${e.position === 'before' ? '앞' : '뒤'}`}`,
      en: `Dropped: ${e.keys.join(', ')} → ${e.targetKey === null ? 'the end' : `${e.position} row ${e.targetKey}`}`,
    })),
    api.on('rowDragCancelled', (e) => log({ ko: `취소했어요 (${e.reason})`, en: `Cancelled (${e.reason})` })),
  ];
  return () => offs.forEach((off) => off());
}
export const actions = {
  select: (api) => api.setSelectedKeys([2, 3]),
  sortOn: (api) => api.setSortModel([{ columnId: 'task', direction: 'asc' }]),
  sortOff: (api) => api.setSortModel([]),
  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="select">Select rows 2 and 3</button>
    <button type="button" id="sortOn">Sort on</button>
    <button type="button" id="sortOff">Sort off</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('select').addEventListener('click', () => actions.select(api));
document.getElementById('sortOn').addEventListener('click', () => actions.sortOn(api));
document.getElementById('sortOff').addEventListener('click', () => actions.sortOff(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',
  'Update docs',
  'Prepare release',
  'Retrospective',
].map((task, i) => ({ id: i + 1, task, owner: owners[i % 4] }));
// rowDrag: true adds a drag handle column at the front; drag a handle to reorder rows (uses the moveRows API).
// With rows selected, dragging the handle of a selected row moves all selected rows together.
export const options = {
  columns: [
    { id: 'task', title: 'Task', width: 180 },
    { id: 'owner', title: 'Owner', width: 120 },
  ],
  selection: 'multiple',
  rowDrag: true,
  rows,
  rowKey: 'id',
};
export function setup(api, log) {
  log('Drag the handle (⠿) on the left to move a row. Select a few rows and drag one of their handles to move them together.');
  const offs = [
    api.on('rowDragStarted', (e) => log(`Drag started: ${e.keys.length} ${e.keys.length === 1 ? 'row' : 'rows'}`)),
    api.on('rowDropped', (e) => log(`Dropped: ${e.keys.join(', ')} → ${e.targetKey === null ? 'the end' : `${e.position} row ${e.targetKey}`}`)),
    api.on('rowDragCancelled', (e) => log(`Cancelled (${e.reason})`)),
  ];
  return () => offs.forEach((off) => off());
}
export const actions = {
  select: (api) => api.setSelectedKeys([2, 3]),
  sortOn: (api) => api.setSortModel([{ columnId: 'task', direction: 'asc' }]),
  sortOff: (api) => api.setSortModel([]),
  undo: (api) => {
    api.undo();
  },
};