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

셀 범위를 끌어서 옮기기Move a cell range by dragging

cellDrag를 켜면 이미 고른 셀 범위를 끌어서 같은 크기로 옮겨요. 덮이지 않는 원본 칸은 비워지고, 되돌리기는 한 단계예요. 메모 열은 cellDraggable: false로 막았어요.With cellDrag on, you can drag an already selected cell range to move it to a rectangle of the same size. Source cells that are not covered are cleared and undo is one step. The note column is blocked with cellDraggable: false.

활용Intermediate
직접 써 보기Live preview셀 범위를 끌어서 옮기기Move a cell range 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×2 선택</button>
    <button type="button" id="undo">되돌리기</button>
  </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('select').addEventListener('click', () => actions.select(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));
example.js
const rows = [
  { id: 1, name: '김서연', plan: '기본', seats: 3, note: '신규' },
  { id: 2, name: '박지훈', plan: '팀', seats: 12, note: '' },
  { id: 3, name: '이도윤', plan: '기본', seats: 5, note: '갱신' },
  { id: 4, name: '최하은', plan: '기업', seats: 40, note: '' },
  { id: 5, name: '정우진', plan: '팀', seats: 8, note: '' },
  { id: 6, name: '한소희', plan: '기본', seats: 2, note: '' },
];
// cellDrag: true는 "이동"이에요. 이미 고른 범위 안을 눌러 끌면 같은 크기로 옮겨지고, 원본 중 덮이지 않는 칸은 비워져요.
// 메모 열은 cellDraggable: false라서 끌지도, 놓지도 못해요.
export const options = {
  columns: [
    { id: 'name', title: '이름', width: 110, editable: true },
    { id: 'plan', title: '요금제', width: 100, editable: true },
    { id: 'seats', title: '좌석', kind: 'number', width: 90, editable: true },
    { id: 'note', title: '메모', width: 110, editable: true, cellDraggable: false },
  ],
  cellSelection: 'multiple',
  cellDrag: true,
  rows,
  rowKey: 'id',
};
export function setup(api, log) {
  log({
    ko: '이름·요금제 칸 몇 개를 끌어서 고른 뒤, 고른 칸 안을 눌러 다른 줄로 끌어 보세요.',
    en: 'Drag to select a few name and plan cells, then press inside the selection and drag it to other rows.',
  });
  const offs = [
    api.on('cellsDragStarted', (e) => log({ ko: `끌기 시작 (${e.mode})`, en: `Drag started (${e.mode})` })),
    api.on('cellsDropped', (e) => log({
      ko: `${e.mode === 'move' ? '옮겼어요' : '복사했어요'}: 목표 ${e.count}칸 씀${e.mode === 'move' ? '(원본은 비움)' : ''}, 놓지 못한 칸 ${e.rejected.length}개${e.rejected[0] ? ` (${e.rejected[0].reason})` : ''}`,
      en: `${e.mode === 'move' ? 'Moved' : 'Copied'}: ${e.count} ${e.count === 1 ? 'cell' : 'cells'} written${e.mode === 'move' ? ' (sources cleared)' : ''}, ${e.rejected.length} refused${e.rejected[0] ? ` (${e.rejected[0].reason})` : ''}`,
    })),
    api.on('cellsDragCancelled', (e) => log({ ko: `취소했어요 (${e.reason})`, en: `Cancelled (${e.reason})` })),
  ];
  return () => offs.forEach((off) => off());
}
export const actions = {
  select: (api) => {
    api.selectCells({ rowKey: 1, columnId: 'name' }, { rowKey: 2, columnId: 'plan' });
  },
  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 2×2</button>
    <button type="button" id="undo">Undo</button>
  </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('select').addEventListener('click', () => actions.select(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));
example.js
const rows = [
  { id: 1, name: 'Liam Carter', plan: 'Basic', seats: 3, note: 'New' },
  { id: 2, name: 'Emma Stone', plan: 'Team', seats: 12, note: '' },
  { id: 3, name: 'Noah Kim', plan: 'Basic', seats: 5, note: 'Renewal' },
  { id: 4, name: 'Olivia Park', plan: 'Enterprise', seats: 40, note: '' },
  { id: 5, name: 'Ethan Cole', plan: 'Team', seats: 8, note: '' },
  { id: 6, name: 'Sophia Reed', plan: 'Basic', seats: 2, note: '' },
];
// cellDrag: true means move. Press inside an already selected range and drag it: it moves as a same-size block and source cells that are not covered are cleared.
// The note column has cellDraggable: false, so it can be neither dragged nor dropped onto.
export const options = {
  columns: [
    { id: 'name', title: 'Name', width: 110, editable: true },
    { id: 'plan', title: 'Plan', width: 100, editable: true },
    { id: 'seats', title: 'Seats', kind: 'number', width: 90, editable: true },
    { id: 'note', title: 'Note', width: 110, editable: true, cellDraggable: false },
  ],
  cellSelection: 'multiple',
  cellDrag: true,
  rows,
  rowKey: 'id',
};
export function setup(api, log) {
  log('Drag to select a few name and plan cells, then press inside the selection and drag it to other rows.');
  const offs = [
    api.on('cellsDragStarted', (e) => log(`Drag started (${e.mode})`)),
    api.on('cellsDropped', (e) => log(`${e.mode === 'move' ? 'Moved' : 'Copied'}: ${e.count} ${e.count === 1 ? 'cell' : 'cells'} written${e.mode === 'move' ? ' (sources cleared)' : ''}, ${e.rejected.length} refused${e.rejected[0] ? ` (${e.rejected[0].reason})` : ''}`)),
    api.on('cellsDragCancelled', (e) => log(`Cancelled (${e.reason})`)),
  ];
  return () => offs.forEach((off) => off());
}
export const actions = {
  select: (api) => {
    api.selectCells({ rowKey: 1, columnId: 'name' }, { rowKey: 2, columnId: 'plan' });
  },
  undo: (api) => {
    api.undo();
  },
};