트리에서 행을 다른 부모 아래로 옮기기Move rows under another parent in a tree
moveRows의 childOf·before·index로 조직을 다른 부모 아래나 원하는 자리로 옮겨요. 가지째 움직이고, 자기 자손 안으로는 옮길 수 없어요.moveRows with childOf, before or index moves units under another parent or to a place you choose. The branch goes along, and a move into your own descendants is 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="underMobile">모바일팀 아래로</button>
<button type="button" id="firstUnderDesign">디자인본부 맨 앞 자식으로</button>
<button type="button" id="beforeDesign">디자인본부 앞으로</button>
<button type="button" id="toTop">맨 위로</button>
<button type="button" id="intoSelf">인프라파트 아래로(자손이면 막힘)</button>
<button type="button" id="undo">되돌리기</button>
</div>
<div id="grid" style="height: 380px"></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('underMobile').addEventListener('click', () => actions.underMobile(api));
document.getElementById('firstUnderDesign').addEventListener('click', () => actions.firstUnderDesign(api));
document.getElementById('beforeDesign').addEventListener('click', () => actions.beforeDesign(api));
document.getElementById('toTop').addEventListener('click', () => actions.toTop(api));
document.getElementById('intoSelf').addEventListener('click', () => actions.intoSelf(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));const rows = [
{ id: 1, parentId: null, name: '개발본부' },
{ id: 2, parentId: 1, name: '플랫폼팀' },
{ id: 3, parentId: 2, name: '인프라파트' },
{ id: 4, parentId: 2, name: '데이터파트' },
{ id: 5, parentId: 1, name: '모바일팀' },
{ id: 6, parentId: null, name: '디자인본부' },
{ id: 7, parentId: 6, name: '브랜드팀' },
];
export const options = {
columns: [
{ id: 'name', title: '조직', width: 180 },
{ id: 'parentId', title: '부모', width: 80, align: 'end' },
],
rows,
rowKey: 'id',
selection: 'multiple',
tree: { parentKey: 'parentId', column: 'name', expandDepth: 'all' },
};
let say = () => { };
export function setup(api, log) {
say = log;
const off = api.on('rowsMoved', (e) => say({
ko: `옮김 ${e.keys.join(', ')} → 부모 ${e.parentKey ?? '없음'}, ${e.index + 1}번째`,
en: `Moved ${e.keys.join(', ')} → parent ${e.parentKey ?? 'none'}, position ${e.index + 1}`,
}));
return () => off();
}
function run(api, move) {
const keys = api.getSelectedKeys();
if (keys.length === 0) {
say({ ko: '먼저 옮길 조직을 골라 주세요', en: 'Select the units to move first' });
return;
}
try {
move(keys);
}
catch (error) {
say({ ko: error.message, en: error.message });
}
}
export const actions = {
underMobile: (api) => run(api, (k) => api.moveRows(k, { childOf: 5 })),
firstUnderDesign: (api) => run(api, (k) => api.moveRows(k, { childOf: 6, index: 0 })),
beforeDesign: (api) => run(api, (k) => api.moveRows(k, { before: 6 })),
toTop: (api) => run(api, (k) => api.moveRows(k, { index: 0 })),
intoSelf: (api) => run(api, (k) => api.moveRows(k, { childOf: 3 })),
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="underMobile">모바일팀 아래로</button>
<button type="button" id="firstUnderDesign">디자인본부 맨 앞 자식으로</button>
<button type="button" id="beforeDesign">디자인본부 앞으로</button>
<button type="button" id="toTop">맨 위로</button>
<button type="button" id="intoSelf">인프라파트 아래로(자손이면 막힘)</button>
<button type="button" id="undo">되돌리기</button>
</div>
<div id="grid" style="height: 380px"></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, parentId: null, name: '개발본부' },
{ id: 2, parentId: 1, name: '플랫폼팀' },
{ id: 3, parentId: 2, name: '인프라파트' },
{ id: 4, parentId: 2, name: '데이터파트' },
{ id: 5, parentId: 1, name: '모바일팀' },
{ id: 6, parentId: null, name: '디자인본부' },
{ id: 7, parentId: 6, name: '브랜드팀' },
];
const options = {
columns: [
{ id: 'name', title: '조직', width: 180 },
{ id: 'parentId', title: '부모', width: 80, align: 'end' },
],
rows,
rowKey: 'id',
selection: 'multiple',
tree: { parentKey: 'parentId', column: 'name', expandDepth: 'all' },
};
let say = () => { };
function setup(api, log) {
say = log;
const off = api.on('rowsMoved', (e) => say({
ko: `옮김 ${e.keys.join(', ')} → 부모 ${e.parentKey ?? '없음'}, ${e.index + 1}번째`,
en: `Moved ${e.keys.join(', ')} → parent ${e.parentKey ?? 'none'}, position ${e.index + 1}`,
}));
return () => off();
}
function run(api, move) {
const keys = api.getSelectedKeys();
if (keys.length === 0) {
say({ ko: '먼저 옮길 조직을 골라 주세요', en: 'Select the units to move first' });
return;
}
try {
move(keys);
}
catch (error) {
say({ ko: error.message, en: error.message });
}
}
const actions = {
underMobile: (api) => run(api, (k) => api.moveRows(k, { childOf: 5 })),
firstUnderDesign: (api) => run(api, (k) => api.moveRows(k, { childOf: 6, index: 0 })),
beforeDesign: (api) => run(api, (k) => api.moveRows(k, { before: 6 })),
toTop: (api) => run(api, (k) => api.moveRows(k, { index: 0 })),
intoSelf: (api) => run(api, (k) => api.moveRows(k, { childOf: 3 })),
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('underMobile').addEventListener('click', () => actions.underMobile(api));
document.getElementById('firstUnderDesign').addEventListener('click', () => actions.firstUnderDesign(api));
document.getElementById('beforeDesign').addEventListener('click', () => actions.beforeDesign(api));
document.getElementById('toTop').addEventListener('click', () => actions.toTop(api));
document.getElementById('intoSelf').addEventListener('click', () => actions.intoSelf(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.underMobile(api)}
>모바일팀 아래로</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.firstUnderDesign(api)}
>디자인본부 맨 앞 자식으로</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.beforeDesign(api)}
>디자인본부 앞으로</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.toTop(api)}
>맨 위로</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.intoSelf(api)}
>인프라파트 아래로(자손이면 막힘)</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.undo(api)}
>되돌리기</button>
</div>
<Grid style={{ height: 380 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
parentId: number | null;
name: string;
}
const rows: Row[] = [
{ id: 1, parentId: null, name: '개발본부' },
{ id: 2, parentId: 1, name: '플랫폼팀' },
{ id: 3, parentId: 2, name: '인프라파트' },
{ id: 4, parentId: 2, name: '데이터파트' },
{ id: 5, parentId: 1, name: '모바일팀' },
{ id: 6, parentId: null, name: '디자인본부' },
{ id: 7, parentId: 6, name: '브랜드팀' },
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: '조직', width: 180 },
{ id: 'parentId', title: '부모', width: 80, align: 'end' },
],
rows,
rowKey: 'id',
selection: 'multiple',
tree: { parentKey: 'parentId', column: 'name', expandDepth: 'all' },
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
const off = api.on('rowsMoved', (e) =>
say({
ko: `옮김 ${e.keys.join(', ')} → 부모 ${e.parentKey ?? '없음'}, ${e.index + 1}번째`,
en: `Moved ${e.keys.join(', ')} → parent ${e.parentKey ?? 'none'}, position ${e.index + 1}`,
}),
);
return () => off();
}
function run(api: GridApi<Row>, move: (keys: number[]) => void): void {
const keys = api.getSelectedKeys() as number[];
if (keys.length === 0) {
say({ ko: '먼저 옮길 조직을 골라 주세요', en: 'Select the units to move first' });
return;
}
try {
move(keys);
} catch (error) {
say({ ko: (error as Error).message, en: (error as Error).message });
}
}
export const actions = {
underMobile: (api: GridApi<Row>) => run(api, (k) => api.moveRows(k, { childOf: 5 })),
firstUnderDesign: (api: GridApi<Row>) =>
run(api, (k) => api.moveRows(k, { childOf: 6, index: 0 })),
beforeDesign: (api: GridApi<Row>) => run(api, (k) => api.moveRows(k, { before: 6 })),
toTop: (api: GridApi<Row>) => run(api, (k) => api.moveRows(k, { index: 0 })),
intoSelf: (api: GridApi<Row>) => run(api, (k) => api.moveRows(k, { childOf: 3 })),
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.underMobile(api)"
>모바일팀 아래로</button>
<button
type="button"
:disabled="!api"
@click="api && actions.firstUnderDesign(api)"
>디자인본부 맨 앞 자식으로</button>
<button
type="button"
:disabled="!api"
@click="api && actions.beforeDesign(api)"
>디자인본부 앞으로</button>
<button type="button" :disabled="!api" @click="api && actions.toTop(api)">맨 위로</button>
<button
type="button"
:disabled="!api"
@click="api && actions.intoSelf(api)"
>인프라파트 아래로(자손이면 막힘)</button>
<button type="button" :disabled="!api" @click="api && actions.undo(api)">되돌리기</button>
</div>
<Grid style="height: 380px" :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;
parentId: number | null;
name: string;
}
const rows: Row[] = [
{ id: 1, parentId: null, name: '개발본부' },
{ id: 2, parentId: 1, name: '플랫폼팀' },
{ id: 3, parentId: 2, name: '인프라파트' },
{ id: 4, parentId: 2, name: '데이터파트' },
{ id: 5, parentId: 1, name: '모바일팀' },
{ id: 6, parentId: null, name: '디자인본부' },
{ id: 7, parentId: 6, name: '브랜드팀' },
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: '조직', width: 180 },
{ id: 'parentId', title: '부모', width: 80, align: 'end' },
],
rows,
rowKey: 'id',
selection: 'multiple',
tree: { parentKey: 'parentId', column: 'name', expandDepth: 'all' },
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
const off = api.on('rowsMoved', (e) =>
say({
ko: `옮김 ${e.keys.join(', ')} → 부모 ${e.parentKey ?? '없음'}, ${e.index + 1}번째`,
en: `Moved ${e.keys.join(', ')} → parent ${e.parentKey ?? 'none'}, position ${e.index + 1}`,
}),
);
return () => off();
}
function run(api: GridApi<Row>, move: (keys: number[]) => void): void {
const keys = api.getSelectedKeys() as number[];
if (keys.length === 0) {
say({ ko: '먼저 옮길 조직을 골라 주세요', en: 'Select the units to move first' });
return;
}
try {
move(keys);
} catch (error) {
say({ ko: (error as Error).message, en: (error as Error).message });
}
}
export const actions = {
underMobile: (api: GridApi<Row>) => run(api, (k) => api.moveRows(k, { childOf: 5 })),
firstUnderDesign: (api: GridApi<Row>) =>
run(api, (k) => api.moveRows(k, { childOf: 6, index: 0 })),
beforeDesign: (api: GridApi<Row>) => run(api, (k) => api.moveRows(k, { before: 6 })),
toTop: (api: GridApi<Row>) => run(api, (k) => api.moveRows(k, { index: 0 })),
intoSelf: (api: GridApi<Row>) => run(api, (k) => api.moveRows(k, { childOf: 3 })),
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.underMobile(api)"
>모바일팀 아래로</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.firstUnderDesign(api)"
>디자인본부 맨 앞 자식으로</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.beforeDesign(api)"
>디자인본부 앞으로</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.toTop(api)"
>맨 위로</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.intoSelf(api)"
>인프라파트 아래로(자손이면 막힘)</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.undo(api)"
>되돌리기</button>
</div>
<mx-grid style="height: 380px" [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;
parentId: number | null;
name: string;
}
const rows: Row[] = [
{ id: 1, parentId: null, name: '개발본부' },
{ id: 2, parentId: 1, name: '플랫폼팀' },
{ id: 3, parentId: 2, name: '인프라파트' },
{ id: 4, parentId: 2, name: '데이터파트' },
{ id: 5, parentId: 1, name: '모바일팀' },
{ id: 6, parentId: null, name: '디자인본부' },
{ id: 7, parentId: 6, name: '브랜드팀' },
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: '조직', width: 180 },
{ id: 'parentId', title: '부모', width: 80, align: 'end' },
],
rows,
rowKey: 'id',
selection: 'multiple',
tree: { parentKey: 'parentId', column: 'name', expandDepth: 'all' },
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
const off = api.on('rowsMoved', (e) =>
say({
ko: `옮김 ${e.keys.join(', ')} → 부모 ${e.parentKey ?? '없음'}, ${e.index + 1}번째`,
en: `Moved ${e.keys.join(', ')} → parent ${e.parentKey ?? 'none'}, position ${e.index + 1}`,
}),
);
return () => off();
}
function run(api: GridApi<Row>, move: (keys: number[]) => void): void {
const keys = api.getSelectedKeys() as number[];
if (keys.length === 0) {
say({ ko: '먼저 옮길 조직을 골라 주세요', en: 'Select the units to move first' });
return;
}
try {
move(keys);
} catch (error) {
say({ ko: (error as Error).message, en: (error as Error).message });
}
}
export const actions = {
underMobile: (api: GridApi<Row>) => run(api, (k) => api.moveRows(k, { childOf: 5 })),
firstUnderDesign: (api: GridApi<Row>) =>
run(api, (k) => api.moveRows(k, { childOf: 6, index: 0 })),
beforeDesign: (api: GridApi<Row>) => run(api, (k) => api.moveRows(k, { before: 6 })),
toTop: (api: GridApi<Row>) => run(api, (k) => api.moveRows(k, { index: 0 })),
intoSelf: (api: GridApi<Row>) => run(api, (k) => api.moveRows(k, { childOf: 3 })),
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="underMobile">Under Mobile</button>
<button type="button" id="firstUnderDesign">First child of Design</button>
<button type="button" id="beforeDesign">Before Design</button>
<button type="button" id="toTop">To top</button>
<button type="button" id="intoSelf">Under Infra (refused for ancestors)</button>
<button type="button" id="undo">Undo</button>
</div>
<div id="grid" style="height: 380px"></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('underMobile').addEventListener('click', () => actions.underMobile(api));
document.getElementById('firstUnderDesign').addEventListener('click', () => actions.firstUnderDesign(api));
document.getElementById('beforeDesign').addEventListener('click', () => actions.beforeDesign(api));
document.getElementById('toTop').addEventListener('click', () => actions.toTop(api));
document.getElementById('intoSelf').addEventListener('click', () => actions.intoSelf(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));const rows = [
{ id: 1, parentId: null, name: 'Dev Division' },
{ id: 2, parentId: 1, name: 'Platform Team' },
{ id: 3, parentId: 2, name: 'Infra Part' },
{ id: 4, parentId: 2, name: 'Data Part' },
{ id: 5, parentId: 1, name: 'Mobile Team' },
{ id: 6, parentId: null, name: 'Design Division' },
{ id: 7, parentId: 6, name: 'Brand Team' },
];
export const options = {
columns: [
{ id: 'name', title: 'Unit', width: 180 },
{ id: 'parentId', title: 'Parent', width: 80, align: 'end' },
],
rows,
rowKey: 'id',
selection: 'multiple',
tree: { parentKey: 'parentId', column: 'name', expandDepth: 'all' },
};
let say = () => { };
export function setup(api, log) {
say = log;
const off = api.on('rowsMoved', (e) => say(`Moved ${e.keys.join(', ')} → parent ${e.parentKey ?? 'none'}, position ${e.index + 1}`));
return () => off();
}
function run(api, move) {
const keys = api.getSelectedKeys();
if (keys.length === 0) {
say('Select the units to move first');
return;
}
try {
move(keys);
}
catch (error) {
say(error.message);
}
}
export const actions = {
underMobile: (api) => run(api, (k) => api.moveRows(k, { childOf: 5 })),
firstUnderDesign: (api) => run(api, (k) => api.moveRows(k, { childOf: 6, index: 0 })),
beforeDesign: (api) => run(api, (k) => api.moveRows(k, { before: 6 })),
toTop: (api) => run(api, (k) => api.moveRows(k, { index: 0 })),
intoSelf: (api) => run(api, (k) => api.moveRows(k, { childOf: 3 })),
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 rows under another parent in a tree</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="underMobile">Under Mobile</button>
<button type="button" id="firstUnderDesign">First child of Design</button>
<button type="button" id="beforeDesign">Before Design</button>
<button type="button" id="toTop">To top</button>
<button type="button" id="intoSelf">Under Infra (refused for ancestors)</button>
<button type="button" id="undo">Undo</button>
</div>
<div id="grid" style="height: 380px"></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, parentId: null, name: 'Dev Division' },
{ id: 2, parentId: 1, name: 'Platform Team' },
{ id: 3, parentId: 2, name: 'Infra Part' },
{ id: 4, parentId: 2, name: 'Data Part' },
{ id: 5, parentId: 1, name: 'Mobile Team' },
{ id: 6, parentId: null, name: 'Design Division' },
{ id: 7, parentId: 6, name: 'Brand Team' },
];
const options = {
columns: [
{ id: 'name', title: 'Unit', width: 180 },
{ id: 'parentId', title: 'Parent', width: 80, align: 'end' },
],
rows,
rowKey: 'id',
selection: 'multiple',
tree: { parentKey: 'parentId', column: 'name', expandDepth: 'all' },
};
let say = () => { };
function setup(api, log) {
say = log;
const off = api.on('rowsMoved', (e) => say(`Moved ${e.keys.join(', ')} → parent ${e.parentKey ?? 'none'}, position ${e.index + 1}`));
return () => off();
}
function run(api, move) {
const keys = api.getSelectedKeys();
if (keys.length === 0) {
say('Select the units to move first');
return;
}
try {
move(keys);
}
catch (error) {
say(error.message);
}
}
const actions = {
underMobile: (api) => run(api, (k) => api.moveRows(k, { childOf: 5 })),
firstUnderDesign: (api) => run(api, (k) => api.moveRows(k, { childOf: 6, index: 0 })),
beforeDesign: (api) => run(api, (k) => api.moveRows(k, { before: 6 })),
toTop: (api) => run(api, (k) => api.moveRows(k, { index: 0 })),
intoSelf: (api) => run(api, (k) => api.moveRows(k, { childOf: 3 })),
undo: (api) => {
api.undo();
},
};
options.ariaLabel = "Move rows under another parent in a tree";
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('underMobile').addEventListener('click', () => actions.underMobile(api));
document.getElementById('firstUnderDesign').addEventListener('click', () => actions.firstUnderDesign(api));
document.getElementById('beforeDesign').addEventListener('click', () => actions.beforeDesign(api));
document.getElementById('toTop').addEventListener('click', () => actions.toTop(api));
document.getElementById('intoSelf').addEventListener('click', () => actions.intoSelf(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.underMobile(api)}
>Under Mobile</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.firstUnderDesign(api)}
>First child of Design</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.beforeDesign(api)}
>Before Design</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.toTop(api)}
>To top</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.intoSelf(api)}
>Under Infra (refused for ancestors)</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.undo(api)}
>Undo</button>
</div>
<Grid style={{ height: 380 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
parentId: number | null;
name: string;
}
const rows: Row[] = [
{ id: 1, parentId: null, name: 'Dev Division' },
{ id: 2, parentId: 1, name: 'Platform Team' },
{ id: 3, parentId: 2, name: 'Infra Part' },
{ id: 4, parentId: 2, name: 'Data Part' },
{ id: 5, parentId: 1, name: 'Mobile Team' },
{ id: 6, parentId: null, name: 'Design Division' },
{ id: 7, parentId: 6, name: 'Brand Team' },
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: 'Unit', width: 180 },
{ id: 'parentId', title: 'Parent', width: 80, align: 'end' },
],
rows,
rowKey: 'id',
selection: 'multiple',
tree: { parentKey: 'parentId', column: 'name', expandDepth: 'all' },
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
const off = api.on('rowsMoved', (e) =>
say(`Moved ${e.keys.join(', ')} → parent ${e.parentKey ?? 'none'}, position ${e.index + 1}`),
);
return () => off();
}
function run(api: GridApi<Row>, move: (keys: number[]) => void): void {
const keys = api.getSelectedKeys() as number[];
if (keys.length === 0) {
say('Select the units to move first');
return;
}
try {
move(keys);
} catch (error) {
say((error as Error).message);
}
}
export const actions = {
underMobile: (api: GridApi<Row>) => run(api, (k) => api.moveRows(k, { childOf: 5 })),
firstUnderDesign: (api: GridApi<Row>) =>
run(api, (k) => api.moveRows(k, { childOf: 6, index: 0 })),
beforeDesign: (api: GridApi<Row>) => run(api, (k) => api.moveRows(k, { before: 6 })),
toTop: (api: GridApi<Row>) => run(api, (k) => api.moveRows(k, { index: 0 })),
intoSelf: (api: GridApi<Row>) => run(api, (k) => api.moveRows(k, { childOf: 3 })),
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.underMobile(api)"
>Under Mobile</button>
<button
type="button"
:disabled="!api"
@click="api && actions.firstUnderDesign(api)"
>First child of Design</button>
<button
type="button"
:disabled="!api"
@click="api && actions.beforeDesign(api)"
>Before Design</button>
<button type="button" :disabled="!api" @click="api && actions.toTop(api)">To top</button>
<button
type="button"
:disabled="!api"
@click="api && actions.intoSelf(api)"
>Under Infra (refused for ancestors)</button>
<button type="button" :disabled="!api" @click="api && actions.undo(api)">Undo</button>
</div>
<Grid style="height: 380px" :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;
parentId: number | null;
name: string;
}
const rows: Row[] = [
{ id: 1, parentId: null, name: 'Dev Division' },
{ id: 2, parentId: 1, name: 'Platform Team' },
{ id: 3, parentId: 2, name: 'Infra Part' },
{ id: 4, parentId: 2, name: 'Data Part' },
{ id: 5, parentId: 1, name: 'Mobile Team' },
{ id: 6, parentId: null, name: 'Design Division' },
{ id: 7, parentId: 6, name: 'Brand Team' },
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: 'Unit', width: 180 },
{ id: 'parentId', title: 'Parent', width: 80, align: 'end' },
],
rows,
rowKey: 'id',
selection: 'multiple',
tree: { parentKey: 'parentId', column: 'name', expandDepth: 'all' },
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
const off = api.on('rowsMoved', (e) =>
say(`Moved ${e.keys.join(', ')} → parent ${e.parentKey ?? 'none'}, position ${e.index + 1}`),
);
return () => off();
}
function run(api: GridApi<Row>, move: (keys: number[]) => void): void {
const keys = api.getSelectedKeys() as number[];
if (keys.length === 0) {
say('Select the units to move first');
return;
}
try {
move(keys);
} catch (error) {
say((error as Error).message);
}
}
export const actions = {
underMobile: (api: GridApi<Row>) => run(api, (k) => api.moveRows(k, { childOf: 5 })),
firstUnderDesign: (api: GridApi<Row>) =>
run(api, (k) => api.moveRows(k, { childOf: 6, index: 0 })),
beforeDesign: (api: GridApi<Row>) => run(api, (k) => api.moveRows(k, { before: 6 })),
toTop: (api: GridApi<Row>) => run(api, (k) => api.moveRows(k, { index: 0 })),
intoSelf: (api: GridApi<Row>) => run(api, (k) => api.moveRows(k, { childOf: 3 })),
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.underMobile(api)"
>Under Mobile</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.firstUnderDesign(api)"
>First child of Design</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.beforeDesign(api)"
>Before Design</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.toTop(api)"
>To top</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.intoSelf(api)"
>Under Infra (refused for ancestors)</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.undo(api)"
>Undo</button>
</div>
<mx-grid style="height: 380px" [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;
parentId: number | null;
name: string;
}
const rows: Row[] = [
{ id: 1, parentId: null, name: 'Dev Division' },
{ id: 2, parentId: 1, name: 'Platform Team' },
{ id: 3, parentId: 2, name: 'Infra Part' },
{ id: 4, parentId: 2, name: 'Data Part' },
{ id: 5, parentId: 1, name: 'Mobile Team' },
{ id: 6, parentId: null, name: 'Design Division' },
{ id: 7, parentId: 6, name: 'Brand Team' },
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: 'Unit', width: 180 },
{ id: 'parentId', title: 'Parent', width: 80, align: 'end' },
],
rows,
rowKey: 'id',
selection: 'multiple',
tree: { parentKey: 'parentId', column: 'name', expandDepth: 'all' },
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
const off = api.on('rowsMoved', (e) =>
say(`Moved ${e.keys.join(', ')} → parent ${e.parentKey ?? 'none'}, position ${e.index + 1}`),
);
return () => off();
}
function run(api: GridApi<Row>, move: (keys: number[]) => void): void {
const keys = api.getSelectedKeys() as number[];
if (keys.length === 0) {
say('Select the units to move first');
return;
}
try {
move(keys);
} catch (error) {
say((error as Error).message);
}
}
export const actions = {
underMobile: (api: GridApi<Row>) => run(api, (k) => api.moveRows(k, { childOf: 5 })),
firstUnderDesign: (api: GridApi<Row>) =>
run(api, (k) => api.moveRows(k, { childOf: 6, index: 0 })),
beforeDesign: (api: GridApi<Row>) => run(api, (k) => api.moveRows(k, { before: 6 })),
toTop: (api: GridApi<Row>) => run(api, (k) => api.moveRows(k, { index: 0 })),
intoSelf: (api: GridApi<Row>) => run(api, (k) => api.moveRows(k, { childOf: 3 })),
undo: (api: GridApi<Row>) => {
api.undo();
},
};