두 그리드 사이에서 행 끌어 놓기Drag rows between two grids
rowDrag의 group과 accept로 두 그리드가 서로의 행을 받아요. 받는 쪽의 mode가 move면 원본에서 지워지고 copy면 남아요. 행 키가 겹치면 받지 않아요.With rowDrag group and accept, two grids accept each other's rows. If the receiving mode is move the source row is removed, with copy it stays. Rows whose keys collide are refused.
코드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="collide">키 겹치게 하기</button>
</div>
<div id="grid" style="height: 360px"></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('collide').addEventListener('click', () => actions.collide(api));// 두 그리드는 group과 accept로 서로를 알아요. group: 'todo' 그리드는 'done'의 행을, 'done' 그리드는 'todo'의 행을 받아요.
// 받는 쪽의 mode가 'move'(기본)이면 원본에서 지워지고 'copy'면 남아요. 두 그리드의 행 키(id)는 겹치지 않아야 해요.
export const options = {
columns: [{ id: 'task', title: '할 일', width: 220 }],
rowDrag: { group: 'todo', accept: ['done'] },
rows: [
{ id: 1, task: '요구사항 정리' },
{ id: 2, task: '화면 설계' },
{ id: 3, task: 'API 구현' },
],
rowKey: 'id',
};
let second = null;
let host = null;
export function setup(api, log) {
// 갤러리 틀이 위 그리드를 meta.height 높이로 만들어 두므로 둘이 함께 보이도록 줄여요.
const first = document.getElementById('grid');
if (first !== null)
first.style.height = '170px';
host = document.createElement('div');
host.style.cssText = 'height:170px;margin-top:10px';
if (first !== null)
first.after(host);
else
document.body.append(host);
second = MaxGrid.createGrid(host, {
columns: [{ id: 'task', title: '끝낸 일', width: 220 }],
rowDrag: { group: 'done', accept: ['todo'], mode: 'move' },
rows: [{ id: 101, task: '킥오프' }],
rowKey: 'id',
ariaLabel: 'done grid',
});
log({
ko: '위 그리드의 행을 아래 그리드로 끌어 놓아 보세요. 받는 쪽에 더해지고 원본에서 사라져요. 아래 행도 위로 끌 수 있어요.',
en: 'Drag a row from the top grid into the bottom grid: it is added there and removed from the source. Rows can go back up too.',
});
const offs = [api, second].map((grid, i) => grid.on('rowDropped', (e) => log({
ko: `${i === 0 ? '위' : '아래'} 그리드 ${e.external ? '받음' : '보냄'}: ${e.keys.join(', ')} (${e.mode})`,
en: `${i === 0 ? 'Top' : 'Bottom'} grid ${e.external ? 'received' : 'sent'}: ${e.keys.join(', ')} (${e.mode})`,
})));
return () => {
offs.forEach((off) => off());
second?.destroy();
host?.remove();
second = null;
host = null;
};
}
export const actions = {
collide: () => {
// 같은 키(1)가 이미 있는 행을 아래로 보내 키 충돌 취소를 보여 줘요.
if (second?.getRows().some((r) => r.id === 1))
return;
second?.addRows([{ id: 1, task: '키가 같은 행' }]);
},
};<!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="collide">키 겹치게 하기</button>
</div>
<div id="grid" style="height: 360px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
// 두 그리드는 group과 accept로 서로를 알아요. group: 'todo' 그리드는 'done'의 행을, 'done' 그리드는 'todo'의 행을 받아요.
// 받는 쪽의 mode가 'move'(기본)이면 원본에서 지워지고 'copy'면 남아요. 두 그리드의 행 키(id)는 겹치지 않아야 해요.
const options = {
columns: [{ id: 'task', title: '할 일', width: 220 }],
rowDrag: { group: 'todo', accept: ['done'] },
rows: [
{ id: 1, task: '요구사항 정리' },
{ id: 2, task: '화면 설계' },
{ id: 3, task: 'API 구현' },
],
rowKey: 'id',
};
let second = null;
let host = null;
function setup(api, log) {
// 갤러리 틀이 위 그리드를 meta.height 높이로 만들어 두므로 둘이 함께 보이도록 줄여요.
const first = document.getElementById('grid');
if (first !== null)
first.style.height = '170px';
host = document.createElement('div');
host.style.cssText = 'height:170px;margin-top:10px';
if (first !== null)
first.after(host);
else
document.body.append(host);
second = MaxGrid.createGrid(host, {
columns: [{ id: 'task', title: '끝낸 일', width: 220 }],
rowDrag: { group: 'done', accept: ['todo'], mode: 'move' },
rows: [{ id: 101, task: '킥오프' }],
rowKey: 'id',
ariaLabel: 'done grid',
});
log({
ko: '위 그리드의 행을 아래 그리드로 끌어 놓아 보세요. 받는 쪽에 더해지고 원본에서 사라져요. 아래 행도 위로 끌 수 있어요.',
en: 'Drag a row from the top grid into the bottom grid: it is added there and removed from the source. Rows can go back up too.',
});
const offs = [api, second].map((grid, i) => grid.on('rowDropped', (e) => log({
ko: `${i === 0 ? '위' : '아래'} 그리드 ${e.external ? '받음' : '보냄'}: ${e.keys.join(', ')} (${e.mode})`,
en: `${i === 0 ? 'Top' : 'Bottom'} grid ${e.external ? 'received' : 'sent'}: ${e.keys.join(', ')} (${e.mode})`,
})));
return () => {
offs.forEach((off) => off());
second?.destroy();
host?.remove();
second = null;
host = null;
};
}
const actions = {
collide: () => {
// 같은 키(1)가 이미 있는 행을 아래로 보내 키 충돌 취소를 보여 줘요.
if (second?.getRows().some((r) => r.id === 1))
return;
second?.addRows([{ id: 1, task: '키가 같은 행' }]);
},
};
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('collide').addEventListener('click', () => actions.collide(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.collide(api)}
>키 겹치게 하기</button>
</div>
<Grid style={{ height: 360 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
task: string;
}
// 갤러리 틀이 불러 둔 전역 MaxGrid. 아래 두 번째 그리드를 만들 때 써요.
declare const MaxGrid: {
createGrid(container: HTMLElement, options: GridOptions<Row>): GridApi<Row>;
};
// 두 그리드는 group과 accept로 서로를 알아요. group: 'todo' 그리드는 'done'의 행을, 'done' 그리드는 'todo'의 행을 받아요.
// 받는 쪽의 mode가 'move'(기본)이면 원본에서 지워지고 'copy'면 남아요. 두 그리드의 행 키(id)는 겹치지 않아야 해요.
export const options: GridOptions<Row> = {
columns: [{ id: 'task', title: '할 일', width: 220 }],
rowDrag: { group: 'todo', accept: ['done'] },
rows: [
{ id: 1, task: '요구사항 정리' },
{ id: 2, task: '화면 설계' },
{ id: 3, task: 'API 구현' },
],
rowKey: 'id',
};
let second: GridApi<Row> | null = null;
let host: HTMLElement | null = null;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
// 갤러리 틀이 위 그리드를 meta.height 높이로 만들어 두므로 둘이 함께 보이도록 줄여요.
const first = document.getElementById('grid');
if (first !== null) first.style.height = '170px';
host = document.createElement('div');
host.style.cssText = 'height:170px;margin-top:10px';
if (first !== null) first.after(host);
else document.body.append(host);
second = MaxGrid.createGrid(host, {
columns: [{ id: 'task', title: '끝낸 일', width: 220 }],
rowDrag: { group: 'done', accept: ['todo'], mode: 'move' },
rows: [{ id: 101, task: '킥오프' }],
rowKey: 'id',
ariaLabel: 'done grid',
});
log({
ko: '위 그리드의 행을 아래 그리드로 끌어 놓아 보세요. 받는 쪽에 더해지고 원본에서 사라져요. 아래 행도 위로 끌 수 있어요.',
en: 'Drag a row from the top grid into the bottom grid: it is added there and removed from the source. Rows can go back up too.',
});
const offs = [api, second].map((grid, i) =>
grid.on('rowDropped', (e) =>
log({
ko: `${i === 0 ? '위' : '아래'} 그리드 ${e.external ? '받음' : '보냄'}: ${e.keys.join(', ')} (${e.mode})`,
en: `${i === 0 ? 'Top' : 'Bottom'} grid ${e.external ? 'received' : 'sent'}: ${e.keys.join(', ')} (${e.mode})`,
}),
),
);
return () => {
offs.forEach((off) => off());
second?.destroy();
host?.remove();
second = null;
host = null;
};
}
export const actions = {
collide: () => {
// 같은 키(1)가 이미 있는 행을 아래로 보내 키 충돌 취소를 보여 줘요.
if (second?.getRows().some((r) => r.id === 1)) return;
second?.addRows([{ id: 1, task: '키가 같은 행' }]);
},
};버튼을 누르면 이 예제의 코드가 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.collide(api)">키 겹치게 하기</button>
</div>
<Grid style="height: 360px" :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;
task: string;
}
// 갤러리 틀이 불러 둔 전역 MaxGrid. 아래 두 번째 그리드를 만들 때 써요.
declare const MaxGrid: {
createGrid(container: HTMLElement, options: GridOptions<Row>): GridApi<Row>;
};
// 두 그리드는 group과 accept로 서로를 알아요. group: 'todo' 그리드는 'done'의 행을, 'done' 그리드는 'todo'의 행을 받아요.
// 받는 쪽의 mode가 'move'(기본)이면 원본에서 지워지고 'copy'면 남아요. 두 그리드의 행 키(id)는 겹치지 않아야 해요.
export const options: GridOptions<Row> = {
columns: [{ id: 'task', title: '할 일', width: 220 }],
rowDrag: { group: 'todo', accept: ['done'] },
rows: [
{ id: 1, task: '요구사항 정리' },
{ id: 2, task: '화면 설계' },
{ id: 3, task: 'API 구현' },
],
rowKey: 'id',
};
let second: GridApi<Row> | null = null;
let host: HTMLElement | null = null;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
// 갤러리 틀이 위 그리드를 meta.height 높이로 만들어 두므로 둘이 함께 보이도록 줄여요.
const first = document.getElementById('grid');
if (first !== null) first.style.height = '170px';
host = document.createElement('div');
host.style.cssText = 'height:170px;margin-top:10px';
if (first !== null) first.after(host);
else document.body.append(host);
second = MaxGrid.createGrid(host, {
columns: [{ id: 'task', title: '끝낸 일', width: 220 }],
rowDrag: { group: 'done', accept: ['todo'], mode: 'move' },
rows: [{ id: 101, task: '킥오프' }],
rowKey: 'id',
ariaLabel: 'done grid',
});
log({
ko: '위 그리드의 행을 아래 그리드로 끌어 놓아 보세요. 받는 쪽에 더해지고 원본에서 사라져요. 아래 행도 위로 끌 수 있어요.',
en: 'Drag a row from the top grid into the bottom grid: it is added there and removed from the source. Rows can go back up too.',
});
const offs = [api, second].map((grid, i) =>
grid.on('rowDropped', (e) =>
log({
ko: `${i === 0 ? '위' : '아래'} 그리드 ${e.external ? '받음' : '보냄'}: ${e.keys.join(', ')} (${e.mode})`,
en: `${i === 0 ? 'Top' : 'Bottom'} grid ${e.external ? 'received' : 'sent'}: ${e.keys.join(', ')} (${e.mode})`,
}),
),
);
return () => {
offs.forEach((off) => off());
second?.destroy();
host?.remove();
second = null;
host = null;
};
}
export const actions = {
collide: () => {
// 같은 키(1)가 이미 있는 행을 아래로 보내 키 충돌 취소를 보여 줘요.
if (second?.getRows().some((r) => r.id === 1)) return;
second?.addRows([{ id: 1, task: '키가 같은 행' }]);
},
};버튼을 누르면 이 예제의 코드가 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.collide(api)"
>키 겹치게 하기</button>
</div>
<mx-grid style="height: 360px" [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;
task: string;
}
// 갤러리 틀이 불러 둔 전역 MaxGrid. 아래 두 번째 그리드를 만들 때 써요.
declare const MaxGrid: {
createGrid(container: HTMLElement, options: GridOptions<Row>): GridApi<Row>;
};
// 두 그리드는 group과 accept로 서로를 알아요. group: 'todo' 그리드는 'done'의 행을, 'done' 그리드는 'todo'의 행을 받아요.
// 받는 쪽의 mode가 'move'(기본)이면 원본에서 지워지고 'copy'면 남아요. 두 그리드의 행 키(id)는 겹치지 않아야 해요.
export const options: GridOptions<Row> = {
columns: [{ id: 'task', title: '할 일', width: 220 }],
rowDrag: { group: 'todo', accept: ['done'] },
rows: [
{ id: 1, task: '요구사항 정리' },
{ id: 2, task: '화면 설계' },
{ id: 3, task: 'API 구현' },
],
rowKey: 'id',
};
let second: GridApi<Row> | null = null;
let host: HTMLElement | null = null;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
// 갤러리 틀이 위 그리드를 meta.height 높이로 만들어 두므로 둘이 함께 보이도록 줄여요.
const first = document.getElementById('grid');
if (first !== null) first.style.height = '170px';
host = document.createElement('div');
host.style.cssText = 'height:170px;margin-top:10px';
if (first !== null) first.after(host);
else document.body.append(host);
second = MaxGrid.createGrid(host, {
columns: [{ id: 'task', title: '끝낸 일', width: 220 }],
rowDrag: { group: 'done', accept: ['todo'], mode: 'move' },
rows: [{ id: 101, task: '킥오프' }],
rowKey: 'id',
ariaLabel: 'done grid',
});
log({
ko: '위 그리드의 행을 아래 그리드로 끌어 놓아 보세요. 받는 쪽에 더해지고 원본에서 사라져요. 아래 행도 위로 끌 수 있어요.',
en: 'Drag a row from the top grid into the bottom grid: it is added there and removed from the source. Rows can go back up too.',
});
const offs = [api, second].map((grid, i) =>
grid.on('rowDropped', (e) =>
log({
ko: `${i === 0 ? '위' : '아래'} 그리드 ${e.external ? '받음' : '보냄'}: ${e.keys.join(', ')} (${e.mode})`,
en: `${i === 0 ? 'Top' : 'Bottom'} grid ${e.external ? 'received' : 'sent'}: ${e.keys.join(', ')} (${e.mode})`,
}),
),
);
return () => {
offs.forEach((off) => off());
second?.destroy();
host?.remove();
second = null;
host = null;
};
}
export const actions = {
collide: () => {
// 같은 키(1)가 이미 있는 행을 아래로 보내 키 충돌 취소를 보여 줘요.
if (second?.getRows().some((r) => r.id === 1)) return;
second?.addRows([{ id: 1, task: '키가 같은 행' }]);
},
};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="collide">Make a key collision</button>
</div>
<div id="grid" style="height: 360px"></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('collide').addEventListener('click', () => actions.collide(api));// The two grids know each other through group and accept. The 'todo' grid accepts rows from 'done', and the 'done' grid accepts rows from 'todo'.
// If the receiving side's mode is 'move' (the default) the row is removed from the source, and with 'copy' it stays. The row keys (id) of the two grids must not overlap.
export const options = {
columns: [{ id: 'task', title: 'To do', width: 220 }],
rowDrag: { group: 'todo', accept: ['done'] },
rows: [
{ id: 1, task: 'Gather requirements' },
{ id: 2, task: 'Design screens' },
{ id: 3, task: 'Build the API' },
],
rowKey: 'id',
};
let second = null;
let host = null;
export function setup(api, log) {
// The gallery frame makes the top grid with the meta.height height, so shrink it to show both together.
const first = document.getElementById('grid');
if (first !== null)
first.style.height = '170px';
host = document.createElement('div');
host.style.cssText = 'height:170px;margin-top:10px';
if (first !== null)
first.after(host);
else
document.body.append(host);
second = MaxGrid.createGrid(host, {
columns: [{ id: 'task', title: 'Done', width: 220 }],
rowDrag: { group: 'done', accept: ['todo'], mode: 'move' },
rows: [{ id: 101, task: 'Kickoff' }],
rowKey: 'id',
ariaLabel: 'done grid',
});
log('Drag a row from the top grid into the bottom grid: it is added there and removed from the source. Rows can go back up too.');
const offs = [api, second].map((grid, i) => grid.on('rowDropped', (e) => log(`${i === 0 ? 'Top' : 'Bottom'} grid ${e.external ? 'received' : 'sent'}: ${e.keys.join(', ')} (${e.mode})`)));
return () => {
offs.forEach((off) => off());
second?.destroy();
host?.remove();
second = null;
host = null;
};
}
export const actions = {
collide: () => {
// Send a row whose key (1) already exists down, to show the key-collision cancel.
if (second?.getRows().some((r) => r.id === 1))
return;
second?.addRows([{ id: 1, task: 'Row with the same key' }]);
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Drag rows between two grids</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="collide">Make a key collision</button>
</div>
<div id="grid" style="height: 360px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
// The two grids know each other through group and accept. The 'todo' grid accepts rows from 'done', and the 'done' grid accepts rows from 'todo'.
// If the receiving side's mode is 'move' (the default) the row is removed from the source, and with 'copy' it stays. The row keys (id) of the two grids must not overlap.
const options = {
columns: [{ id: 'task', title: 'To do', width: 220 }],
rowDrag: { group: 'todo', accept: ['done'] },
rows: [
{ id: 1, task: 'Gather requirements' },
{ id: 2, task: 'Design screens' },
{ id: 3, task: 'Build the API' },
],
rowKey: 'id',
};
let second = null;
let host = null;
function setup(api, log) {
// The gallery frame makes the top grid with the meta.height height, so shrink it to show both together.
const first = document.getElementById('grid');
if (first !== null)
first.style.height = '170px';
host = document.createElement('div');
host.style.cssText = 'height:170px;margin-top:10px';
if (first !== null)
first.after(host);
else
document.body.append(host);
second = MaxGrid.createGrid(host, {
columns: [{ id: 'task', title: 'Done', width: 220 }],
rowDrag: { group: 'done', accept: ['todo'], mode: 'move' },
rows: [{ id: 101, task: 'Kickoff' }],
rowKey: 'id',
ariaLabel: 'done grid',
});
log('Drag a row from the top grid into the bottom grid: it is added there and removed from the source. Rows can go back up too.');
const offs = [api, second].map((grid, i) => grid.on('rowDropped', (e) => log(`${i === 0 ? 'Top' : 'Bottom'} grid ${e.external ? 'received' : 'sent'}: ${e.keys.join(', ')} (${e.mode})`)));
return () => {
offs.forEach((off) => off());
second?.destroy();
host?.remove();
second = null;
host = null;
};
}
const actions = {
collide: () => {
// Send a row whose key (1) already exists down, to show the key-collision cancel.
if (second?.getRows().some((r) => r.id === 1))
return;
second?.addRows([{ id: 1, task: 'Row with the same key' }]);
},
};
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('collide').addEventListener('click', () => actions.collide(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.collide(api)}
>Make a key collision</button>
</div>
<Grid style={{ height: 360 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
task: string;
}
// The global MaxGrid that the gallery frame has loaded. It is used to create the second grid below.
declare const MaxGrid: {
createGrid(container: HTMLElement, options: GridOptions<Row>): GridApi<Row>;
};
// The two grids know each other through group and accept. The 'todo' grid accepts rows from 'done', and the 'done' grid accepts rows from 'todo'.
// If the receiving side's mode is 'move' (the default) the row is removed from the source, and with 'copy' it stays. The row keys (id) of the two grids must not overlap.
export const options: GridOptions<Row> = {
columns: [{ id: 'task', title: 'To do', width: 220 }],
rowDrag: { group: 'todo', accept: ['done'] },
rows: [
{ id: 1, task: 'Gather requirements' },
{ id: 2, task: 'Design screens' },
{ id: 3, task: 'Build the API' },
],
rowKey: 'id',
};
let second: GridApi<Row> | null = null;
let host: HTMLElement | null = null;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
// The gallery frame makes the top grid with the meta.height height, so shrink it to show both together.
const first = document.getElementById('grid');
if (first !== null) first.style.height = '170px';
host = document.createElement('div');
host.style.cssText = 'height:170px;margin-top:10px';
if (first !== null) first.after(host);
else document.body.append(host);
second = MaxGrid.createGrid(host, {
columns: [{ id: 'task', title: 'Done', width: 220 }],
rowDrag: { group: 'done', accept: ['todo'], mode: 'move' },
rows: [{ id: 101, task: 'Kickoff' }],
rowKey: 'id',
ariaLabel: 'done grid',
});
log(
'Drag a row from the top grid into the bottom grid: it is added there and removed from the source. Rows can go back up too.',
);
const offs = [api, second].map((grid, i) =>
grid.on('rowDropped', (e) =>
log(
`${i === 0 ? 'Top' : 'Bottom'} grid ${e.external ? 'received' : 'sent'}: ${e.keys.join(', ')} (${e.mode})`,
),
),
);
return () => {
offs.forEach((off) => off());
second?.destroy();
host?.remove();
second = null;
host = null;
};
}
export const actions = {
collide: () => {
// Send a row whose key (1) already exists down, to show the key-collision cancel.
if (second?.getRows().some((r) => r.id === 1)) return;
second?.addRows([{ id: 1, task: 'Row with the same key' }]);
},
};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.collide(api)">Make a key collision</button>
</div>
<Grid style="height: 360px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
task: string;
}
// The global MaxGrid that the gallery frame has loaded. It is used to create the second grid below.
declare const MaxGrid: {
createGrid(container: HTMLElement, options: GridOptions<Row>): GridApi<Row>;
};
// The two grids know each other through group and accept. The 'todo' grid accepts rows from 'done', and the 'done' grid accepts rows from 'todo'.
// If the receiving side's mode is 'move' (the default) the row is removed from the source, and with 'copy' it stays. The row keys (id) of the two grids must not overlap.
export const options: GridOptions<Row> = {
columns: [{ id: 'task', title: 'To do', width: 220 }],
rowDrag: { group: 'todo', accept: ['done'] },
rows: [
{ id: 1, task: 'Gather requirements' },
{ id: 2, task: 'Design screens' },
{ id: 3, task: 'Build the API' },
],
rowKey: 'id',
};
let second: GridApi<Row> | null = null;
let host: HTMLElement | null = null;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
// The gallery frame makes the top grid with the meta.height height, so shrink it to show both together.
const first = document.getElementById('grid');
if (first !== null) first.style.height = '170px';
host = document.createElement('div');
host.style.cssText = 'height:170px;margin-top:10px';
if (first !== null) first.after(host);
else document.body.append(host);
second = MaxGrid.createGrid(host, {
columns: [{ id: 'task', title: 'Done', width: 220 }],
rowDrag: { group: 'done', accept: ['todo'], mode: 'move' },
rows: [{ id: 101, task: 'Kickoff' }],
rowKey: 'id',
ariaLabel: 'done grid',
});
log(
'Drag a row from the top grid into the bottom grid: it is added there and removed from the source. Rows can go back up too.',
);
const offs = [api, second].map((grid, i) =>
grid.on('rowDropped', (e) =>
log(
`${i === 0 ? 'Top' : 'Bottom'} grid ${e.external ? 'received' : 'sent'}: ${e.keys.join(', ')} (${e.mode})`,
),
),
);
return () => {
offs.forEach((off) => off());
second?.destroy();
host?.remove();
second = null;
host = null;
};
}
export const actions = {
collide: () => {
// Send a row whose key (1) already exists down, to show the key-collision cancel.
if (second?.getRows().some((r) => r.id === 1)) return;
second?.addRows([{ id: 1, task: 'Row with the same key' }]);
},
};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.collide(api)"
>Make a key collision</button>
</div>
<mx-grid style="height: 360px" [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;
export interface Row {
id: number;
task: string;
}
// The global MaxGrid that the gallery frame has loaded. It is used to create the second grid below.
declare const MaxGrid: {
createGrid(container: HTMLElement, options: GridOptions<Row>): GridApi<Row>;
};
// The two grids know each other through group and accept. The 'todo' grid accepts rows from 'done', and the 'done' grid accepts rows from 'todo'.
// If the receiving side's mode is 'move' (the default) the row is removed from the source, and with 'copy' it stays. The row keys (id) of the two grids must not overlap.
export const options: GridOptions<Row> = {
columns: [{ id: 'task', title: 'To do', width: 220 }],
rowDrag: { group: 'todo', accept: ['done'] },
rows: [
{ id: 1, task: 'Gather requirements' },
{ id: 2, task: 'Design screens' },
{ id: 3, task: 'Build the API' },
],
rowKey: 'id',
};
let second: GridApi<Row> | null = null;
let host: HTMLElement | null = null;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
// The gallery frame makes the top grid with the meta.height height, so shrink it to show both together.
const first = document.getElementById('grid');
if (first !== null) first.style.height = '170px';
host = document.createElement('div');
host.style.cssText = 'height:170px;margin-top:10px';
if (first !== null) first.after(host);
else document.body.append(host);
second = MaxGrid.createGrid(host, {
columns: [{ id: 'task', title: 'Done', width: 220 }],
rowDrag: { group: 'done', accept: ['todo'], mode: 'move' },
rows: [{ id: 101, task: 'Kickoff' }],
rowKey: 'id',
ariaLabel: 'done grid',
});
log(
'Drag a row from the top grid into the bottom grid: it is added there and removed from the source. Rows can go back up too.',
);
const offs = [api, second].map((grid, i) =>
grid.on('rowDropped', (e) =>
log(
`${i === 0 ? 'Top' : 'Bottom'} grid ${e.external ? 'received' : 'sent'}: ${e.keys.join(', ')} (${e.mode})`,
),
),
);
return () => {
offs.forEach((off) => off());
second?.destroy();
host?.remove();
second = null;
host = null;
};
}
export const actions = {
collide: () => {
// Send a row whose key (1) already exists down, to show the key-collision cancel.
if (second?.getRows().some((r) => r.id === 1)) return;
second?.addRows([{ id: 1, task: 'Row with the same key' }]);
},
};