번호 간격이 모자라면 전체를 다시 매겨요Renumber when the gaps run out
이웃한 번호 차이가 2보다 작아 끼울 자리가 없으면 같은 부모끼리 전체를 10, 20, 30 …으로 다시 매겨요. getOrderGaps로 간격을 보고 renumberOrder로 직접 다시 매길 수도 있어요.When neighboring numbers differ by less than 2 there is no room, so the whole list is renumbered 10, 20, 30 and so on. getOrderGaps shows the gaps and renumberOrder renumbers on demand.
코드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="showGaps">간격 보기</button>
<button type="button" id="moveIntoTight">빽빽한 곳에 끼우기</button>
<button type="button" id="renumber">다시 매기기</button>
<select aria-label="다시 매기는 범위" id="mode">
<option value="moved" selected>옮긴 행만 (기본)</option>
<option value="all">모두</option>
</select>
</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('showGaps').addEventListener('click', () => actions.showGaps(api));
document.getElementById('moveIntoTight').addEventListener('click', () => actions.moveIntoTight(api));
document.getElementById('renumber').addEventListener('click', () => actions.renumber(api));
document.getElementById('mode').addEventListener('change', (event) => actions.mode(api, event.target.value));const tasks = ['기획', '설계', '개발', '리뷰', '테스트', '수정', '배포', '정리'];
const seqs = [10, 11, 12, 13, 14, 15, 40, 80];
// 앞쪽 여섯 행은 번호가 붙어 있어서(10~15) 사이에 끼울 자리가 없어요.
export const options = {
columns: [
{ id: 'seq', title: '순서 번호', width: 90, align: 'end' },
{ id: 'task', title: '할 일', width: 180 },
],
rows: tasks.map((task, i) => ({ id: i + 1, seq: seqs[i], task })),
rowKey: 'id',
rowDrag: true,
orderIndex: { field: 'seq', step: 10 },
};
let say = () => { };
export function setup(api, log) {
say = log;
log({
ko: '번호 사이가 비좁은 곳으로 행을 옮기면 전체를 다시 매겨요.',
en: 'Moving a row into a tight spot renumbers the whole list.',
});
const off = api.on('orderIndexChanged', (e) => log({
ko: `번호 바뀜: ${e.rowKeys.length}개 행, 전체 다시 매김 ${e.renumbered}`,
en: `Order changed: ${e.rowKeys.length} rows, renumbered ${e.renumbered}`,
}));
return () => off();
}
export const actions = {
showGaps: (api) => {
const g = api.getOrderGaps();
say({
ko: `최소 ${g.min}, 최대 ${g.max}, 가장 좁은 간격 ${g.minGap}, 다시 매기기 필요 ${g.needsRenumber}`,
en: `min ${g.min}, max ${g.max}, smallest gap ${g.minGap}, needs renumber ${g.needsRenumber}`,
});
},
moveIntoTight: (api) => {
const all = api.getRows();
const last = all[all.length - 1];
api.moveRows([last.id], { after: all[1].id });
},
renumber: (api) => {
const n = api.renumberOrder();
say({ ko: `${n}개 행의 번호를 다시 매겼어요`, en: `Renumbered ${n} rows` });
},
mode: (api, value) => api.setOptions({
orderIndex: { field: 'seq', step: 10, renumber: value === 'all' ? 'all' : 'moved' },
}),
};<!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="showGaps">간격 보기</button>
<button type="button" id="moveIntoTight">빽빽한 곳에 끼우기</button>
<button type="button" id="renumber">다시 매기기</button>
<select aria-label="다시 매기는 범위" id="mode">
<option value="moved" selected>옮긴 행만 (기본)</option>
<option value="all">모두</option>
</select>
</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 tasks = ['기획', '설계', '개발', '리뷰', '테스트', '수정', '배포', '정리'];
const seqs = [10, 11, 12, 13, 14, 15, 40, 80];
// 앞쪽 여섯 행은 번호가 붙어 있어서(10~15) 사이에 끼울 자리가 없어요.
const options = {
columns: [
{ id: 'seq', title: '순서 번호', width: 90, align: 'end' },
{ id: 'task', title: '할 일', width: 180 },
],
rows: tasks.map((task, i) => ({ id: i + 1, seq: seqs[i], task })),
rowKey: 'id',
rowDrag: true,
orderIndex: { field: 'seq', step: 10 },
};
let say = () => { };
function setup(api, log) {
say = log;
log({
ko: '번호 사이가 비좁은 곳으로 행을 옮기면 전체를 다시 매겨요.',
en: 'Moving a row into a tight spot renumbers the whole list.',
});
const off = api.on('orderIndexChanged', (e) => log({
ko: `번호 바뀜: ${e.rowKeys.length}개 행, 전체 다시 매김 ${e.renumbered}`,
en: `Order changed: ${e.rowKeys.length} rows, renumbered ${e.renumbered}`,
}));
return () => off();
}
const actions = {
showGaps: (api) => {
const g = api.getOrderGaps();
say({
ko: `최소 ${g.min}, 최대 ${g.max}, 가장 좁은 간격 ${g.minGap}, 다시 매기기 필요 ${g.needsRenumber}`,
en: `min ${g.min}, max ${g.max}, smallest gap ${g.minGap}, needs renumber ${g.needsRenumber}`,
});
},
moveIntoTight: (api) => {
const all = api.getRows();
const last = all[all.length - 1];
api.moveRows([last.id], { after: all[1].id });
},
renumber: (api) => {
const n = api.renumberOrder();
say({ ko: `${n}개 행의 번호를 다시 매겼어요`, en: `Renumbered ${n} rows` });
},
mode: (api, value) => api.setOptions({
orderIndex: { field: 'seq', step: 10, renumber: value === 'all' ? 'all' : 'moved' },
}),
};
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('showGaps').addEventListener('click', () => actions.showGaps(api));
document.getElementById('moveIntoTight').addEventListener('click', () => actions.moveIntoTight(api));
document.getElementById('renumber').addEventListener('click', () => actions.renumber(api));
document.getElementById('mode').addEventListener('change', (event) => actions.mode(api, event.target.value));
</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.showGaps(api)}
>간격 보기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.moveIntoTight(api)}
>빽빽한 곳에 끼우기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.renumber(api)}
>다시 매기기</button>
<select
aria-label="다시 매기는 범위"
disabled={!api}
onChange={(event) => api && actions.mode(api, event.target.value)}
>
<option value="moved">옮긴 행만 (기본)</option>
<option value="all">모두</option>
</select>
</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;
seq: number;
task: string;
}
const tasks = ['기획', '설계', '개발', '리뷰', '테스트', '수정', '배포', '정리'];
const seqs = [10, 11, 12, 13, 14, 15, 40, 80];
// 앞쪽 여섯 행은 번호가 붙어 있어서(10~15) 사이에 끼울 자리가 없어요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'seq', title: '순서 번호', width: 90, align: 'end' },
{ id: 'task', title: '할 일', width: 180 },
],
rows: tasks.map((task, i) => ({ id: i + 1, seq: seqs[i] as number, task })),
rowKey: 'id',
rowDrag: true,
orderIndex: { field: 'seq', step: 10 },
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
log({
ko: '번호 사이가 비좁은 곳으로 행을 옮기면 전체를 다시 매겨요.',
en: 'Moving a row into a tight spot renumbers the whole list.',
});
const off = api.on('orderIndexChanged', (e) =>
log({
ko: `번호 바뀜: ${e.rowKeys.length}개 행, 전체 다시 매김 ${e.renumbered}`,
en: `Order changed: ${e.rowKeys.length} rows, renumbered ${e.renumbered}`,
}),
);
return () => off();
}
export const actions = {
showGaps: (api: GridApi<Row>) => {
const g = api.getOrderGaps();
say({
ko: `최소 ${g.min}, 최대 ${g.max}, 가장 좁은 간격 ${g.minGap}, 다시 매기기 필요 ${g.needsRenumber}`,
en: `min ${g.min}, max ${g.max}, smallest gap ${g.minGap}, needs renumber ${g.needsRenumber}`,
});
},
moveIntoTight: (api: GridApi<Row>) => {
const all = api.getRows();
const last = all[all.length - 1] as Row;
api.moveRows([last.id], { after: (all[1] as Row).id });
},
renumber: (api: GridApi<Row>) => {
const n = api.renumberOrder();
say({ ko: `${n}개 행의 번호를 다시 매겼어요`, en: `Renumbered ${n} rows` });
},
mode: (api: GridApi<Row>, value?: string) =>
api.setOptions({
orderIndex: { field: 'seq', step: 10, renumber: value === 'all' ? 'all' : 'moved' },
}),
};버튼을 누르면 이 예제의 코드가 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.showGaps(api)"
>간격 보기</button>
<button
type="button"
:disabled="!api"
@click="api && actions.moveIntoTight(api)"
>빽빽한 곳에 끼우기</button>
<button
type="button"
:disabled="!api"
@click="api && actions.renumber(api)"
>다시 매기기</button>
<select
aria-label="다시 매기는 범위"
:disabled="!api"
@change="api && actions.mode(api, ($event.target as HTMLSelectElement).value)"
>
<option value="moved" selected>옮긴 행만 (기본)</option>
<option value="all">모두</option>
</select>
</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;
seq: number;
task: string;
}
const tasks = ['기획', '설계', '개발', '리뷰', '테스트', '수정', '배포', '정리'];
const seqs = [10, 11, 12, 13, 14, 15, 40, 80];
// 앞쪽 여섯 행은 번호가 붙어 있어서(10~15) 사이에 끼울 자리가 없어요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'seq', title: '순서 번호', width: 90, align: 'end' },
{ id: 'task', title: '할 일', width: 180 },
],
rows: tasks.map((task, i) => ({ id: i + 1, seq: seqs[i] as number, task })),
rowKey: 'id',
rowDrag: true,
orderIndex: { field: 'seq', step: 10 },
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
log({
ko: '번호 사이가 비좁은 곳으로 행을 옮기면 전체를 다시 매겨요.',
en: 'Moving a row into a tight spot renumbers the whole list.',
});
const off = api.on('orderIndexChanged', (e) =>
log({
ko: `번호 바뀜: ${e.rowKeys.length}개 행, 전체 다시 매김 ${e.renumbered}`,
en: `Order changed: ${e.rowKeys.length} rows, renumbered ${e.renumbered}`,
}),
);
return () => off();
}
export const actions = {
showGaps: (api: GridApi<Row>) => {
const g = api.getOrderGaps();
say({
ko: `최소 ${g.min}, 최대 ${g.max}, 가장 좁은 간격 ${g.minGap}, 다시 매기기 필요 ${g.needsRenumber}`,
en: `min ${g.min}, max ${g.max}, smallest gap ${g.minGap}, needs renumber ${g.needsRenumber}`,
});
},
moveIntoTight: (api: GridApi<Row>) => {
const all = api.getRows();
const last = all[all.length - 1] as Row;
api.moveRows([last.id], { after: (all[1] as Row).id });
},
renumber: (api: GridApi<Row>) => {
const n = api.renumberOrder();
say({ ko: `${n}개 행의 번호를 다시 매겼어요`, en: `Renumbered ${n} rows` });
},
mode: (api: GridApi<Row>, value?: string) =>
api.setOptions({
orderIndex: { field: 'seq', step: 10, renumber: value === 'all' ? 'all' : 'moved' },
}),
};버튼을 누르면 이 예제의 코드가 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.showGaps(api)"
>간격 보기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.moveIntoTight(api)"
>빽빽한 곳에 끼우기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.renumber(api)"
>다시 매기기</button>
<select
aria-label="다시 매기는 범위"
[disabled]="!api"
(change)="api && actions.mode(api, $any($event.target).value)"
>
<option value="moved" selected>옮긴 행만 (기본)</option>
<option value="all">모두</option>
</select>
</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;
seq: number;
task: string;
}
const tasks = ['기획', '설계', '개발', '리뷰', '테스트', '수정', '배포', '정리'];
const seqs = [10, 11, 12, 13, 14, 15, 40, 80];
// 앞쪽 여섯 행은 번호가 붙어 있어서(10~15) 사이에 끼울 자리가 없어요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'seq', title: '순서 번호', width: 90, align: 'end' },
{ id: 'task', title: '할 일', width: 180 },
],
rows: tasks.map((task, i) => ({ id: i + 1, seq: seqs[i] as number, task })),
rowKey: 'id',
rowDrag: true,
orderIndex: { field: 'seq', step: 10 },
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
log({
ko: '번호 사이가 비좁은 곳으로 행을 옮기면 전체를 다시 매겨요.',
en: 'Moving a row into a tight spot renumbers the whole list.',
});
const off = api.on('orderIndexChanged', (e) =>
log({
ko: `번호 바뀜: ${e.rowKeys.length}개 행, 전체 다시 매김 ${e.renumbered}`,
en: `Order changed: ${e.rowKeys.length} rows, renumbered ${e.renumbered}`,
}),
);
return () => off();
}
export const actions = {
showGaps: (api: GridApi<Row>) => {
const g = api.getOrderGaps();
say({
ko: `최소 ${g.min}, 최대 ${g.max}, 가장 좁은 간격 ${g.minGap}, 다시 매기기 필요 ${g.needsRenumber}`,
en: `min ${g.min}, max ${g.max}, smallest gap ${g.minGap}, needs renumber ${g.needsRenumber}`,
});
},
moveIntoTight: (api: GridApi<Row>) => {
const all = api.getRows();
const last = all[all.length - 1] as Row;
api.moveRows([last.id], { after: (all[1] as Row).id });
},
renumber: (api: GridApi<Row>) => {
const n = api.renumberOrder();
say({ ko: `${n}개 행의 번호를 다시 매겼어요`, en: `Renumbered ${n} rows` });
},
mode: (api: GridApi<Row>, value?: string) =>
api.setOptions({
orderIndex: { field: 'seq', step: 10, renumber: value === 'all' ? 'all' : 'moved' },
}),
};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="showGaps">Show gaps</button>
<button type="button" id="moveIntoTight">Move into tight spot</button>
<button type="button" id="renumber">Renumber</button>
<select aria-label="Renumber scope" id="mode">
<option value="moved" selected>Moved rows (default)</option>
<option value="all">All rows</option>
</select>
</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('showGaps').addEventListener('click', () => actions.showGaps(api));
document.getElementById('moveIntoTight').addEventListener('click', () => actions.moveIntoTight(api));
document.getElementById('renumber').addEventListener('click', () => actions.renumber(api));
document.getElementById('mode').addEventListener('change', (event) => actions.mode(api, event.target.value));const tasks = ['Plan', 'Design', 'Build', 'Review', 'Test', 'Fix', 'Release', 'Wrap up'];
const seqs = [10, 11, 12, 13, 14, 15, 40, 80];
// The first six rows are numbered 10 to 15, so there is no room between them.
export const options = {
columns: [
{ id: 'seq', title: 'Order', width: 80, align: 'end' },
{ id: 'task', title: 'Task', width: 180 },
],
rows: tasks.map((task, i) => ({ id: i + 1, seq: seqs[i], task })),
rowKey: 'id',
rowDrag: true,
orderIndex: { field: 'seq', step: 10 },
};
let say = () => { };
export function setup(api, log) {
say = log;
log('Moving a row into a tight spot renumbers the whole list.');
const off = api.on('orderIndexChanged', (e) => log(`Order changed: ${e.rowKeys.length} rows, renumbered ${e.renumbered}`));
return () => off();
}
export const actions = {
showGaps: (api) => {
const g = api.getOrderGaps();
say(`min ${g.min}, max ${g.max}, smallest gap ${g.minGap}, needs renumber ${g.needsRenumber}`);
},
moveIntoTight: (api) => {
const all = api.getRows();
const last = all[all.length - 1];
api.moveRows([last.id], { after: all[1].id });
},
renumber: (api) => {
const n = api.renumberOrder();
say(`Renumbered ${n} rows`);
},
mode: (api, value) => api.setOptions({
orderIndex: { field: 'seq', step: 10, renumber: value === 'all' ? 'all' : 'moved' },
}),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Renumber when the gaps run out</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="showGaps">Show gaps</button>
<button type="button" id="moveIntoTight">Move into tight spot</button>
<button type="button" id="renumber">Renumber</button>
<select aria-label="Renumber scope" id="mode">
<option value="moved" selected>Moved rows (default)</option>
<option value="all">All rows</option>
</select>
</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 tasks = ['Plan', 'Design', 'Build', 'Review', 'Test', 'Fix', 'Release', 'Wrap up'];
const seqs = [10, 11, 12, 13, 14, 15, 40, 80];
// The first six rows are numbered 10 to 15, so there is no room between them.
const options = {
columns: [
{ id: 'seq', title: 'Order', width: 80, align: 'end' },
{ id: 'task', title: 'Task', width: 180 },
],
rows: tasks.map((task, i) => ({ id: i + 1, seq: seqs[i], task })),
rowKey: 'id',
rowDrag: true,
orderIndex: { field: 'seq', step: 10 },
};
let say = () => { };
function setup(api, log) {
say = log;
log('Moving a row into a tight spot renumbers the whole list.');
const off = api.on('orderIndexChanged', (e) => log(`Order changed: ${e.rowKeys.length} rows, renumbered ${e.renumbered}`));
return () => off();
}
const actions = {
showGaps: (api) => {
const g = api.getOrderGaps();
say(`min ${g.min}, max ${g.max}, smallest gap ${g.minGap}, needs renumber ${g.needsRenumber}`);
},
moveIntoTight: (api) => {
const all = api.getRows();
const last = all[all.length - 1];
api.moveRows([last.id], { after: all[1].id });
},
renumber: (api) => {
const n = api.renumberOrder();
say(`Renumbered ${n} rows`);
},
mode: (api, value) => api.setOptions({
orderIndex: { field: 'seq', step: 10, renumber: value === 'all' ? 'all' : 'moved' },
}),
};
options.ariaLabel = "Renumber when the gaps run out";
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('showGaps').addEventListener('click', () => actions.showGaps(api));
document.getElementById('moveIntoTight').addEventListener('click', () => actions.moveIntoTight(api));
document.getElementById('renumber').addEventListener('click', () => actions.renumber(api));
document.getElementById('mode').addEventListener('change', (event) => actions.mode(api, event.target.value));
</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.showGaps(api)}
>Show gaps</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.moveIntoTight(api)}
>Move into tight spot</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.renumber(api)}
>Renumber</button>
<select
aria-label="Renumber scope"
disabled={!api}
onChange={(event) => api && actions.mode(api, event.target.value)}
>
<option value="moved">Moved rows (default)</option>
<option value="all">All rows</option>
</select>
</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;
seq: number;
task: string;
}
const tasks = ['Plan', 'Design', 'Build', 'Review', 'Test', 'Fix', 'Release', 'Wrap up'];
const seqs = [10, 11, 12, 13, 14, 15, 40, 80];
// The first six rows are numbered 10 to 15, so there is no room between them.
export const options: GridOptions<Row> = {
columns: [
{ id: 'seq', title: 'Order', width: 80, align: 'end' },
{ id: 'task', title: 'Task', width: 180 },
],
rows: tasks.map((task, i) => ({ id: i + 1, seq: seqs[i] as number, task })),
rowKey: 'id',
rowDrag: true,
orderIndex: { field: 'seq', step: 10 },
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
log('Moving a row into a tight spot renumbers the whole list.');
const off = api.on('orderIndexChanged', (e) =>
log(`Order changed: ${e.rowKeys.length} rows, renumbered ${e.renumbered}`),
);
return () => off();
}
export const actions = {
showGaps: (api: GridApi<Row>) => {
const g = api.getOrderGaps();
say(`min ${g.min}, max ${g.max}, smallest gap ${g.minGap}, needs renumber ${g.needsRenumber}`);
},
moveIntoTight: (api: GridApi<Row>) => {
const all = api.getRows();
const last = all[all.length - 1] as Row;
api.moveRows([last.id], { after: (all[1] as Row).id });
},
renumber: (api: GridApi<Row>) => {
const n = api.renumberOrder();
say(`Renumbered ${n} rows`);
},
mode: (api: GridApi<Row>, value?: string) =>
api.setOptions({
orderIndex: { field: 'seq', step: 10, renumber: value === 'all' ? 'all' : 'moved' },
}),
};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.showGaps(api)"
>Show gaps</button>
<button
type="button"
:disabled="!api"
@click="api && actions.moveIntoTight(api)"
>Move into tight spot</button>
<button
type="button"
:disabled="!api"
@click="api && actions.renumber(api)"
>Renumber</button>
<select
aria-label="Renumber scope"
:disabled="!api"
@change="api && actions.mode(api, ($event.target as HTMLSelectElement).value)"
>
<option value="moved" selected>Moved rows (default)</option>
<option value="all">All rows</option>
</select>
</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;
seq: number;
task: string;
}
const tasks = ['Plan', 'Design', 'Build', 'Review', 'Test', 'Fix', 'Release', 'Wrap up'];
const seqs = [10, 11, 12, 13, 14, 15, 40, 80];
// The first six rows are numbered 10 to 15, so there is no room between them.
export const options: GridOptions<Row> = {
columns: [
{ id: 'seq', title: 'Order', width: 80, align: 'end' },
{ id: 'task', title: 'Task', width: 180 },
],
rows: tasks.map((task, i) => ({ id: i + 1, seq: seqs[i] as number, task })),
rowKey: 'id',
rowDrag: true,
orderIndex: { field: 'seq', step: 10 },
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
log('Moving a row into a tight spot renumbers the whole list.');
const off = api.on('orderIndexChanged', (e) =>
log(`Order changed: ${e.rowKeys.length} rows, renumbered ${e.renumbered}`),
);
return () => off();
}
export const actions = {
showGaps: (api: GridApi<Row>) => {
const g = api.getOrderGaps();
say(`min ${g.min}, max ${g.max}, smallest gap ${g.minGap}, needs renumber ${g.needsRenumber}`);
},
moveIntoTight: (api: GridApi<Row>) => {
const all = api.getRows();
const last = all[all.length - 1] as Row;
api.moveRows([last.id], { after: (all[1] as Row).id });
},
renumber: (api: GridApi<Row>) => {
const n = api.renumberOrder();
say(`Renumbered ${n} rows`);
},
mode: (api: GridApi<Row>, value?: string) =>
api.setOptions({
orderIndex: { field: 'seq', step: 10, renumber: value === 'all' ? 'all' : 'moved' },
}),
};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.showGaps(api)"
>Show gaps</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.moveIntoTight(api)"
>Move into tight spot</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.renumber(api)"
>Renumber</button>
<select
aria-label="Renumber scope"
[disabled]="!api"
(change)="api && actions.mode(api, $any($event.target).value)"
>
<option value="moved" selected>Moved rows (default)</option>
<option value="all">All rows</option>
</select>
</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;
seq: number;
task: string;
}
const tasks = ['Plan', 'Design', 'Build', 'Review', 'Test', 'Fix', 'Release', 'Wrap up'];
const seqs = [10, 11, 12, 13, 14, 15, 40, 80];
// The first six rows are numbered 10 to 15, so there is no room between them.
export const options: GridOptions<Row> = {
columns: [
{ id: 'seq', title: 'Order', width: 80, align: 'end' },
{ id: 'task', title: 'Task', width: 180 },
],
rows: tasks.map((task, i) => ({ id: i + 1, seq: seqs[i] as number, task })),
rowKey: 'id',
rowDrag: true,
orderIndex: { field: 'seq', step: 10 },
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
log('Moving a row into a tight spot renumbers the whole list.');
const off = api.on('orderIndexChanged', (e) =>
log(`Order changed: ${e.rowKeys.length} rows, renumbered ${e.renumbered}`),
);
return () => off();
}
export const actions = {
showGaps: (api: GridApi<Row>) => {
const g = api.getOrderGaps();
say(`min ${g.min}, max ${g.max}, smallest gap ${g.minGap}, needs renumber ${g.needsRenumber}`);
},
moveIntoTight: (api: GridApi<Row>) => {
const all = api.getRows();
const last = all[all.length - 1] as Row;
api.moveRows([last.id], { after: (all[1] as Row).id });
},
renumber: (api: GridApi<Row>) => {
const n = api.renumberOrder();
say(`Renumbered ${n} rows`);
},
mode: (api: GridApi<Row>, value?: string) =>
api.setOptions({
orderIndex: { field: 'seq', step: 10, renumber: value === 'all' ? 'all' : 'moved' },
}),
};