데이터를 새로 넣어도 보던 자리 지키기Keep the view when you load new data
setRows(rows, { scroll, keep })로 스크롤 위치, 선택, 포커스, 정렬, 필터, 쪽, 펼침 중 무엇을 남길지 정해요. 기본은 모두 남기고 새 데이터에 없는 키만 정리해요.setRows(rows, { scroll, keep }) decides which of scroll position, selection, focus, sort, filter, page and expansion stay. By default all stay and only keys missing from the new data are dropped.
코드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="selectSome">세 행 선택</button>
<button type="button" id="keep">새 데이터 (유지)</button>
<button type="button" id="top">새 데이터 (맨 위로)</button>
<button type="button" id="reset">모두 초기화</button>
</div>
<div id="grid" style="height: 300px"></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('selectSome').addEventListener('click', () => actions.selectSome(api));
document.getElementById('keep').addEventListener('click', () => actions.keep(api));
document.getElementById('top').addEventListener('click', () => actions.top(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));const make = (stamp) => Array.from({ length: 300 }, (_, i) => ({
id: i + 1,
name: `행 ${String(i + 1).padStart(3, '0')}`,
value: (i * 7 + stamp) % 100,
}));
// setRows(rows, { scroll, keep })는 새 데이터를 넣을 때 무엇을 남길지 정해요.
// 아무것도 주지 않으면 스크롤·정렬·선택이 남고, scroll: 'top'은 맨 위로, keep 배열은 적은 것만 남겨요.
export const options = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 80 },
{ id: 'name', title: '이름', width: 140 },
{ id: 'value', title: '값', kind: 'number', width: 100 },
],
rows: make(0),
rowKey: 'id',
selection: 'multiple',
};
let round = 0;
export function setup(api, log) {
api.setSortModel([{ columnId: 'value', direction: 'desc' }]);
round = 0;
const off = api.on('rowsChanged', (e) => log({ ko: `행 바뀜: ${e.reason} ${e.count}개`, en: `Rows changed: ${e.reason}, ${e.count}` }));
return () => off();
}
export const actions = {
keep: (api) => api.setRows(make(++round)),
top: (api) => api.setRows(make(++round), { scroll: 'top' }),
reset: (api) => api.setRows(make(++round), { scroll: 'top', keep: [] }),
selectSome: (api) => api.selectRows([3, 150, 299]),
};<!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="selectSome">세 행 선택</button>
<button type="button" id="keep">새 데이터 (유지)</button>
<button type="button" id="top">새 데이터 (맨 위로)</button>
<button type="button" id="reset">모두 초기화</button>
</div>
<div id="grid" style="height: 300px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const make = (stamp) => Array.from({ length: 300 }, (_, i) => ({
id: i + 1,
name: `행 ${String(i + 1).padStart(3, '0')}`,
value: (i * 7 + stamp) % 100,
}));
// setRows(rows, { scroll, keep })는 새 데이터를 넣을 때 무엇을 남길지 정해요.
// 아무것도 주지 않으면 스크롤·정렬·선택이 남고, scroll: 'top'은 맨 위로, keep 배열은 적은 것만 남겨요.
const options = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 80 },
{ id: 'name', title: '이름', width: 140 },
{ id: 'value', title: '값', kind: 'number', width: 100 },
],
rows: make(0),
rowKey: 'id',
selection: 'multiple',
};
let round = 0;
function setup(api, log) {
api.setSortModel([{ columnId: 'value', direction: 'desc' }]);
round = 0;
const off = api.on('rowsChanged', (e) => log({ ko: `행 바뀜: ${e.reason} ${e.count}개`, en: `Rows changed: ${e.reason}, ${e.count}` }));
return () => off();
}
const actions = {
keep: (api) => api.setRows(make(++round)),
top: (api) => api.setRows(make(++round), { scroll: 'top' }),
reset: (api) => api.setRows(make(++round), { scroll: 'top', keep: [] }),
selectSome: (api) => api.selectRows([3, 150, 299]),
};
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('selectSome').addEventListener('click', () => actions.selectSome(api));
document.getElementById('keep').addEventListener('click', () => actions.keep(api));
document.getElementById('top').addEventListener('click', () => actions.top(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(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.selectSome(api)}
>세 행 선택</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.keep(api)}
>새 데이터 (유지)</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.top(api)}
>새 데이터 (맨 위로)</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.reset(api)}
>모두 초기화</button>
</div>
<Grid style={{ height: 300 }} 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;
value: number;
}
const make = (stamp: number): Row[] =>
Array.from({ length: 300 }, (_, i) => ({
id: i + 1,
name: `행 ${String(i + 1).padStart(3, '0')}`,
value: (i * 7 + stamp) % 100,
}));
// setRows(rows, { scroll, keep })는 새 데이터를 넣을 때 무엇을 남길지 정해요.
// 아무것도 주지 않으면 스크롤·정렬·선택이 남고, scroll: 'top'은 맨 위로, keep 배열은 적은 것만 남겨요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 80 },
{ id: 'name', title: '이름', width: 140 },
{ id: 'value', title: '값', kind: 'number', width: 100 },
],
rows: make(0),
rowKey: 'id',
selection: 'multiple',
};
let round = 0;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
api.setSortModel([{ columnId: 'value', direction: 'desc' }]);
round = 0;
const off = api.on('rowsChanged', (e) =>
log({ ko: `행 바뀜: ${e.reason} ${e.count}개`, en: `Rows changed: ${e.reason}, ${e.count}` }),
);
return () => off();
}
export const actions = {
keep: (api: GridApi<Row>) => api.setRows(make(++round)),
top: (api: GridApi<Row>) => api.setRows(make(++round), { scroll: 'top' }),
reset: (api: GridApi<Row>) => api.setRows(make(++round), { scroll: 'top', keep: [] }),
selectSome: (api: GridApi<Row>) => api.selectRows([3, 150, 299]),
};버튼을 누르면 이 예제의 코드가 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.selectSome(api)"
>세 행 선택</button>
<button type="button" :disabled="!api" @click="api && actions.keep(api)">새 데이터 (유지)</button>
<button type="button" :disabled="!api" @click="api && actions.top(api)">새 데이터 (맨 위로)</button>
<button type="button" :disabled="!api" @click="api && actions.reset(api)">모두 초기화</button>
</div>
<Grid style="height: 300px" :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;
value: number;
}
const make = (stamp: number): Row[] =>
Array.from({ length: 300 }, (_, i) => ({
id: i + 1,
name: `행 ${String(i + 1).padStart(3, '0')}`,
value: (i * 7 + stamp) % 100,
}));
// setRows(rows, { scroll, keep })는 새 데이터를 넣을 때 무엇을 남길지 정해요.
// 아무것도 주지 않으면 스크롤·정렬·선택이 남고, scroll: 'top'은 맨 위로, keep 배열은 적은 것만 남겨요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 80 },
{ id: 'name', title: '이름', width: 140 },
{ id: 'value', title: '값', kind: 'number', width: 100 },
],
rows: make(0),
rowKey: 'id',
selection: 'multiple',
};
let round = 0;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
api.setSortModel([{ columnId: 'value', direction: 'desc' }]);
round = 0;
const off = api.on('rowsChanged', (e) =>
log({ ko: `행 바뀜: ${e.reason} ${e.count}개`, en: `Rows changed: ${e.reason}, ${e.count}` }),
);
return () => off();
}
export const actions = {
keep: (api: GridApi<Row>) => api.setRows(make(++round)),
top: (api: GridApi<Row>) => api.setRows(make(++round), { scroll: 'top' }),
reset: (api: GridApi<Row>) => api.setRows(make(++round), { scroll: 'top', keep: [] }),
selectSome: (api: GridApi<Row>) => api.selectRows([3, 150, 299]),
};버튼을 누르면 이 예제의 코드가 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.selectSome(api)"
>세 행 선택</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.keep(api)"
>새 데이터 (유지)</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.top(api)"
>새 데이터 (맨 위로)</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.reset(api)"
>모두 초기화</button>
</div>
<mx-grid style="height: 300px" [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;
value: number;
}
const make = (stamp: number): Row[] =>
Array.from({ length: 300 }, (_, i) => ({
id: i + 1,
name: `행 ${String(i + 1).padStart(3, '0')}`,
value: (i * 7 + stamp) % 100,
}));
// setRows(rows, { scroll, keep })는 새 데이터를 넣을 때 무엇을 남길지 정해요.
// 아무것도 주지 않으면 스크롤·정렬·선택이 남고, scroll: 'top'은 맨 위로, keep 배열은 적은 것만 남겨요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 80 },
{ id: 'name', title: '이름', width: 140 },
{ id: 'value', title: '값', kind: 'number', width: 100 },
],
rows: make(0),
rowKey: 'id',
selection: 'multiple',
};
let round = 0;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
api.setSortModel([{ columnId: 'value', direction: 'desc' }]);
round = 0;
const off = api.on('rowsChanged', (e) =>
log({ ko: `행 바뀜: ${e.reason} ${e.count}개`, en: `Rows changed: ${e.reason}, ${e.count}` }),
);
return () => off();
}
export const actions = {
keep: (api: GridApi<Row>) => api.setRows(make(++round)),
top: (api: GridApi<Row>) => api.setRows(make(++round), { scroll: 'top' }),
reset: (api: GridApi<Row>) => api.setRows(make(++round), { scroll: 'top', keep: [] }),
selectSome: (api: GridApi<Row>) => api.selectRows([3, 150, 299]),
};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="selectSome">Select three rows</button>
<button type="button" id="keep">New data (keep)</button>
<button type="button" id="top">New data (to top)</button>
<button type="button" id="reset">Reset everything</button>
</div>
<div id="grid" style="height: 300px"></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('selectSome').addEventListener('click', () => actions.selectSome(api));
document.getElementById('keep').addEventListener('click', () => actions.keep(api));
document.getElementById('top').addEventListener('click', () => actions.top(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));const make = (stamp) => Array.from({ length: 300 }, (_, i) => ({
id: i + 1,
name: `Row ${String(i + 1).padStart(3, '0')}`,
value: (i * 7 + stamp) % 100,
}));
// setRows(rows, { scroll, keep }) decides what to keep when new data is loaded.
// With no options, scroll, sort and selection are kept; scroll: 'top' jumps to the top; a keep array keeps only what it lists.
export const options = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 80 },
{ id: 'name', title: 'Name', width: 140 },
{ id: 'value', title: 'Value', kind: 'number', width: 100 },
],
rows: make(0),
rowKey: 'id',
selection: 'multiple',
};
let round = 0;
export function setup(api, log) {
api.setSortModel([{ columnId: 'value', direction: 'desc' }]);
round = 0;
const off = api.on('rowsChanged', (e) => log(`Rows changed: ${e.reason}, ${e.count}`));
return () => off();
}
export const actions = {
keep: (api) => api.setRows(make(++round)),
top: (api) => api.setRows(make(++round), { scroll: 'top' }),
reset: (api) => api.setRows(make(++round), { scroll: 'top', keep: [] }),
selectSome: (api) => api.selectRows([3, 150, 299]),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Keep the view when you load new data</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="selectSome">Select three rows</button>
<button type="button" id="keep">New data (keep)</button>
<button type="button" id="top">New data (to top)</button>
<button type="button" id="reset">Reset everything</button>
</div>
<div id="grid" style="height: 300px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const make = (stamp) => Array.from({ length: 300 }, (_, i) => ({
id: i + 1,
name: `Row ${String(i + 1).padStart(3, '0')}`,
value: (i * 7 + stamp) % 100,
}));
// setRows(rows, { scroll, keep }) decides what to keep when new data is loaded.
// With no options, scroll, sort and selection are kept; scroll: 'top' jumps to the top; a keep array keeps only what it lists.
const options = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 80 },
{ id: 'name', title: 'Name', width: 140 },
{ id: 'value', title: 'Value', kind: 'number', width: 100 },
],
rows: make(0),
rowKey: 'id',
selection: 'multiple',
};
let round = 0;
function setup(api, log) {
api.setSortModel([{ columnId: 'value', direction: 'desc' }]);
round = 0;
const off = api.on('rowsChanged', (e) => log(`Rows changed: ${e.reason}, ${e.count}`));
return () => off();
}
const actions = {
keep: (api) => api.setRows(make(++round)),
top: (api) => api.setRows(make(++round), { scroll: 'top' }),
reset: (api) => api.setRows(make(++round), { scroll: 'top', keep: [] }),
selectSome: (api) => api.selectRows([3, 150, 299]),
};
options.ariaLabel = "Keep the view when you load new data";
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('selectSome').addEventListener('click', () => actions.selectSome(api));
document.getElementById('keep').addEventListener('click', () => actions.keep(api));
document.getElementById('top').addEventListener('click', () => actions.top(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(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.selectSome(api)}
>Select three rows</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.keep(api)}
>New data (keep)</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.top(api)}
>New data (to top)</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.reset(api)}
>Reset everything</button>
</div>
<Grid style={{ height: 300 }} 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;
value: number;
}
const make = (stamp: number): Row[] =>
Array.from({ length: 300 }, (_, i) => ({
id: i + 1,
name: `Row ${String(i + 1).padStart(3, '0')}`,
value: (i * 7 + stamp) % 100,
}));
// setRows(rows, { scroll, keep }) decides what to keep when new data is loaded.
// With no options, scroll, sort and selection are kept; scroll: 'top' jumps to the top; a keep array keeps only what it lists.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 80 },
{ id: 'name', title: 'Name', width: 140 },
{ id: 'value', title: 'Value', kind: 'number', width: 100 },
],
rows: make(0),
rowKey: 'id',
selection: 'multiple',
};
let round = 0;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
api.setSortModel([{ columnId: 'value', direction: 'desc' }]);
round = 0;
const off = api.on('rowsChanged', (e) => log(`Rows changed: ${e.reason}, ${e.count}`));
return () => off();
}
export const actions = {
keep: (api: GridApi<Row>) => api.setRows(make(++round)),
top: (api: GridApi<Row>) => api.setRows(make(++round), { scroll: 'top' }),
reset: (api: GridApi<Row>) => api.setRows(make(++round), { scroll: 'top', keep: [] }),
selectSome: (api: GridApi<Row>) => api.selectRows([3, 150, 299]),
};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.selectSome(api)"
>Select three rows</button>
<button type="button" :disabled="!api" @click="api && actions.keep(api)">New data (keep)</button>
<button type="button" :disabled="!api" @click="api && actions.top(api)">New data (to top)</button>
<button type="button" :disabled="!api" @click="api && actions.reset(api)">Reset everything</button>
</div>
<Grid style="height: 300px" :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;
value: number;
}
const make = (stamp: number): Row[] =>
Array.from({ length: 300 }, (_, i) => ({
id: i + 1,
name: `Row ${String(i + 1).padStart(3, '0')}`,
value: (i * 7 + stamp) % 100,
}));
// setRows(rows, { scroll, keep }) decides what to keep when new data is loaded.
// With no options, scroll, sort and selection are kept; scroll: 'top' jumps to the top; a keep array keeps only what it lists.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 80 },
{ id: 'name', title: 'Name', width: 140 },
{ id: 'value', title: 'Value', kind: 'number', width: 100 },
],
rows: make(0),
rowKey: 'id',
selection: 'multiple',
};
let round = 0;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
api.setSortModel([{ columnId: 'value', direction: 'desc' }]);
round = 0;
const off = api.on('rowsChanged', (e) => log(`Rows changed: ${e.reason}, ${e.count}`));
return () => off();
}
export const actions = {
keep: (api: GridApi<Row>) => api.setRows(make(++round)),
top: (api: GridApi<Row>) => api.setRows(make(++round), { scroll: 'top' }),
reset: (api: GridApi<Row>) => api.setRows(make(++round), { scroll: 'top', keep: [] }),
selectSome: (api: GridApi<Row>) => api.selectRows([3, 150, 299]),
};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.selectSome(api)"
>Select three rows</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.keep(api)"
>New data (keep)</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.top(api)"
>New data (to top)</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.reset(api)"
>Reset everything</button>
</div>
<mx-grid style="height: 300px" [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;
value: number;
}
const make = (stamp: number): Row[] =>
Array.from({ length: 300 }, (_, i) => ({
id: i + 1,
name: `Row ${String(i + 1).padStart(3, '0')}`,
value: (i * 7 + stamp) % 100,
}));
// setRows(rows, { scroll, keep }) decides what to keep when new data is loaded.
// With no options, scroll, sort and selection are kept; scroll: 'top' jumps to the top; a keep array keeps only what it lists.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 80 },
{ id: 'name', title: 'Name', width: 140 },
{ id: 'value', title: 'Value', kind: 'number', width: 100 },
],
rows: make(0),
rowKey: 'id',
selection: 'multiple',
};
let round = 0;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
api.setSortModel([{ columnId: 'value', direction: 'desc' }]);
round = 0;
const off = api.on('rowsChanged', (e) => log(`Rows changed: ${e.reason}, ${e.count}`));
return () => off();
}
export const actions = {
keep: (api: GridApi<Row>) => api.setRows(make(++round)),
top: (api: GridApi<Row>) => api.setRows(make(++round), { scroll: 'top' }),
reset: (api: GridApi<Row>) => api.setRows(make(++round), { scroll: 'top', keep: [] }),
selectSome: (api: GridApi<Row>) => api.selectRows([3, 150, 299]),
};