컬럼이 1,000개여도 부드럽게A thousand columns, still smooth
지표가 1,000개나 되는 아주 넓은 표도 화면에 보이는 컬럼만 그려서 빠르게 움직여요. 번호는 왼쪽, 합계는 오른쪽에 고정해 두면 가로로 멀리 가도 어느 줄인지 알 수 있어요.Even a table with 1,000 metric columns stays fast because only the visible columns are drawn. Pinning the No. left and the total right keeps rows readable while you scroll far.
코드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="first">첫 컬럼으로</button>
<button type="button" id="middle">500번째로</button>
<button type="button" id="last">마지막 컬럼으로</button>
</div>
<div id="grid" style="height: 420px"></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('first').addEventListener('click', () => actions.first(api));
document.getElementById('middle').addEventListener('click', () => actions.middle(api));
document.getElementById('last').addEventListener('click', () => actions.last(api));const COLUMNS = 1000;
// 컬럼이 1,000개여도 화면에 보이는 컬럼만 그려서 빨라요(가로 가상화).
// 맨 왼쪽 '번호'는 왼쪽에, '합계'는 오른쪽에 고정해서 가로로 스크롤해도 늘 보여요.
export const options = {
columns: [
{ id: 'no', title: '번호', width: 70, pinned: 'start', align: 'end' },
...Array.from({ length: COLUMNS }, (_, c) => ({
id: `c${c}`,
title: `지표 ${c + 1}`,
kind: 'number',
width: 96,
align: 'end',
})),
{ id: 'sum', title: '합계', kind: 'number', width: 110, pinned: 'end', align: 'end' },
],
rows: Array.from({ length: 200 }, (_, r) => {
const row = { id: r, no: r + 1, sum: 0 };
let sum = 0;
for (let c = 0; c < COLUMNS; c += 1) {
const value = ((r + 1) * (c + 3)) % 997;
row[`c${c}`] = value;
sum += value;
}
row.sum = sum;
return row;
}),
rowKey: 'id',
};
export function setup(api, log) {
// 스크롤이 멈춘 뒤(그리기가 끝난 뒤) 한 줄에 몇 칸이 그려져 있는지 센다.
const count = () => document.querySelector('[role="row"].mxg-row')?.querySelectorAll('[role="gridcell"]').length ??
0;
let timer;
const off = api.on('scrolled', () => {
clearTimeout(timer);
timer = setTimeout(() => log({
ko: `지금 그려진 칸: 한 줄에 ${count()}개 (전체 ${COLUMNS + 2}컬럼)`,
en: `Cells drawn now: ${count()} per row (${COLUMNS + 2} columns in all)`,
}), 150);
});
return () => {
clearTimeout(timer);
off();
};
}
export const actions = {
middle: (api) => api.focusCell(1, 'c500'),
last: (api) => api.focusCell(1, `c${COLUMNS - 1}`),
first: (api) => api.focusCell(1, 'c0'),
};<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>컬럼이 1,000개여도 부드럽게</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="first">첫 컬럼으로</button>
<button type="button" id="middle">500번째로</button>
<button type="button" id="last">마지막 컬럼으로</button>
</div>
<div id="grid" style="height: 420px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const COLUMNS = 1000;
// 컬럼이 1,000개여도 화면에 보이는 컬럼만 그려서 빨라요(가로 가상화).
// 맨 왼쪽 '번호'는 왼쪽에, '합계'는 오른쪽에 고정해서 가로로 스크롤해도 늘 보여요.
const options = {
columns: [
{ id: 'no', title: '번호', width: 70, pinned: 'start', align: 'end' },
...Array.from({ length: COLUMNS }, (_, c) => ({
id: `c${c}`,
title: `지표 ${c + 1}`,
kind: 'number',
width: 96,
align: 'end',
})),
{ id: 'sum', title: '합계', kind: 'number', width: 110, pinned: 'end', align: 'end' },
],
rows: Array.from({ length: 200 }, (_, r) => {
const row = { id: r, no: r + 1, sum: 0 };
let sum = 0;
for (let c = 0; c < COLUMNS; c += 1) {
const value = ((r + 1) * (c + 3)) % 997;
row[`c${c}`] = value;
sum += value;
}
row.sum = sum;
return row;
}),
rowKey: 'id',
};
function setup(api, log) {
// 스크롤이 멈춘 뒤(그리기가 끝난 뒤) 한 줄에 몇 칸이 그려져 있는지 센다.
const count = () => document.querySelector('[role="row"].mxg-row')?.querySelectorAll('[role="gridcell"]').length ??
0;
let timer;
const off = api.on('scrolled', () => {
clearTimeout(timer);
timer = setTimeout(() => log({
ko: `지금 그려진 칸: 한 줄에 ${count()}개 (전체 ${COLUMNS + 2}컬럼)`,
en: `Cells drawn now: ${count()} per row (${COLUMNS + 2} columns in all)`,
}), 150);
});
return () => {
clearTimeout(timer);
off();
};
}
const actions = {
middle: (api) => api.focusCell(1, 'c500'),
last: (api) => api.focusCell(1, `c${COLUMNS - 1}`),
first: (api) => api.focusCell(1, 'c0'),
};
options.ariaLabel = "컬럼이 1,000개여도 부드럽게";
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('first').addEventListener('click', () => actions.first(api));
document.getElementById('middle').addEventListener('click', () => actions.middle(api));
document.getElementById('last').addEventListener('click', () => actions.last(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.first(api)}
>첫 컬럼으로</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.middle(api)}
>500번째로</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.last(api)}
>마지막 컬럼으로</button>
</div>
<Grid style={{ height: 420 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export type Row = Record<string, number | string>;
const COLUMNS = 1000;
// 컬럼이 1,000개여도 화면에 보이는 컬럼만 그려서 빨라요(가로 가상화).
// 맨 왼쪽 '번호'는 왼쪽에, '합계'는 오른쪽에 고정해서 가로로 스크롤해도 늘 보여요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'no', title: '번호', width: 70, pinned: 'start', align: 'end' },
...Array.from({ length: COLUMNS }, (_, c) => ({
id: `c${c}`,
title: `지표 ${c + 1}`,
kind: 'number' as const,
width: 96,
align: 'end' as const,
})),
{ id: 'sum', title: '합계', kind: 'number', width: 110, pinned: 'end', align: 'end' },
],
rows: Array.from({ length: 200 }, (_, r) => {
const row: Row = { id: r, no: r + 1, sum: 0 };
let sum = 0;
for (let c = 0; c < COLUMNS; c += 1) {
const value = ((r + 1) * (c + 3)) % 997;
row[`c${c}`] = value;
sum += value;
}
row.sum = sum;
return row;
}),
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
// 스크롤이 멈춘 뒤(그리기가 끝난 뒤) 한 줄에 몇 칸이 그려져 있는지 센다.
const count = () =>
document.querySelector('[role="row"].mxg-row')?.querySelectorAll('[role="gridcell"]').length ??
0;
let timer: ReturnType<typeof setTimeout> | undefined;
const off = api.on('scrolled', () => {
clearTimeout(timer);
timer = setTimeout(
() =>
log({
ko: `지금 그려진 칸: 한 줄에 ${count()}개 (전체 ${COLUMNS + 2}컬럼)`,
en: `Cells drawn now: ${count()} per row (${COLUMNS + 2} columns in all)`,
}),
150,
);
});
return () => {
clearTimeout(timer);
off();
};
}
export const actions = {
middle: (api: GridApi<Row>) => api.focusCell(1, 'c500'),
last: (api: GridApi<Row>) => api.focusCell(1, `c${COLUMNS - 1}`),
first: (api: GridApi<Row>) => api.focusCell(1, 'c0'),
};버튼을 누르면 이 예제의 코드가 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.first(api)">첫 컬럼으로</button>
<button type="button" :disabled="!api" @click="api && actions.middle(api)">500번째로</button>
<button type="button" :disabled="!api" @click="api && actions.last(api)">마지막 컬럼으로</button>
</div>
<Grid style="height: 420px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export type Row = Record<string, number | string>;
const COLUMNS = 1000;
// 컬럼이 1,000개여도 화면에 보이는 컬럼만 그려서 빨라요(가로 가상화).
// 맨 왼쪽 '번호'는 왼쪽에, '합계'는 오른쪽에 고정해서 가로로 스크롤해도 늘 보여요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'no', title: '번호', width: 70, pinned: 'start', align: 'end' },
...Array.from({ length: COLUMNS }, (_, c) => ({
id: `c${c}`,
title: `지표 ${c + 1}`,
kind: 'number' as const,
width: 96,
align: 'end' as const,
})),
{ id: 'sum', title: '합계', kind: 'number', width: 110, pinned: 'end', align: 'end' },
],
rows: Array.from({ length: 200 }, (_, r) => {
const row: Row = { id: r, no: r + 1, sum: 0 };
let sum = 0;
for (let c = 0; c < COLUMNS; c += 1) {
const value = ((r + 1) * (c + 3)) % 997;
row[`c${c}`] = value;
sum += value;
}
row.sum = sum;
return row;
}),
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
// 스크롤이 멈춘 뒤(그리기가 끝난 뒤) 한 줄에 몇 칸이 그려져 있는지 센다.
const count = () =>
document.querySelector('[role="row"].mxg-row')?.querySelectorAll('[role="gridcell"]').length ??
0;
let timer: ReturnType<typeof setTimeout> | undefined;
const off = api.on('scrolled', () => {
clearTimeout(timer);
timer = setTimeout(
() =>
log({
ko: `지금 그려진 칸: 한 줄에 ${count()}개 (전체 ${COLUMNS + 2}컬럼)`,
en: `Cells drawn now: ${count()} per row (${COLUMNS + 2} columns in all)`,
}),
150,
);
});
return () => {
clearTimeout(timer);
off();
};
}
export const actions = {
middle: (api: GridApi<Row>) => api.focusCell(1, 'c500'),
last: (api: GridApi<Row>) => api.focusCell(1, `c${COLUMNS - 1}`),
first: (api: GridApi<Row>) => api.focusCell(1, 'c0'),
};버튼을 누르면 이 예제의 코드가 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.first(api)"
>첫 컬럼으로</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.middle(api)"
>500번째로</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.last(api)"
>마지막 컬럼으로</button>
</div>
<mx-grid style="height: 420px" [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 type Row = Record<string, number | string>;
const COLUMNS = 1000;
// 컬럼이 1,000개여도 화면에 보이는 컬럼만 그려서 빨라요(가로 가상화).
// 맨 왼쪽 '번호'는 왼쪽에, '합계'는 오른쪽에 고정해서 가로로 스크롤해도 늘 보여요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'no', title: '번호', width: 70, pinned: 'start', align: 'end' },
...Array.from({ length: COLUMNS }, (_, c) => ({
id: `c${c}`,
title: `지표 ${c + 1}`,
kind: 'number' as const,
width: 96,
align: 'end' as const,
})),
{ id: 'sum', title: '합계', kind: 'number', width: 110, pinned: 'end', align: 'end' },
],
rows: Array.from({ length: 200 }, (_, r) => {
const row: Row = { id: r, no: r + 1, sum: 0 };
let sum = 0;
for (let c = 0; c < COLUMNS; c += 1) {
const value = ((r + 1) * (c + 3)) % 997;
row[`c${c}`] = value;
sum += value;
}
row.sum = sum;
return row;
}),
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
// 스크롤이 멈춘 뒤(그리기가 끝난 뒤) 한 줄에 몇 칸이 그려져 있는지 센다.
const count = () =>
document.querySelector('[role="row"].mxg-row')?.querySelectorAll('[role="gridcell"]').length ??
0;
let timer: ReturnType<typeof setTimeout> | undefined;
const off = api.on('scrolled', () => {
clearTimeout(timer);
timer = setTimeout(
() =>
log({
ko: `지금 그려진 칸: 한 줄에 ${count()}개 (전체 ${COLUMNS + 2}컬럼)`,
en: `Cells drawn now: ${count()} per row (${COLUMNS + 2} columns in all)`,
}),
150,
);
});
return () => {
clearTimeout(timer);
off();
};
}
export const actions = {
middle: (api: GridApi<Row>) => api.focusCell(1, 'c500'),
last: (api: GridApi<Row>) => api.focusCell(1, `c${COLUMNS - 1}`),
first: (api: GridApi<Row>) => api.focusCell(1, 'c0'),
};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="first">To the first</button>
<button type="button" id="middle">To column 500</button>
<button type="button" id="last">To the last</button>
</div>
<div id="grid" style="height: 420px"></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('first').addEventListener('click', () => actions.first(api));
document.getElementById('middle').addEventListener('click', () => actions.middle(api));
document.getElementById('last').addEventListener('click', () => actions.last(api));const COLUMNS = 1000;
// Even with 1,000 columns it stays fast because only the visible columns are drawn (horizontal virtualization).
// 'No.' is pinned on the left and 'Total' on the right, so both stay visible while scrolling sideways.
export const options = {
columns: [
{ id: 'no', title: 'No.', width: 70, pinned: 'start', align: 'end' },
...Array.from({ length: COLUMNS }, (_, c) => ({
id: `c${c}`,
title: `Metric ${c + 1}`,
kind: 'number',
width: 112,
align: 'end',
})),
{ id: 'sum', title: 'Total', kind: 'number', width: 110, pinned: 'end', align: 'end' },
],
rows: Array.from({ length: 200 }, (_, r) => {
const row = { id: r, no: r + 1, sum: 0 };
let sum = 0;
for (let c = 0; c < COLUMNS; c += 1) {
const value = ((r + 1) * (c + 3)) % 997;
row[`c${c}`] = value;
sum += value;
}
row.sum = sum;
return row;
}),
rowKey: 'id',
};
export function setup(api, log) {
// After scrolling stops (drawing is done), count how many cells are drawn in one row.
const count = () => document.querySelector('[role="row"].mxg-row')?.querySelectorAll('[role="gridcell"]').length ??
0;
let timer;
const off = api.on('scrolled', () => {
clearTimeout(timer);
timer = setTimeout(() => log(`Cells drawn now: ${count()} per row (${COLUMNS + 2} columns in all)`), 150);
});
return () => {
clearTimeout(timer);
off();
};
}
export const actions = {
middle: (api) => api.focusCell(1, 'c500'),
last: (api) => api.focusCell(1, `c${COLUMNS - 1}`),
first: (api) => api.focusCell(1, 'c0'),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>A thousand columns, still smooth</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="first">To the first</button>
<button type="button" id="middle">To column 500</button>
<button type="button" id="last">To the last</button>
</div>
<div id="grid" style="height: 420px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const COLUMNS = 1000;
// Even with 1,000 columns it stays fast because only the visible columns are drawn (horizontal virtualization).
// 'No.' is pinned on the left and 'Total' on the right, so both stay visible while scrolling sideways.
const options = {
columns: [
{ id: 'no', title: 'No.', width: 70, pinned: 'start', align: 'end' },
...Array.from({ length: COLUMNS }, (_, c) => ({
id: `c${c}`,
title: `Metric ${c + 1}`,
kind: 'number',
width: 112,
align: 'end',
})),
{ id: 'sum', title: 'Total', kind: 'number', width: 110, pinned: 'end', align: 'end' },
],
rows: Array.from({ length: 200 }, (_, r) => {
const row = { id: r, no: r + 1, sum: 0 };
let sum = 0;
for (let c = 0; c < COLUMNS; c += 1) {
const value = ((r + 1) * (c + 3)) % 997;
row[`c${c}`] = value;
sum += value;
}
row.sum = sum;
return row;
}),
rowKey: 'id',
};
function setup(api, log) {
// After scrolling stops (drawing is done), count how many cells are drawn in one row.
const count = () => document.querySelector('[role="row"].mxg-row')?.querySelectorAll('[role="gridcell"]').length ??
0;
let timer;
const off = api.on('scrolled', () => {
clearTimeout(timer);
timer = setTimeout(() => log(`Cells drawn now: ${count()} per row (${COLUMNS + 2} columns in all)`), 150);
});
return () => {
clearTimeout(timer);
off();
};
}
const actions = {
middle: (api) => api.focusCell(1, 'c500'),
last: (api) => api.focusCell(1, `c${COLUMNS - 1}`),
first: (api) => api.focusCell(1, 'c0'),
};
options.ariaLabel = "A thousand columns, still smooth";
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('first').addEventListener('click', () => actions.first(api));
document.getElementById('middle').addEventListener('click', () => actions.middle(api));
document.getElementById('last').addEventListener('click', () => actions.last(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.first(api)}
>To the first</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.middle(api)}
>To column 500</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.last(api)}
>To the last</button>
</div>
<Grid style={{ height: 420 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export type Row = Record<string, number | string>;
const COLUMNS = 1000;
// Even with 1,000 columns it stays fast because only the visible columns are drawn (horizontal virtualization).
// 'No.' is pinned on the left and 'Total' on the right, so both stay visible while scrolling sideways.
export const options: GridOptions<Row> = {
columns: [
{ id: 'no', title: 'No.', width: 70, pinned: 'start', align: 'end' },
...Array.from({ length: COLUMNS }, (_, c) => ({
id: `c${c}`,
title: `Metric ${c + 1}`,
kind: 'number' as const,
width: 112,
align: 'end' as const,
})),
{ id: 'sum', title: 'Total', kind: 'number', width: 110, pinned: 'end', align: 'end' },
],
rows: Array.from({ length: 200 }, (_, r) => {
const row: Row = { id: r, no: r + 1, sum: 0 };
let sum = 0;
for (let c = 0; c < COLUMNS; c += 1) {
const value = ((r + 1) * (c + 3)) % 997;
row[`c${c}`] = value;
sum += value;
}
row.sum = sum;
return row;
}),
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
// After scrolling stops (drawing is done), count how many cells are drawn in one row.
const count = () =>
document.querySelector('[role="row"].mxg-row')?.querySelectorAll('[role="gridcell"]').length ??
0;
let timer: ReturnType<typeof setTimeout> | undefined;
const off = api.on('scrolled', () => {
clearTimeout(timer);
timer = setTimeout(
() => log(`Cells drawn now: ${count()} per row (${COLUMNS + 2} columns in all)`),
150,
);
});
return () => {
clearTimeout(timer);
off();
};
}
export const actions = {
middle: (api: GridApi<Row>) => api.focusCell(1, 'c500'),
last: (api: GridApi<Row>) => api.focusCell(1, `c${COLUMNS - 1}`),
first: (api: GridApi<Row>) => api.focusCell(1, 'c0'),
};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.first(api)">To the first</button>
<button type="button" :disabled="!api" @click="api && actions.middle(api)">To column 500</button>
<button type="button" :disabled="!api" @click="api && actions.last(api)">To the last</button>
</div>
<Grid style="height: 420px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export type Row = Record<string, number | string>;
const COLUMNS = 1000;
// Even with 1,000 columns it stays fast because only the visible columns are drawn (horizontal virtualization).
// 'No.' is pinned on the left and 'Total' on the right, so both stay visible while scrolling sideways.
export const options: GridOptions<Row> = {
columns: [
{ id: 'no', title: 'No.', width: 70, pinned: 'start', align: 'end' },
...Array.from({ length: COLUMNS }, (_, c) => ({
id: `c${c}`,
title: `Metric ${c + 1}`,
kind: 'number' as const,
width: 112,
align: 'end' as const,
})),
{ id: 'sum', title: 'Total', kind: 'number', width: 110, pinned: 'end', align: 'end' },
],
rows: Array.from({ length: 200 }, (_, r) => {
const row: Row = { id: r, no: r + 1, sum: 0 };
let sum = 0;
for (let c = 0; c < COLUMNS; c += 1) {
const value = ((r + 1) * (c + 3)) % 997;
row[`c${c}`] = value;
sum += value;
}
row.sum = sum;
return row;
}),
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
// After scrolling stops (drawing is done), count how many cells are drawn in one row.
const count = () =>
document.querySelector('[role="row"].mxg-row')?.querySelectorAll('[role="gridcell"]').length ??
0;
let timer: ReturnType<typeof setTimeout> | undefined;
const off = api.on('scrolled', () => {
clearTimeout(timer);
timer = setTimeout(
() => log(`Cells drawn now: ${count()} per row (${COLUMNS + 2} columns in all)`),
150,
);
});
return () => {
clearTimeout(timer);
off();
};
}
export const actions = {
middle: (api: GridApi<Row>) => api.focusCell(1, 'c500'),
last: (api: GridApi<Row>) => api.focusCell(1, `c${COLUMNS - 1}`),
first: (api: GridApi<Row>) => api.focusCell(1, 'c0'),
};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.first(api)"
>To the first</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.middle(api)"
>To column 500</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.last(api)"
>To the last</button>
</div>
<mx-grid style="height: 420px" [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 type Row = Record<string, number | string>;
const COLUMNS = 1000;
// Even with 1,000 columns it stays fast because only the visible columns are drawn (horizontal virtualization).
// 'No.' is pinned on the left and 'Total' on the right, so both stay visible while scrolling sideways.
export const options: GridOptions<Row> = {
columns: [
{ id: 'no', title: 'No.', width: 70, pinned: 'start', align: 'end' },
...Array.from({ length: COLUMNS }, (_, c) => ({
id: `c${c}`,
title: `Metric ${c + 1}`,
kind: 'number' as const,
width: 112,
align: 'end' as const,
})),
{ id: 'sum', title: 'Total', kind: 'number', width: 110, pinned: 'end', align: 'end' },
],
rows: Array.from({ length: 200 }, (_, r) => {
const row: Row = { id: r, no: r + 1, sum: 0 };
let sum = 0;
for (let c = 0; c < COLUMNS; c += 1) {
const value = ((r + 1) * (c + 3)) % 997;
row[`c${c}`] = value;
sum += value;
}
row.sum = sum;
return row;
}),
rowKey: 'id',
};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
// After scrolling stops (drawing is done), count how many cells are drawn in one row.
const count = () =>
document.querySelector('[role="row"].mxg-row')?.querySelectorAll('[role="gridcell"]').length ??
0;
let timer: ReturnType<typeof setTimeout> | undefined;
const off = api.on('scrolled', () => {
clearTimeout(timer);
timer = setTimeout(
() => log(`Cells drawn now: ${count()} per row (${COLUMNS + 2} columns in all)`),
150,
);
});
return () => {
clearTimeout(timer);
off();
};
}
export const actions = {
middle: (api: GridApi<Row>) => api.focusCell(1, 'c500'),
last: (api: GridApi<Row>) => api.focusCell(1, `c${COLUMNS - 1}`),
first: (api: GridApi<Row>) => api.focusCell(1, 'c0'),
};