행을 옮기면 순서 번호가 따라와요Order numbers follow moved rows
orderIndex 옵션을 켜면 행을 옮길 때 옮긴 행의 seq 칸에 앞뒤 번호 사이 값이 자동으로 들어가요. 끌어서 옮기거나 버튼으로 옮겨 보세요.With the orderIndex option, a moved row gets a number between its new neighbors in the seq field automatically. Drag a row or use the buttons.
코드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="moveFirstToEnd">첫 행을 맨 끝으로</button>
<button type="button" id="moveLastToTop">마지막 행을 맨 위로</button>
<button type="button" id="undo">되돌리기</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('moveFirstToEnd').addEventListener('click', () => actions.moveFirstToEnd(api));
document.getElementById('moveLastToTop').addEventListener('click', () => actions.moveLastToTop(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));const owners = ['김서연', '박지훈', '이도윤', '최하은'];
const rows = [
'요구사항 정리',
'화면 설계',
'데이터 모델',
'API 구현',
'테스트 작성',
'문서 보완',
'배포 준비',
'회고',
].map((task, i) => ({ id: i + 1, seq: (i + 1) * 10, task, owner: owners[i % 4] }));
// orderIndex를 켜면 행을 옮긴 뒤 옮긴 행의 seq 칸에 앞뒤 번호의 가운데 값을 써요.
export const options = {
columns: [
{ id: 'seq', title: '순서 번호', width: 90, align: 'end' },
{ id: 'task', title: '할 일', width: 170 },
{ id: 'owner', title: '담당', width: 100 },
],
rows,
rowKey: 'id',
rowDrag: true,
orderIndex: { field: 'seq' },
};
export function setup(api, log) {
log({
ko: '손잡이를 끌어 행을 옮기면 순서 번호가 바뀌어요.',
en: 'Drag a handle to move a row and its order number changes.',
});
const off = api.on('orderIndexChanged', (e) => {
const seqs = e.rowKeys.map((k) => `${k}번=${api.getRowByKey(k)?.seq}`).join(', ');
log({
ko: `번호 바뀜: ${seqs} (${e.renumbered ? '전체를 다시 매김' : '옮긴 행만'})`,
en: `Order changed: ${seqs} (${e.renumbered ? 'renumbered all' : 'moved rows only'})`,
});
});
return () => off();
}
export const actions = {
moveFirstToEnd: (api) => {
const all = api.getRows();
api.moveRows([all[0].id], { after: all[all.length - 1].id });
},
moveLastToTop: (api) => {
const all = api.getRows();
api.moveRows([all[all.length - 1].id], { before: all[0].id });
},
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="moveFirstToEnd">첫 행을 맨 끝으로</button>
<button type="button" id="moveLastToTop">마지막 행을 맨 위로</button>
<button type="button" id="undo">되돌리기</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>
const owners = ['김서연', '박지훈', '이도윤', '최하은'];
const rows = [
'요구사항 정리',
'화면 설계',
'데이터 모델',
'API 구현',
'테스트 작성',
'문서 보완',
'배포 준비',
'회고',
].map((task, i) => ({ id: i + 1, seq: (i + 1) * 10, task, owner: owners[i % 4] }));
// orderIndex를 켜면 행을 옮긴 뒤 옮긴 행의 seq 칸에 앞뒤 번호의 가운데 값을 써요.
const options = {
columns: [
{ id: 'seq', title: '순서 번호', width: 90, align: 'end' },
{ id: 'task', title: '할 일', width: 170 },
{ id: 'owner', title: '담당', width: 100 },
],
rows,
rowKey: 'id',
rowDrag: true,
orderIndex: { field: 'seq' },
};
function setup(api, log) {
log({
ko: '손잡이를 끌어 행을 옮기면 순서 번호가 바뀌어요.',
en: 'Drag a handle to move a row and its order number changes.',
});
const off = api.on('orderIndexChanged', (e) => {
const seqs = e.rowKeys.map((k) => `${k}번=${api.getRowByKey(k)?.seq}`).join(', ');
log({
ko: `번호 바뀜: ${seqs} (${e.renumbered ? '전체를 다시 매김' : '옮긴 행만'})`,
en: `Order changed: ${seqs} (${e.renumbered ? 'renumbered all' : 'moved rows only'})`,
});
});
return () => off();
}
const actions = {
moveFirstToEnd: (api) => {
const all = api.getRows();
api.moveRows([all[0].id], { after: all[all.length - 1].id });
},
moveLastToTop: (api) => {
const all = api.getRows();
api.moveRows([all[all.length - 1].id], { before: all[0].id });
},
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('moveFirstToEnd').addEventListener('click', () => actions.moveFirstToEnd(api));
document.getElementById('moveLastToTop').addEventListener('click', () => actions.moveLastToTop(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.moveFirstToEnd(api)}
>첫 행을 맨 끝으로</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.moveLastToTop(api)}
>마지막 행을 맨 위로</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.undo(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;
seq: number;
task: string;
owner: string;
}
const owners = ['김서연', '박지훈', '이도윤', '최하은'];
const rows: Row[] = [
'요구사항 정리',
'화면 설계',
'데이터 모델',
'API 구현',
'테스트 작성',
'문서 보완',
'배포 준비',
'회고',
].map((task, i) => ({ id: i + 1, seq: (i + 1) * 10, task, owner: owners[i % 4] as string }));
// orderIndex를 켜면 행을 옮긴 뒤 옮긴 행의 seq 칸에 앞뒤 번호의 가운데 값을 써요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'seq', title: '순서 번호', width: 90, align: 'end' },
{ id: 'task', title: '할 일', width: 170 },
{ id: 'owner', title: '담당', width: 100 },
],
rows,
rowKey: 'id',
rowDrag: true,
orderIndex: { field: 'seq' },
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
log({
ko: '손잡이를 끌어 행을 옮기면 순서 번호가 바뀌어요.',
en: 'Drag a handle to move a row and its order number changes.',
});
const off = api.on('orderIndexChanged', (e) => {
const seqs = e.rowKeys.map((k) => `${k}번=${api.getRowByKey(k)?.seq}`).join(', ');
log({
ko: `번호 바뀜: ${seqs} (${e.renumbered ? '전체를 다시 매김' : '옮긴 행만'})`,
en: `Order changed: ${seqs} (${e.renumbered ? 'renumbered all' : 'moved rows only'})`,
});
});
return () => off();
}
export const actions = {
moveFirstToEnd: (api: GridApi<Row>) => {
const all = api.getRows();
api.moveRows([(all[0] as Row).id], { after: (all[all.length - 1] as Row).id });
},
moveLastToTop: (api: GridApi<Row>) => {
const all = api.getRows();
api.moveRows([(all[all.length - 1] as Row).id], { before: (all[0] as Row).id });
},
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.moveFirstToEnd(api)"
>첫 행을 맨 끝으로</button>
<button
type="button"
:disabled="!api"
@click="api && actions.moveLastToTop(api)"
>마지막 행을 맨 위로</button>
<button type="button" :disabled="!api" @click="api && actions.undo(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;
seq: number;
task: string;
owner: string;
}
const owners = ['김서연', '박지훈', '이도윤', '최하은'];
const rows: Row[] = [
'요구사항 정리',
'화면 설계',
'데이터 모델',
'API 구현',
'테스트 작성',
'문서 보완',
'배포 준비',
'회고',
].map((task, i) => ({ id: i + 1, seq: (i + 1) * 10, task, owner: owners[i % 4] as string }));
// orderIndex를 켜면 행을 옮긴 뒤 옮긴 행의 seq 칸에 앞뒤 번호의 가운데 값을 써요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'seq', title: '순서 번호', width: 90, align: 'end' },
{ id: 'task', title: '할 일', width: 170 },
{ id: 'owner', title: '담당', width: 100 },
],
rows,
rowKey: 'id',
rowDrag: true,
orderIndex: { field: 'seq' },
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
log({
ko: '손잡이를 끌어 행을 옮기면 순서 번호가 바뀌어요.',
en: 'Drag a handle to move a row and its order number changes.',
});
const off = api.on('orderIndexChanged', (e) => {
const seqs = e.rowKeys.map((k) => `${k}번=${api.getRowByKey(k)?.seq}`).join(', ');
log({
ko: `번호 바뀜: ${seqs} (${e.renumbered ? '전체를 다시 매김' : '옮긴 행만'})`,
en: `Order changed: ${seqs} (${e.renumbered ? 'renumbered all' : 'moved rows only'})`,
});
});
return () => off();
}
export const actions = {
moveFirstToEnd: (api: GridApi<Row>) => {
const all = api.getRows();
api.moveRows([(all[0] as Row).id], { after: (all[all.length - 1] as Row).id });
},
moveLastToTop: (api: GridApi<Row>) => {
const all = api.getRows();
api.moveRows([(all[all.length - 1] as Row).id], { before: (all[0] as Row).id });
},
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.moveFirstToEnd(api)"
>첫 행을 맨 끝으로</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.moveLastToTop(api)"
>마지막 행을 맨 위로</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.undo(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;
seq: number;
task: string;
owner: string;
}
const owners = ['김서연', '박지훈', '이도윤', '최하은'];
const rows: Row[] = [
'요구사항 정리',
'화면 설계',
'데이터 모델',
'API 구현',
'테스트 작성',
'문서 보완',
'배포 준비',
'회고',
].map((task, i) => ({ id: i + 1, seq: (i + 1) * 10, task, owner: owners[i % 4] as string }));
// orderIndex를 켜면 행을 옮긴 뒤 옮긴 행의 seq 칸에 앞뒤 번호의 가운데 값을 써요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'seq', title: '순서 번호', width: 90, align: 'end' },
{ id: 'task', title: '할 일', width: 170 },
{ id: 'owner', title: '담당', width: 100 },
],
rows,
rowKey: 'id',
rowDrag: true,
orderIndex: { field: 'seq' },
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
log({
ko: '손잡이를 끌어 행을 옮기면 순서 번호가 바뀌어요.',
en: 'Drag a handle to move a row and its order number changes.',
});
const off = api.on('orderIndexChanged', (e) => {
const seqs = e.rowKeys.map((k) => `${k}번=${api.getRowByKey(k)?.seq}`).join(', ');
log({
ko: `번호 바뀜: ${seqs} (${e.renumbered ? '전체를 다시 매김' : '옮긴 행만'})`,
en: `Order changed: ${seqs} (${e.renumbered ? 'renumbered all' : 'moved rows only'})`,
});
});
return () => off();
}
export const actions = {
moveFirstToEnd: (api: GridApi<Row>) => {
const all = api.getRows();
api.moveRows([(all[0] as Row).id], { after: (all[all.length - 1] as Row).id });
},
moveLastToTop: (api: GridApi<Row>) => {
const all = api.getRows();
api.moveRows([(all[all.length - 1] as Row).id], { before: (all[0] as Row).id });
},
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="moveFirstToEnd">First row to the end</button>
<button type="button" id="moveLastToTop">Last row to the top</button>
<button type="button" id="undo">Undo</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('moveFirstToEnd').addEventListener('click', () => actions.moveFirstToEnd(api));
document.getElementById('moveLastToTop').addEventListener('click', () => actions.moveLastToTop(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));const owners = ['Liam Carter', 'Emma Stone', 'Noah Reed', 'Olivia Hayes'];
const rows = [
'Gather requirements',
'Design screens',
'Data model',
'Build API',
'Write tests',
'Polish docs',
'Prepare release',
'Retrospective',
].map((task, i) => ({ id: i + 1, seq: (i + 1) * 10, task, owner: owners[i % 4] }));
// With orderIndex on, a moved row gets a value between its new neighbors in the seq field.
export const options = {
columns: [
{ id: 'seq', title: 'Order', width: 80, align: 'end' },
{ id: 'task', title: 'Task', width: 170 },
{ id: 'owner', title: 'Owner', width: 120 },
],
rows,
rowKey: 'id',
rowDrag: true,
orderIndex: { field: 'seq' },
};
export function setup(api, log) {
log('Drag a handle to move a row and its order number changes.');
const off = api.on('orderIndexChanged', (e) => {
const seqs = e.rowKeys.map((k) => `row ${k}=${api.getRowByKey(k)?.seq}`).join(', ');
log(`Order changed: ${seqs} (${e.renumbered ? 'renumbered all' : 'moved rows only'})`);
});
return () => off();
}
export const actions = {
moveFirstToEnd: (api) => {
const all = api.getRows();
api.moveRows([all[0].id], { after: all[all.length - 1].id });
},
moveLastToTop: (api) => {
const all = api.getRows();
api.moveRows([all[all.length - 1].id], { before: all[0].id });
},
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>Order numbers follow moved rows</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="moveFirstToEnd">First row to the end</button>
<button type="button" id="moveLastToTop">Last row to the top</button>
<button type="button" id="undo">Undo</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>
const owners = ['Liam Carter', 'Emma Stone', 'Noah Reed', 'Olivia Hayes'];
const rows = [
'Gather requirements',
'Design screens',
'Data model',
'Build API',
'Write tests',
'Polish docs',
'Prepare release',
'Retrospective',
].map((task, i) => ({ id: i + 1, seq: (i + 1) * 10, task, owner: owners[i % 4] }));
// With orderIndex on, a moved row gets a value between its new neighbors in the seq field.
const options = {
columns: [
{ id: 'seq', title: 'Order', width: 80, align: 'end' },
{ id: 'task', title: 'Task', width: 170 },
{ id: 'owner', title: 'Owner', width: 120 },
],
rows,
rowKey: 'id',
rowDrag: true,
orderIndex: { field: 'seq' },
};
function setup(api, log) {
log('Drag a handle to move a row and its order number changes.');
const off = api.on('orderIndexChanged', (e) => {
const seqs = e.rowKeys.map((k) => `row ${k}=${api.getRowByKey(k)?.seq}`).join(', ');
log(`Order changed: ${seqs} (${e.renumbered ? 'renumbered all' : 'moved rows only'})`);
});
return () => off();
}
const actions = {
moveFirstToEnd: (api) => {
const all = api.getRows();
api.moveRows([all[0].id], { after: all[all.length - 1].id });
},
moveLastToTop: (api) => {
const all = api.getRows();
api.moveRows([all[all.length - 1].id], { before: all[0].id });
},
undo: (api) => {
api.undo();
},
};
options.ariaLabel = "Order numbers follow moved rows";
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('moveFirstToEnd').addEventListener('click', () => actions.moveFirstToEnd(api));
document.getElementById('moveLastToTop').addEventListener('click', () => actions.moveLastToTop(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.moveFirstToEnd(api)}
>First row to the end</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.moveLastToTop(api)}
>Last row to the top</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.undo(api)}
>Undo</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;
seq: number;
task: string;
owner: string;
}
const owners = ['Liam Carter', 'Emma Stone', 'Noah Reed', 'Olivia Hayes'];
const rows: Row[] = [
'Gather requirements',
'Design screens',
'Data model',
'Build API',
'Write tests',
'Polish docs',
'Prepare release',
'Retrospective',
].map((task, i) => ({ id: i + 1, seq: (i + 1) * 10, task, owner: owners[i % 4] as string }));
// With orderIndex on, a moved row gets a value between its new neighbors in the seq field.
export const options: GridOptions<Row> = {
columns: [
{ id: 'seq', title: 'Order', width: 80, align: 'end' },
{ id: 'task', title: 'Task', width: 170 },
{ id: 'owner', title: 'Owner', width: 120 },
],
rows,
rowKey: 'id',
rowDrag: true,
orderIndex: { field: 'seq' },
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
log('Drag a handle to move a row and its order number changes.');
const off = api.on('orderIndexChanged', (e) => {
const seqs = e.rowKeys.map((k) => `row ${k}=${api.getRowByKey(k)?.seq}`).join(', ');
log(`Order changed: ${seqs} (${e.renumbered ? 'renumbered all' : 'moved rows only'})`);
});
return () => off();
}
export const actions = {
moveFirstToEnd: (api: GridApi<Row>) => {
const all = api.getRows();
api.moveRows([(all[0] as Row).id], { after: (all[all.length - 1] as Row).id });
},
moveLastToTop: (api: GridApi<Row>) => {
const all = api.getRows();
api.moveRows([(all[all.length - 1] as Row).id], { before: (all[0] as Row).id });
},
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.moveFirstToEnd(api)"
>First row to the end</button>
<button
type="button"
:disabled="!api"
@click="api && actions.moveLastToTop(api)"
>Last row to the top</button>
<button type="button" :disabled="!api" @click="api && actions.undo(api)">Undo</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;
seq: number;
task: string;
owner: string;
}
const owners = ['Liam Carter', 'Emma Stone', 'Noah Reed', 'Olivia Hayes'];
const rows: Row[] = [
'Gather requirements',
'Design screens',
'Data model',
'Build API',
'Write tests',
'Polish docs',
'Prepare release',
'Retrospective',
].map((task, i) => ({ id: i + 1, seq: (i + 1) * 10, task, owner: owners[i % 4] as string }));
// With orderIndex on, a moved row gets a value between its new neighbors in the seq field.
export const options: GridOptions<Row> = {
columns: [
{ id: 'seq', title: 'Order', width: 80, align: 'end' },
{ id: 'task', title: 'Task', width: 170 },
{ id: 'owner', title: 'Owner', width: 120 },
],
rows,
rowKey: 'id',
rowDrag: true,
orderIndex: { field: 'seq' },
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
log('Drag a handle to move a row and its order number changes.');
const off = api.on('orderIndexChanged', (e) => {
const seqs = e.rowKeys.map((k) => `row ${k}=${api.getRowByKey(k)?.seq}`).join(', ');
log(`Order changed: ${seqs} (${e.renumbered ? 'renumbered all' : 'moved rows only'})`);
});
return () => off();
}
export const actions = {
moveFirstToEnd: (api: GridApi<Row>) => {
const all = api.getRows();
api.moveRows([(all[0] as Row).id], { after: (all[all.length - 1] as Row).id });
},
moveLastToTop: (api: GridApi<Row>) => {
const all = api.getRows();
api.moveRows([(all[all.length - 1] as Row).id], { before: (all[0] as Row).id });
},
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.moveFirstToEnd(api)"
>First row to the end</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.moveLastToTop(api)"
>Last row to the top</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.undo(api)"
>Undo</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;
seq: number;
task: string;
owner: string;
}
const owners = ['Liam Carter', 'Emma Stone', 'Noah Reed', 'Olivia Hayes'];
const rows: Row[] = [
'Gather requirements',
'Design screens',
'Data model',
'Build API',
'Write tests',
'Polish docs',
'Prepare release',
'Retrospective',
].map((task, i) => ({ id: i + 1, seq: (i + 1) * 10, task, owner: owners[i % 4] as string }));
// With orderIndex on, a moved row gets a value between its new neighbors in the seq field.
export const options: GridOptions<Row> = {
columns: [
{ id: 'seq', title: 'Order', width: 80, align: 'end' },
{ id: 'task', title: 'Task', width: 170 },
{ id: 'owner', title: 'Owner', width: 120 },
],
rows,
rowKey: 'id',
rowDrag: true,
orderIndex: { field: 'seq' },
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
log('Drag a handle to move a row and its order number changes.');
const off = api.on('orderIndexChanged', (e) => {
const seqs = e.rowKeys.map((k) => `row ${k}=${api.getRowByKey(k)?.seq}`).join(', ');
log(`Order changed: ${seqs} (${e.renumbered ? 'renumbered all' : 'moved rows only'})`);
});
return () => off();
}
export const actions = {
moveFirstToEnd: (api: GridApi<Row>) => {
const all = api.getRows();
api.moveRows([(all[0] as Row).id], { after: (all[all.length - 1] as Row).id });
},
moveLastToTop: (api: GridApi<Row>) => {
const all = api.getRows();
api.moveRows([(all[all.length - 1] as Row).id], { before: (all[0] as Row).id });
},
undo: (api: GridApi<Row>) => {
api.undo();
},
};