셀 범위를 끌어서 옮기기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.
코드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)
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<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>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));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();
},
};<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>셀 범위를 끌어서 옮기기</title>
</head>
<body>
<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>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
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라서 끌지도, 놓지도 못해요.
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',
};
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());
}
const actions = {
select: (api) => {
api.selectCells({ rowKey: 1, columnId: 'name' }, { rowKey: 2, columnId: 'plan' });
},
undo: (api) => {
api.undo();
},
};
options.ariaLabel = "셀 범위를 끌어서 옮기기";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
document.getElementById('select').addEventListener('click', () => actions.select(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));
</script>
</body>
</html>버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<button
type="button"
disabled={!api}
onClick={() => api && actions.select(api)}
>이름·요금제 2×2 선택</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.undo(api)}
>되돌리기</button>
</div>
<Grid style={{ height: 330 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
plan: string;
seats: number;
note: string;
}
const rows: Row[] = [
{ 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: GridOptions<Row> = {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => {
api.selectCells({ rowKey: 1, columnId: 'name' }, { rowKey: 2, columnId: 'plan' });
},
undo: (api: GridApi<Row>) => {
api.undo();
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<button type="button" :disabled="!api" @click="api && actions.select(api)">이름·요금제 2×2 선택</button>
<button type="button" :disabled="!api" @click="api && actions.undo(api)">되돌리기</button>
</div>
<Grid style="height: 330px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
plan: string;
seats: number;
note: string;
}
const rows: Row[] = [
{ 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: GridOptions<Row> = {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => {
api.selectCells({ rowKey: 1, columnId: 'name' }, { rowKey: 2, columnId: 'plan' });
},
undo: (api: GridApi<Row>) => {
api.undo();
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<button
type="button"
[disabled]="!api"
(click)="api && actions.select(api)"
>이름·요금제 2×2 선택</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.undo(api)"
>되돌리기</button>
</div>
<mx-grid style="height: 330px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
plan: string;
seats: number;
note: string;
}
const rows: Row[] = [
{ 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: GridOptions<Row> = {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => {
api.selectCells({ rowKey: 1, columnId: 'name' }, { rowKey: 2, columnId: 'plan' });
},
undo: (api: GridApi<Row>) => {
api.undo();
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<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>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));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();
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Move a cell range by dragging</title>
</head>
<body>
<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>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
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.
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',
};
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());
}
const actions = {
select: (api) => {
api.selectCells({ rowKey: 1, columnId: 'name' }, { rowKey: 2, columnId: 'plan' });
},
undo: (api) => {
api.undo();
},
};
options.ariaLabel = "Move a cell range by dragging";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
document.getElementById('select').addEventListener('click', () => actions.select(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));
</script>
</body>
</html>When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<button
type="button"
disabled={!api}
onClick={() => api && actions.select(api)}
>Select 2×2</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.undo(api)}
>Undo</button>
</div>
<Grid style={{ height: 330 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
plan: string;
seats: number;
note: string;
}
const rows: Row[] = [
{ 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: GridOptions<Row> = {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => {
api.selectCells({ rowKey: 1, columnId: 'name' }, { rowKey: 2, columnId: 'plan' });
},
undo: (api: GridApi<Row>) => {
api.undo();
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<button type="button" :disabled="!api" @click="api && actions.select(api)">Select 2×2</button>
<button type="button" :disabled="!api" @click="api && actions.undo(api)">Undo</button>
</div>
<Grid style="height: 330px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
plan: string;
seats: number;
note: string;
}
const rows: Row[] = [
{ 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: GridOptions<Row> = {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => {
api.selectCells({ rowKey: 1, columnId: 'name' }, { rowKey: 2, columnId: 'plan' });
},
undo: (api: GridApi<Row>) => {
api.undo();
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<button
type="button"
[disabled]="!api"
(click)="api && actions.select(api)"
>Select 2×2</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.undo(api)"
>Undo</button>
</div>
<mx-grid style="height: 330px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
plan: string;
seats: number;
note: string;
}
const rows: Row[] = [
{ 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: GridOptions<Row> = {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => {
api.selectCells({ rowKey: 1, columnId: 'name' }, { rowKey: 2, columnId: 'plan' });
},
undo: (api: GridApi<Row>) => {
api.undo();
},
};