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

셀 범위를 끌어서 복사하기Copy a cell range by dragging

cellDrag의 mode를 'copy'로 두면 끌어 놓은 곳에 값이 복사되고 원본은 그대로예요. 놓을 때 Ctrl(Mac은 Cmd)을 누르면 이동이 돼요.With cellDrag mode 'copy', the dropped values are copied and the source stays. Hold Ctrl (Cmd on Mac) at drop time to move instead.

활용Intermediate
직접 써 보기Live preview셀 범위를 끌어서 복사하기Copy 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: { mode: 'copy' }는 "복사"예요. 이미 고른 범위 안을 눌러 끌어 놓으면 원본은 그대로 두고 값이 복사돼요. 놓을 때 Ctrl(Mac은 Cmd)을 누르면 이동이 돼요.
// 메모 열은 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: { mode: 'copy' },
  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.count}칸 씀, 놓지 못한 칸 ${e.rejected.length}개${e.rejected[0] ? ` (${e.rejected[0].reason})` : ''}`,
      en: `Dropped: ${e.count} cells written, ${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: { mode: 'copy' } means copy. Press inside an already selected range and drag it: the source stays and the values are copied. Hold Ctrl (Cmd on Mac) when dropping to move instead.
// 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: { mode: 'copy' },
  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(`Dropped: ${e.count} cells written, ${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();
  },
};