내용에 맞춰 자라는 줄 높이Row height that follows the content
rowHeight: 'auto'와 컬럼 wrap: true면 긴 글이 줄바꿈되고 줄이 그 높이만큼 자라요. 보이는 줄만 재서 200줄도 가벼워요.With rowHeight: 'auto' and column wrap: true, long text wraps and the row grows to fit. Only visible rows are measured, so 200 rows stay light.
코드Code
탭을 골라 코드를 복사하세요. 위 화면은 이 코드를 그대로 실행한 결과예요.Pick a tab and copy the code. The preview above runs exactly this code.
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<div id="app">
<div class="toolbar">
<button type="button" id="narrow">좁게</button>
<button type="button" id="wide">넓게</button>
<button type="button" id="edit">2번 줄 고치기</button>
<button type="button" id="remeasure">다시 재기</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('narrow').addEventListener('click', () => actions.narrow(api));
document.getElementById('wide').addEventListener('click', () => actions.wide(api));
document.getElementById('edit').addEventListener('click', () => actions.edit(api));
document.getElementById('remeasure').addEventListener('click', () => actions.remeasure(api));const MEMOS = [
'짧은 메모',
'조금 긴 메모예요. 폭이 좁으면 두 줄로 나뉘어요.',
'아주 긴 메모는 컬럼 폭 안에서 단어 단위로 줄바꿈하고, 줄 높이는 내용에 맞춰 자라요. 폭을 바꾸면 다시 재요.',
];
export const options = {
columns: [
{ id: 'title', title: '제목', width: 120 },
{ id: 'memo', title: '메모', width: 240, wrap: true },
],
rows: Array.from({ length: 200 }, (_, i) => ({
id: i + 1,
title: `항목 ${i + 1}`,
memo: MEMOS[i % 3],
})),
rowKey: 'id',
rowHeight: 'auto',
};
let say = () => { };
export function setup(_api, log) {
say = log;
return () => { };
}
export const actions = {
narrow: (api) => {
api.resizeColumn('memo', 140);
say({ ko: '메모 폭 140px: 줄이 더 자라요', en: 'Memo width 140px: rows grow taller' });
},
wide: (api) => {
api.resizeColumn('memo', 420);
say({ ko: '메모 폭 420px: 줄이 줄어요', en: 'Memo width 420px: rows shrink' });
},
edit: (api) => {
api.updateRows([{ id: 2, title: '항목 2', memo: MEMOS[2] }]);
say({ ko: '2번 줄만 다시 재요', en: 'Only row 2 is measured again' });
},
remeasure: (api) => {
api.remeasureRows();
say({
ko: 'remeasureRows(): 보이는 줄을 다시 재요',
en: 'remeasureRows(): visible rows are measured again',
});
},
};<!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="narrow">좁게</button>
<button type="button" id="wide">넓게</button>
<button type="button" id="edit">2번 줄 고치기</button>
<button type="button" id="remeasure">다시 재기</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 MEMOS = [
'짧은 메모',
'조금 긴 메모예요. 폭이 좁으면 두 줄로 나뉘어요.',
'아주 긴 메모는 컬럼 폭 안에서 단어 단위로 줄바꿈하고, 줄 높이는 내용에 맞춰 자라요. 폭을 바꾸면 다시 재요.',
];
const options = {
columns: [
{ id: 'title', title: '제목', width: 120 },
{ id: 'memo', title: '메모', width: 240, wrap: true },
],
rows: Array.from({ length: 200 }, (_, i) => ({
id: i + 1,
title: `항목 ${i + 1}`,
memo: MEMOS[i % 3],
})),
rowKey: 'id',
rowHeight: 'auto',
};
let say = () => { };
function setup(_api, log) {
say = log;
return () => { };
}
const actions = {
narrow: (api) => {
api.resizeColumn('memo', 140);
say({ ko: '메모 폭 140px: 줄이 더 자라요', en: 'Memo width 140px: rows grow taller' });
},
wide: (api) => {
api.resizeColumn('memo', 420);
say({ ko: '메모 폭 420px: 줄이 줄어요', en: 'Memo width 420px: rows shrink' });
},
edit: (api) => {
api.updateRows([{ id: 2, title: '항목 2', memo: MEMOS[2] }]);
say({ ko: '2번 줄만 다시 재요', en: 'Only row 2 is measured again' });
},
remeasure: (api) => {
api.remeasureRows();
say({
ko: 'remeasureRows(): 보이는 줄을 다시 재요',
en: 'remeasureRows(): visible rows are measured again',
});
},
};
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('narrow').addEventListener('click', () => actions.narrow(api));
document.getElementById('wide').addEventListener('click', () => actions.wide(api));
document.getElementById('edit').addEventListener('click', () => actions.edit(api));
document.getElementById('remeasure').addEventListener('click', () => actions.remeasure(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.narrow(api)}
>좁게</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.wide(api)}
>넓게</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.edit(api)}
>2번 줄 고치기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.remeasure(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;
title: string;
memo: string;
}
const MEMOS = [
'짧은 메모',
'조금 긴 메모예요. 폭이 좁으면 두 줄로 나뉘어요.',
'아주 긴 메모는 컬럼 폭 안에서 단어 단위로 줄바꿈하고, 줄 높이는 내용에 맞춰 자라요. 폭을 바꾸면 다시 재요.',
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'title', title: '제목', width: 120 },
{ id: 'memo', title: '메모', width: 240, wrap: true },
],
rows: Array.from({ length: 200 }, (_, i) => ({
id: i + 1,
title: `항목 ${i + 1}`,
memo: MEMOS[i % 3] as string,
})),
rowKey: 'id',
rowHeight: 'auto',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {};
}
export const actions = {
narrow: (api: GridApi<Row>) => {
api.resizeColumn('memo', 140);
say({ ko: '메모 폭 140px: 줄이 더 자라요', en: 'Memo width 140px: rows grow taller' });
},
wide: (api: GridApi<Row>) => {
api.resizeColumn('memo', 420);
say({ ko: '메모 폭 420px: 줄이 줄어요', en: 'Memo width 420px: rows shrink' });
},
edit: (api: GridApi<Row>) => {
api.updateRows([{ id: 2, title: '항목 2', memo: MEMOS[2] as string }]);
say({ ko: '2번 줄만 다시 재요', en: 'Only row 2 is measured again' });
},
remeasure: (api: GridApi<Row>) => {
api.remeasureRows();
say({
ko: 'remeasureRows(): 보이는 줄을 다시 재요',
en: 'remeasureRows(): visible rows are measured again',
});
},
};버튼을 누르면 이 예제의 코드가 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.narrow(api)">좁게</button>
<button type="button" :disabled="!api" @click="api && actions.wide(api)">넓게</button>
<button type="button" :disabled="!api" @click="api && actions.edit(api)">2번 줄 고치기</button>
<button
type="button"
:disabled="!api"
@click="api && actions.remeasure(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;
title: string;
memo: string;
}
const MEMOS = [
'짧은 메모',
'조금 긴 메모예요. 폭이 좁으면 두 줄로 나뉘어요.',
'아주 긴 메모는 컬럼 폭 안에서 단어 단위로 줄바꿈하고, 줄 높이는 내용에 맞춰 자라요. 폭을 바꾸면 다시 재요.',
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'title', title: '제목', width: 120 },
{ id: 'memo', title: '메모', width: 240, wrap: true },
],
rows: Array.from({ length: 200 }, (_, i) => ({
id: i + 1,
title: `항목 ${i + 1}`,
memo: MEMOS[i % 3] as string,
})),
rowKey: 'id',
rowHeight: 'auto',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {};
}
export const actions = {
narrow: (api: GridApi<Row>) => {
api.resizeColumn('memo', 140);
say({ ko: '메모 폭 140px: 줄이 더 자라요', en: 'Memo width 140px: rows grow taller' });
},
wide: (api: GridApi<Row>) => {
api.resizeColumn('memo', 420);
say({ ko: '메모 폭 420px: 줄이 줄어요', en: 'Memo width 420px: rows shrink' });
},
edit: (api: GridApi<Row>) => {
api.updateRows([{ id: 2, title: '항목 2', memo: MEMOS[2] as string }]);
say({ ko: '2번 줄만 다시 재요', en: 'Only row 2 is measured again' });
},
remeasure: (api: GridApi<Row>) => {
api.remeasureRows();
say({
ko: 'remeasureRows(): 보이는 줄을 다시 재요',
en: 'remeasureRows(): visible rows are measured again',
});
},
};버튼을 누르면 이 예제의 코드가 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.narrow(api)"
>좁게</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.wide(api)"
>넓게</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.edit(api)"
>2번 줄 고치기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.remeasure(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;
title: string;
memo: string;
}
const MEMOS = [
'짧은 메모',
'조금 긴 메모예요. 폭이 좁으면 두 줄로 나뉘어요.',
'아주 긴 메모는 컬럼 폭 안에서 단어 단위로 줄바꿈하고, 줄 높이는 내용에 맞춰 자라요. 폭을 바꾸면 다시 재요.',
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'title', title: '제목', width: 120 },
{ id: 'memo', title: '메모', width: 240, wrap: true },
],
rows: Array.from({ length: 200 }, (_, i) => ({
id: i + 1,
title: `항목 ${i + 1}`,
memo: MEMOS[i % 3] as string,
})),
rowKey: 'id',
rowHeight: 'auto',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {};
}
export const actions = {
narrow: (api: GridApi<Row>) => {
api.resizeColumn('memo', 140);
say({ ko: '메모 폭 140px: 줄이 더 자라요', en: 'Memo width 140px: rows grow taller' });
},
wide: (api: GridApi<Row>) => {
api.resizeColumn('memo', 420);
say({ ko: '메모 폭 420px: 줄이 줄어요', en: 'Memo width 420px: rows shrink' });
},
edit: (api: GridApi<Row>) => {
api.updateRows([{ id: 2, title: '항목 2', memo: MEMOS[2] as string }]);
say({ ko: '2번 줄만 다시 재요', en: 'Only row 2 is measured again' });
},
remeasure: (api: GridApi<Row>) => {
api.remeasureRows();
say({
ko: 'remeasureRows(): 보이는 줄을 다시 재요',
en: 'remeasureRows(): visible rows are measured again',
});
},
};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="narrow">Narrow</button>
<button type="button" id="wide">Wide</button>
<button type="button" id="edit">Edit row 2</button>
<button type="button" id="remeasure">Measure again</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('narrow').addEventListener('click', () => actions.narrow(api));
document.getElementById('wide').addEventListener('click', () => actions.wide(api));
document.getElementById('edit').addEventListener('click', () => actions.edit(api));
document.getElementById('remeasure').addEventListener('click', () => actions.remeasure(api));const MEMOS = [
'Short note',
'A slightly longer note. If the column is narrow, it splits into two lines.',
'A very long note wraps word by word inside the column width, and the row height grows to fit the content. Change the width and it is measured again.',
];
export const options = {
columns: [
{ id: 'title', title: 'Title', width: 120 },
{ id: 'memo', title: 'Memo', width: 240, wrap: true },
],
rows: Array.from({ length: 200 }, (_, i) => ({
id: i + 1,
title: `Item ${i + 1}`,
memo: MEMOS[i % 3],
})),
rowKey: 'id',
rowHeight: 'auto',
};
let say = () => { };
export function setup(_api, log) {
say = log;
return () => { };
}
export const actions = {
narrow: (api) => {
api.resizeColumn('memo', 140);
say('Memo width 140px: rows grow taller');
},
wide: (api) => {
api.resizeColumn('memo', 420);
say('Memo width 420px: rows shrink');
},
edit: (api) => {
api.updateRows([{ id: 2, title: 'Item 2', memo: MEMOS[2] }]);
say('Only row 2 is measured again');
},
remeasure: (api) => {
api.remeasureRows();
say('remeasureRows(): visible rows are measured again');
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Row height that follows the content</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="narrow">Narrow</button>
<button type="button" id="wide">Wide</button>
<button type="button" id="edit">Edit row 2</button>
<button type="button" id="remeasure">Measure again</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 MEMOS = [
'Short note',
'A slightly longer note. If the column is narrow, it splits into two lines.',
'A very long note wraps word by word inside the column width, and the row height grows to fit the content. Change the width and it is measured again.',
];
const options = {
columns: [
{ id: 'title', title: 'Title', width: 120 },
{ id: 'memo', title: 'Memo', width: 240, wrap: true },
],
rows: Array.from({ length: 200 }, (_, i) => ({
id: i + 1,
title: `Item ${i + 1}`,
memo: MEMOS[i % 3],
})),
rowKey: 'id',
rowHeight: 'auto',
};
let say = () => { };
function setup(_api, log) {
say = log;
return () => { };
}
const actions = {
narrow: (api) => {
api.resizeColumn('memo', 140);
say('Memo width 140px: rows grow taller');
},
wide: (api) => {
api.resizeColumn('memo', 420);
say('Memo width 420px: rows shrink');
},
edit: (api) => {
api.updateRows([{ id: 2, title: 'Item 2', memo: MEMOS[2] }]);
say('Only row 2 is measured again');
},
remeasure: (api) => {
api.remeasureRows();
say('remeasureRows(): visible rows are measured again');
},
};
options.ariaLabel = "Row height that follows the content";
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('narrow').addEventListener('click', () => actions.narrow(api));
document.getElementById('wide').addEventListener('click', () => actions.wide(api));
document.getElementById('edit').addEventListener('click', () => actions.edit(api));
document.getElementById('remeasure').addEventListener('click', () => actions.remeasure(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.narrow(api)}
>Narrow</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.wide(api)}
>Wide</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.edit(api)}
>Edit row 2</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.remeasure(api)}
>Measure again</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;
title: string;
memo: string;
}
const MEMOS = [
'Short note',
'A slightly longer note. If the column is narrow, it splits into two lines.',
'A very long note wraps word by word inside the column width, and the row height grows to fit the content. Change the width and it is measured again.',
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'title', title: 'Title', width: 120 },
{ id: 'memo', title: 'Memo', width: 240, wrap: true },
],
rows: Array.from({ length: 200 }, (_, i) => ({
id: i + 1,
title: `Item ${i + 1}`,
memo: MEMOS[i % 3] as string,
})),
rowKey: 'id',
rowHeight: 'auto',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {};
}
export const actions = {
narrow: (api: GridApi<Row>) => {
api.resizeColumn('memo', 140);
say('Memo width 140px: rows grow taller');
},
wide: (api: GridApi<Row>) => {
api.resizeColumn('memo', 420);
say('Memo width 420px: rows shrink');
},
edit: (api: GridApi<Row>) => {
api.updateRows([{ id: 2, title: 'Item 2', memo: MEMOS[2] as string }]);
say('Only row 2 is measured again');
},
remeasure: (api: GridApi<Row>) => {
api.remeasureRows();
say('remeasureRows(): visible rows are measured again');
},
};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.narrow(api)">Narrow</button>
<button type="button" :disabled="!api" @click="api && actions.wide(api)">Wide</button>
<button type="button" :disabled="!api" @click="api && actions.edit(api)">Edit row 2</button>
<button
type="button"
:disabled="!api"
@click="api && actions.remeasure(api)"
>Measure again</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;
title: string;
memo: string;
}
const MEMOS = [
'Short note',
'A slightly longer note. If the column is narrow, it splits into two lines.',
'A very long note wraps word by word inside the column width, and the row height grows to fit the content. Change the width and it is measured again.',
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'title', title: 'Title', width: 120 },
{ id: 'memo', title: 'Memo', width: 240, wrap: true },
],
rows: Array.from({ length: 200 }, (_, i) => ({
id: i + 1,
title: `Item ${i + 1}`,
memo: MEMOS[i % 3] as string,
})),
rowKey: 'id',
rowHeight: 'auto',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {};
}
export const actions = {
narrow: (api: GridApi<Row>) => {
api.resizeColumn('memo', 140);
say('Memo width 140px: rows grow taller');
},
wide: (api: GridApi<Row>) => {
api.resizeColumn('memo', 420);
say('Memo width 420px: rows shrink');
},
edit: (api: GridApi<Row>) => {
api.updateRows([{ id: 2, title: 'Item 2', memo: MEMOS[2] as string }]);
say('Only row 2 is measured again');
},
remeasure: (api: GridApi<Row>) => {
api.remeasureRows();
say('remeasureRows(): visible rows are measured again');
},
};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.narrow(api)"
>Narrow</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.wide(api)"
>Wide</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.edit(api)"
>Edit row 2</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.remeasure(api)"
>Measure again</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;
title: string;
memo: string;
}
const MEMOS = [
'Short note',
'A slightly longer note. If the column is narrow, it splits into two lines.',
'A very long note wraps word by word inside the column width, and the row height grows to fit the content. Change the width and it is measured again.',
];
export const options: GridOptions<Row> = {
columns: [
{ id: 'title', title: 'Title', width: 120 },
{ id: 'memo', title: 'Memo', width: 240, wrap: true },
],
rows: Array.from({ length: 200 }, (_, i) => ({
id: i + 1,
title: `Item ${i + 1}`,
memo: MEMOS[i % 3] as string,
})),
rowKey: 'id',
rowHeight: 'auto',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return () => {};
}
export const actions = {
narrow: (api: GridApi<Row>) => {
api.resizeColumn('memo', 140);
say('Memo width 140px: rows grow taller');
},
wide: (api: GridApi<Row>) => {
api.resizeColumn('memo', 420);
say('Memo width 420px: rows shrink');
},
edit: (api: GridApi<Row>) => {
api.updateRows([{ id: 2, title: 'Item 2', memo: MEMOS[2] as string }]);
say('Only row 2 is measured again');
},
remeasure: (api: GridApi<Row>) => {
api.remeasureRows();
say('remeasureRows(): visible rows are measured again');
},
};