정렬 중에 줄이 뛰지 않게 행 추가·수정하기Add and edit rows without the sorted rows jumping
addRows와 updateRows에 resort: false를 주면 정렬된 상태에서도 새 행은 맨 뒤에, 고친 행은 지금 자리에 머물러요. 정렬이나 필터를 바꾸면 평소 순서로 돌아가요.With resort: false, addRows and updateRows keep a new row at the end and an edited row at its current place even while a sort is active. Changing the sort or filter returns to the normal order.
코드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="addSorted">행 추가 (정렬)</button>
<button type="button" id="addStay">행 추가 (맨 뒤)</button>
<button type="button" id="bumpSorted">하늘 99점 (정렬)</button>
<button type="button" id="bumpStay">하늘 99점 (제자리)</button>
<button type="button" id="resort">정렬 다시 적용</button>
</div>
<div id="grid" style="height: 250px"></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('addSorted').addEventListener('click', () => actions.addSorted(api));
document.getElementById('addStay').addEventListener('click', () => actions.addStay(api));
document.getElementById('bumpSorted').addEventListener('click', () => actions.bumpSorted(api));
document.getElementById('bumpStay').addEventListener('click', () => actions.bumpStay(api));
document.getElementById('resort').addEventListener('click', () => actions.resort(api));const rows = [
{ id: 1, name: '민수', score: 90 },
{ id: 2, name: '서연', score: 70 },
{ id: 3, name: '지호', score: 50 },
{ id: 4, name: '하늘', score: 30 },
];
let next = 5;
// 정렬 중에 행을 더하거나 고치면 줄이 바로 뛰어요. resort: false를 주면 다음 정렬·필터 변경 전까지
// 추가한 행은 맨 뒤에, 고친 행은 지금 자리에 머물러요.
export const options = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'name', title: '이름', width: 100 },
{ id: 'score', title: '점수', kind: 'number', width: 100 },
],
rows,
rowKey: 'id',
};
export function setup(api, log) {
next = 5;
api.setSortModel([{ columnId: 'score', direction: 'desc' }]);
const off = api.on('sortChanged', () => log({
ko: '정렬을 바꿔서 임시 자리가 사라졌어요',
en: 'Sort changed: the temporary places are gone',
}));
return () => off();
}
export const actions = {
addSorted: (api) => api.addRows([{ id: next++, name: `새${next}`, score: 80 }]),
addStay: (api) => api.addRows([{ id: next++, name: `새${next}`, score: 80 }], { resort: false }),
bumpSorted: (api) => api.updateRows([{ id: 4, name: '하늘', score: 99 }]),
bumpStay: (api) => api.updateRows([{ id: 4, name: '하늘', score: 99 }], { resort: false }),
resort: (api) => api.setSortModel([
{ columnId: 'score', direction: 'desc' },
{ columnId: 'id', direction: 'asc' },
]),
};<!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="addSorted">행 추가 (정렬)</button>
<button type="button" id="addStay">행 추가 (맨 뒤)</button>
<button type="button" id="bumpSorted">하늘 99점 (정렬)</button>
<button type="button" id="bumpStay">하늘 99점 (제자리)</button>
<button type="button" id="resort">정렬 다시 적용</button>
</div>
<div id="grid" style="height: 250px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const rows = [
{ id: 1, name: '민수', score: 90 },
{ id: 2, name: '서연', score: 70 },
{ id: 3, name: '지호', score: 50 },
{ id: 4, name: '하늘', score: 30 },
];
let next = 5;
// 정렬 중에 행을 더하거나 고치면 줄이 바로 뛰어요. resort: false를 주면 다음 정렬·필터 변경 전까지
// 추가한 행은 맨 뒤에, 고친 행은 지금 자리에 머물러요.
const options = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'name', title: '이름', width: 100 },
{ id: 'score', title: '점수', kind: 'number', width: 100 },
],
rows,
rowKey: 'id',
};
function setup(api, log) {
next = 5;
api.setSortModel([{ columnId: 'score', direction: 'desc' }]);
const off = api.on('sortChanged', () => log({
ko: '정렬을 바꿔서 임시 자리가 사라졌어요',
en: 'Sort changed: the temporary places are gone',
}));
return () => off();
}
const actions = {
addSorted: (api) => api.addRows([{ id: next++, name: `새${next}`, score: 80 }]),
addStay: (api) => api.addRows([{ id: next++, name: `새${next}`, score: 80 }], { resort: false }),
bumpSorted: (api) => api.updateRows([{ id: 4, name: '하늘', score: 99 }]),
bumpStay: (api) => api.updateRows([{ id: 4, name: '하늘', score: 99 }], { resort: false }),
resort: (api) => api.setSortModel([
{ columnId: 'score', direction: 'desc' },
{ columnId: 'id', direction: 'asc' },
]),
};
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('addSorted').addEventListener('click', () => actions.addSorted(api));
document.getElementById('addStay').addEventListener('click', () => actions.addStay(api));
document.getElementById('bumpSorted').addEventListener('click', () => actions.bumpSorted(api));
document.getElementById('bumpStay').addEventListener('click', () => actions.bumpStay(api));
document.getElementById('resort').addEventListener('click', () => actions.resort(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.addSorted(api)}
>행 추가 (정렬)</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.addStay(api)}
>행 추가 (맨 뒤)</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.bumpSorted(api)}
>하늘 99점 (정렬)</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.bumpStay(api)}
>하늘 99점 (제자리)</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.resort(api)}
>정렬 다시 적용</button>
</div>
<Grid style={{ height: 250 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
score: number;
}
const rows: Row[] = [
{ id: 1, name: '민수', score: 90 },
{ id: 2, name: '서연', score: 70 },
{ id: 3, name: '지호', score: 50 },
{ id: 4, name: '하늘', score: 30 },
];
let next = 5;
// 정렬 중에 행을 더하거나 고치면 줄이 바로 뛰어요. resort: false를 주면 다음 정렬·필터 변경 전까지
// 추가한 행은 맨 뒤에, 고친 행은 지금 자리에 머물러요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'name', title: '이름', width: 100 },
{ id: 'score', title: '점수', kind: 'number', width: 100 },
],
rows,
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
next = 5;
api.setSortModel([{ columnId: 'score', direction: 'desc' }]);
const off = api.on('sortChanged', () =>
log({
ko: '정렬을 바꿔서 임시 자리가 사라졌어요',
en: 'Sort changed: the temporary places are gone',
}),
);
return () => off();
}
export const actions = {
addSorted: (api: GridApi<Row>) => api.addRows([{ id: next++, name: `새${next}`, score: 80 }]),
addStay: (api: GridApi<Row>) =>
api.addRows([{ id: next++, name: `새${next}`, score: 80 }], { resort: false }),
bumpSorted: (api: GridApi<Row>) => api.updateRows([{ id: 4, name: '하늘', score: 99 }]),
bumpStay: (api: GridApi<Row>) =>
api.updateRows([{ id: 4, name: '하늘', score: 99 }], { resort: false }),
resort: (api: GridApi<Row>) =>
api.setSortModel([
{ columnId: 'score', direction: 'desc' },
{ columnId: 'id', direction: 'asc' },
]),
};버튼을 누르면 이 예제의 코드가 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.addSorted(api)"
>행 추가 (정렬)</button>
<button type="button" :disabled="!api" @click="api && actions.addStay(api)">행 추가 (맨 뒤)</button>
<button
type="button"
:disabled="!api"
@click="api && actions.bumpSorted(api)"
>하늘 99점 (정렬)</button>
<button
type="button"
:disabled="!api"
@click="api && actions.bumpStay(api)"
>하늘 99점 (제자리)</button>
<button type="button" :disabled="!api" @click="api && actions.resort(api)">정렬 다시 적용</button>
</div>
<Grid style="height: 250px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
score: number;
}
const rows: Row[] = [
{ id: 1, name: '민수', score: 90 },
{ id: 2, name: '서연', score: 70 },
{ id: 3, name: '지호', score: 50 },
{ id: 4, name: '하늘', score: 30 },
];
let next = 5;
// 정렬 중에 행을 더하거나 고치면 줄이 바로 뛰어요. resort: false를 주면 다음 정렬·필터 변경 전까지
// 추가한 행은 맨 뒤에, 고친 행은 지금 자리에 머물러요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'name', title: '이름', width: 100 },
{ id: 'score', title: '점수', kind: 'number', width: 100 },
],
rows,
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
next = 5;
api.setSortModel([{ columnId: 'score', direction: 'desc' }]);
const off = api.on('sortChanged', () =>
log({
ko: '정렬을 바꿔서 임시 자리가 사라졌어요',
en: 'Sort changed: the temporary places are gone',
}),
);
return () => off();
}
export const actions = {
addSorted: (api: GridApi<Row>) => api.addRows([{ id: next++, name: `새${next}`, score: 80 }]),
addStay: (api: GridApi<Row>) =>
api.addRows([{ id: next++, name: `새${next}`, score: 80 }], { resort: false }),
bumpSorted: (api: GridApi<Row>) => api.updateRows([{ id: 4, name: '하늘', score: 99 }]),
bumpStay: (api: GridApi<Row>) =>
api.updateRows([{ id: 4, name: '하늘', score: 99 }], { resort: false }),
resort: (api: GridApi<Row>) =>
api.setSortModel([
{ columnId: 'score', direction: 'desc' },
{ columnId: 'id', direction: 'asc' },
]),
};버튼을 누르면 이 예제의 코드가 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.addSorted(api)"
>행 추가 (정렬)</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.addStay(api)"
>행 추가 (맨 뒤)</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.bumpSorted(api)"
>하늘 99점 (정렬)</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.bumpStay(api)"
>하늘 99점 (제자리)</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.resort(api)"
>정렬 다시 적용</button>
</div>
<mx-grid style="height: 250px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
score: number;
}
const rows: Row[] = [
{ id: 1, name: '민수', score: 90 },
{ id: 2, name: '서연', score: 70 },
{ id: 3, name: '지호', score: 50 },
{ id: 4, name: '하늘', score: 30 },
];
let next = 5;
// 정렬 중에 행을 더하거나 고치면 줄이 바로 뛰어요. resort: false를 주면 다음 정렬·필터 변경 전까지
// 추가한 행은 맨 뒤에, 고친 행은 지금 자리에 머물러요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 70 },
{ id: 'name', title: '이름', width: 100 },
{ id: 'score', title: '점수', kind: 'number', width: 100 },
],
rows,
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
next = 5;
api.setSortModel([{ columnId: 'score', direction: 'desc' }]);
const off = api.on('sortChanged', () =>
log({
ko: '정렬을 바꿔서 임시 자리가 사라졌어요',
en: 'Sort changed: the temporary places are gone',
}),
);
return () => off();
}
export const actions = {
addSorted: (api: GridApi<Row>) => api.addRows([{ id: next++, name: `새${next}`, score: 80 }]),
addStay: (api: GridApi<Row>) =>
api.addRows([{ id: next++, name: `새${next}`, score: 80 }], { resort: false }),
bumpSorted: (api: GridApi<Row>) => api.updateRows([{ id: 4, name: '하늘', score: 99 }]),
bumpStay: (api: GridApi<Row>) =>
api.updateRows([{ id: 4, name: '하늘', score: 99 }], { resort: false }),
resort: (api: GridApi<Row>) =>
api.setSortModel([
{ columnId: 'score', direction: 'desc' },
{ columnId: 'id', direction: 'asc' },
]),
};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="addSorted">Add (sorted)</button>
<button type="button" id="addStay">Add (at the end)</button>
<button type="button" id="bumpSorted">Bump (sorted)</button>
<button type="button" id="bumpStay">Bump (stay)</button>
<button type="button" id="resort">Apply sort again</button>
</div>
<div id="grid" style="height: 250px"></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('addSorted').addEventListener('click', () => actions.addSorted(api));
document.getElementById('addStay').addEventListener('click', () => actions.addStay(api));
document.getElementById('bumpSorted').addEventListener('click', () => actions.bumpSorted(api));
document.getElementById('bumpStay').addEventListener('click', () => actions.bumpStay(api));
document.getElementById('resort').addEventListener('click', () => actions.resort(api));const rows = [
{ id: 1, name: 'Liam', score: 90 },
{ id: 2, name: 'Emma', score: 70 },
{ id: 3, name: 'Noah', score: 50 },
{ id: 4, name: 'Olivia', score: 30 },
];
let next = 5;
// Adding or editing rows while sorted makes lines jump right away. With resort: false, added rows go to the end
// and edited rows stay where they are until the next sort or filter change.
export const options = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'name', title: 'Name', width: 100 },
{ id: 'score', title: 'Score', kind: 'number', width: 100 },
],
rows,
rowKey: 'id',
};
export function setup(api, log) {
next = 5;
api.setSortModel([{ columnId: 'score', direction: 'desc' }]);
const off = api.on('sortChanged', () => log('Sort changed: the temporary places are gone'));
return () => off();
}
export const actions = {
addSorted: (api) => api.addRows([{ id: next++, name: `New${next}`, score: 80 }]),
addStay: (api) => api.addRows([{ id: next++, name: `New${next}`, score: 80 }], { resort: false }),
bumpSorted: (api) => api.updateRows([{ id: 4, name: 'Olivia', score: 99 }]),
bumpStay: (api) => api.updateRows([{ id: 4, name: 'Olivia', score: 99 }], { resort: false }),
resort: (api) => api.setSortModel([
{ columnId: 'score', direction: 'desc' },
{ columnId: 'id', direction: 'asc' },
]),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Add and edit rows without the sorted rows jumping</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="addSorted">Add (sorted)</button>
<button type="button" id="addStay">Add (at the end)</button>
<button type="button" id="bumpSorted">Bump (sorted)</button>
<button type="button" id="bumpStay">Bump (stay)</button>
<button type="button" id="resort">Apply sort again</button>
</div>
<div id="grid" style="height: 250px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const rows = [
{ id: 1, name: 'Liam', score: 90 },
{ id: 2, name: 'Emma', score: 70 },
{ id: 3, name: 'Noah', score: 50 },
{ id: 4, name: 'Olivia', score: 30 },
];
let next = 5;
// Adding or editing rows while sorted makes lines jump right away. With resort: false, added rows go to the end
// and edited rows stay where they are until the next sort or filter change.
const options = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'name', title: 'Name', width: 100 },
{ id: 'score', title: 'Score', kind: 'number', width: 100 },
],
rows,
rowKey: 'id',
};
function setup(api, log) {
next = 5;
api.setSortModel([{ columnId: 'score', direction: 'desc' }]);
const off = api.on('sortChanged', () => log('Sort changed: the temporary places are gone'));
return () => off();
}
const actions = {
addSorted: (api) => api.addRows([{ id: next++, name: `New${next}`, score: 80 }]),
addStay: (api) => api.addRows([{ id: next++, name: `New${next}`, score: 80 }], { resort: false }),
bumpSorted: (api) => api.updateRows([{ id: 4, name: 'Olivia', score: 99 }]),
bumpStay: (api) => api.updateRows([{ id: 4, name: 'Olivia', score: 99 }], { resort: false }),
resort: (api) => api.setSortModel([
{ columnId: 'score', direction: 'desc' },
{ columnId: 'id', direction: 'asc' },
]),
};
options.ariaLabel = "Add and edit rows without the sorted rows jumping";
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('addSorted').addEventListener('click', () => actions.addSorted(api));
document.getElementById('addStay').addEventListener('click', () => actions.addStay(api));
document.getElementById('bumpSorted').addEventListener('click', () => actions.bumpSorted(api));
document.getElementById('bumpStay').addEventListener('click', () => actions.bumpStay(api));
document.getElementById('resort').addEventListener('click', () => actions.resort(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.addSorted(api)}
>Add (sorted)</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.addStay(api)}
>Add (at the end)</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.bumpSorted(api)}
>Bump (sorted)</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.bumpStay(api)}
>Bump (stay)</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.resort(api)}
>Apply sort again</button>
</div>
<Grid style={{ height: 250 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
score: number;
}
const rows: Row[] = [
{ id: 1, name: 'Liam', score: 90 },
{ id: 2, name: 'Emma', score: 70 },
{ id: 3, name: 'Noah', score: 50 },
{ id: 4, name: 'Olivia', score: 30 },
];
let next = 5;
// Adding or editing rows while sorted makes lines jump right away. With resort: false, added rows go to the end
// and edited rows stay where they are until the next sort or filter change.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'name', title: 'Name', width: 100 },
{ id: 'score', title: 'Score', kind: 'number', width: 100 },
],
rows,
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
next = 5;
api.setSortModel([{ columnId: 'score', direction: 'desc' }]);
const off = api.on('sortChanged', () => log('Sort changed: the temporary places are gone'));
return () => off();
}
export const actions = {
addSorted: (api: GridApi<Row>) => api.addRows([{ id: next++, name: `New${next}`, score: 80 }]),
addStay: (api: GridApi<Row>) =>
api.addRows([{ id: next++, name: `New${next}`, score: 80 }], { resort: false }),
bumpSorted: (api: GridApi<Row>) => api.updateRows([{ id: 4, name: 'Olivia', score: 99 }]),
bumpStay: (api: GridApi<Row>) =>
api.updateRows([{ id: 4, name: 'Olivia', score: 99 }], { resort: false }),
resort: (api: GridApi<Row>) =>
api.setSortModel([
{ columnId: 'score', direction: 'desc' },
{ columnId: 'id', direction: 'asc' },
]),
};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.addSorted(api)"
>Add (sorted)</button>
<button type="button" :disabled="!api" @click="api && actions.addStay(api)">Add (at the end)</button>
<button
type="button"
:disabled="!api"
@click="api && actions.bumpSorted(api)"
>Bump (sorted)</button>
<button
type="button"
:disabled="!api"
@click="api && actions.bumpStay(api)"
>Bump (stay)</button>
<button type="button" :disabled="!api" @click="api && actions.resort(api)">Apply sort again</button>
</div>
<Grid style="height: 250px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
score: number;
}
const rows: Row[] = [
{ id: 1, name: 'Liam', score: 90 },
{ id: 2, name: 'Emma', score: 70 },
{ id: 3, name: 'Noah', score: 50 },
{ id: 4, name: 'Olivia', score: 30 },
];
let next = 5;
// Adding or editing rows while sorted makes lines jump right away. With resort: false, added rows go to the end
// and edited rows stay where they are until the next sort or filter change.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'name', title: 'Name', width: 100 },
{ id: 'score', title: 'Score', kind: 'number', width: 100 },
],
rows,
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
next = 5;
api.setSortModel([{ columnId: 'score', direction: 'desc' }]);
const off = api.on('sortChanged', () => log('Sort changed: the temporary places are gone'));
return () => off();
}
export const actions = {
addSorted: (api: GridApi<Row>) => api.addRows([{ id: next++, name: `New${next}`, score: 80 }]),
addStay: (api: GridApi<Row>) =>
api.addRows([{ id: next++, name: `New${next}`, score: 80 }], { resort: false }),
bumpSorted: (api: GridApi<Row>) => api.updateRows([{ id: 4, name: 'Olivia', score: 99 }]),
bumpStay: (api: GridApi<Row>) =>
api.updateRows([{ id: 4, name: 'Olivia', score: 99 }], { resort: false }),
resort: (api: GridApi<Row>) =>
api.setSortModel([
{ columnId: 'score', direction: 'desc' },
{ columnId: 'id', direction: 'asc' },
]),
};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.addSorted(api)"
>Add (sorted)</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.addStay(api)"
>Add (at the end)</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.bumpSorted(api)"
>Bump (sorted)</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.bumpStay(api)"
>Bump (stay)</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.resort(api)"
>Apply sort again</button>
</div>
<mx-grid style="height: 250px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
score: number;
}
const rows: Row[] = [
{ id: 1, name: 'Liam', score: 90 },
{ id: 2, name: 'Emma', score: 70 },
{ id: 3, name: 'Noah', score: 50 },
{ id: 4, name: 'Olivia', score: 30 },
];
let next = 5;
// Adding or editing rows while sorted makes lines jump right away. With resort: false, added rows go to the end
// and edited rows stay where they are until the next sort or filter change.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 70 },
{ id: 'name', title: 'Name', width: 100 },
{ id: 'score', title: 'Score', kind: 'number', width: 100 },
],
rows,
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
next = 5;
api.setSortModel([{ columnId: 'score', direction: 'desc' }]);
const off = api.on('sortChanged', () => log('Sort changed: the temporary places are gone'));
return () => off();
}
export const actions = {
addSorted: (api: GridApi<Row>) => api.addRows([{ id: next++, name: `New${next}`, score: 80 }]),
addStay: (api: GridApi<Row>) =>
api.addRows([{ id: next++, name: `New${next}`, score: 80 }], { resort: false }),
bumpSorted: (api: GridApi<Row>) => api.updateRows([{ id: 4, name: 'Olivia', score: 99 }]),
bumpStay: (api: GridApi<Row>) =>
api.updateRows([{ id: 4, name: 'Olivia', score: 99 }], { resort: false }),
resort: (api: GridApi<Row>) =>
api.setSortModel([
{ columnId: 'score', direction: 'desc' },
{ columnId: 'id', direction: 'asc' },
]),
};