표가 화면 폭에 딱 맞게 늘어나요Columns stretch to fill the grid width
컬럼 폭을 적어 둔 만큼만 쓰고 오른쪽이 비는 대신, 남는 폭을 컬럼들이 비율대로 나눠 가져 표가 가로로 꽉 차요. 창 크기가 바뀌어도 그대로고, 폭을 지키고 싶은 컬럼은 fixed로 고정해요.Instead of leaving blank space on the right, columns share the leftover width by ratio so the table always fills the grid, even when the window resizes. Pin a column's width with fixed.
코드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">
<select aria-label="채우기" id="fit">
<option value="fill" selected>꽉 채우기 (기본)</option>
<option value="none">꽉 채우지 않음</option>
</select>
<select aria-label="그리드 폭" id="width">
<option value="100" selected>화면 폭 그대로 (100%)</option>
<option value="75">75%</option>
<option value="50">50%</option>
</select>
</div>
<div id="grid" style="height: 380px"></div>
</div>
<script type="module" src="./main.js"></script>import { createGrid } from 'maxgrid';
import { actions, options } from './example';
const api = createGrid(document.getElementById('grid'), options);
document.getElementById('fit').addEventListener('change', (event) => actions.fit(api, event.target.value));
document.getElementById('width').addEventListener('change', (event) => actions.width(api, event.target.value));const regions = ['서울', '경기', '부산', '대구', '광주'];
const products = [
'프리미엄 원두 1kg',
'드립백 세트',
'텀블러 500ml',
'핸드밀 그라인더',
'시즈널 블렌드',
];
// columnFit: 'fill'(기본값)이면 컬럼 폭의 합이 그리드보다 작을 때 남는 폭을 폭 비율대로 나눠 가져서
// 오른쪽에 빈 공간 없이 꽉 차요. 'none'으로 바꾸면 정의한 폭 그대로 두고 오른쪽을 비워 둬요.
export const options = {
columns: [
// fixed: true인 컬럼은 남는 폭을 받지 않고 정한 폭을 지켜요.
{ id: 'id', title: 'No.', width: 50, align: 'end', fixed: true },
{ id: 'month', title: '월', width: 60 },
{ id: 'region', title: '지역', width: 70 },
{ id: 'product', title: '상품', width: 130 },
{ id: 'quantity', title: '수량', kind: 'number', width: 60, align: 'end' },
{ id: 'amount', title: '금액(원)', kind: 'number', width: 90, align: 'end' },
],
rows: Array.from({ length: 14 }, (_, i) => ({
id: i + 1,
month: `${1 + (i % 12)}월`,
region: regions[i % 5],
product: products[(i * 3) % 5],
quantity: 8 + ((i * 17) % 60),
amount: 120000 + ((i * 183000) % 1800000),
})),
rowKey: 'id',
columnFit: 'fill',
};
// 적어 둔 폭의 합(460px)과 세로 스크롤바 폭보다 그리드가 좁아지면 줄어들지는 않고 가로 스크롤이 생겨요.
// 그리드가 놓인 칸(createGrid에 넘긴 요소)의 폭을 바꿔서 "창 크기가 달라져도 꽉 찬다"를 보여 줘요.
const host = () => document.querySelector('.mxg')?.parentElement ?? null;
export const actions = {
fit: (api, value) => api.setOptions({ columnFit: value === 'none' ? 'none' : 'fill' }),
width: (_api, value) => {
const el = host();
if (el) {
el.style.maxWidth = '100%';
el.style.width = `${value ?? 100}%`;
}
},
};<!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">
<select aria-label="채우기" id="fit">
<option value="fill" selected>꽉 채우기 (기본)</option>
<option value="none">꽉 채우지 않음</option>
</select>
<select aria-label="그리드 폭" id="width">
<option value="100" selected>화면 폭 그대로 (100%)</option>
<option value="75">75%</option>
<option value="50">50%</option>
</select>
</div>
<div id="grid" style="height: 380px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const regions = ['서울', '경기', '부산', '대구', '광주'];
const products = [
'프리미엄 원두 1kg',
'드립백 세트',
'텀블러 500ml',
'핸드밀 그라인더',
'시즈널 블렌드',
];
// columnFit: 'fill'(기본값)이면 컬럼 폭의 합이 그리드보다 작을 때 남는 폭을 폭 비율대로 나눠 가져서
// 오른쪽에 빈 공간 없이 꽉 차요. 'none'으로 바꾸면 정의한 폭 그대로 두고 오른쪽을 비워 둬요.
const options = {
columns: [
// fixed: true인 컬럼은 남는 폭을 받지 않고 정한 폭을 지켜요.
{ id: 'id', title: 'No.', width: 50, align: 'end', fixed: true },
{ id: 'month', title: '월', width: 60 },
{ id: 'region', title: '지역', width: 70 },
{ id: 'product', title: '상품', width: 130 },
{ id: 'quantity', title: '수량', kind: 'number', width: 60, align: 'end' },
{ id: 'amount', title: '금액(원)', kind: 'number', width: 90, align: 'end' },
],
rows: Array.from({ length: 14 }, (_, i) => ({
id: i + 1,
month: `${1 + (i % 12)}월`,
region: regions[i % 5],
product: products[(i * 3) % 5],
quantity: 8 + ((i * 17) % 60),
amount: 120000 + ((i * 183000) % 1800000),
})),
rowKey: 'id',
columnFit: 'fill',
};
// 적어 둔 폭의 합(460px)과 세로 스크롤바 폭보다 그리드가 좁아지면 줄어들지는 않고 가로 스크롤이 생겨요.
// 그리드가 놓인 칸(createGrid에 넘긴 요소)의 폭을 바꿔서 "창 크기가 달라져도 꽉 찬다"를 보여 줘요.
const host = () => document.querySelector('.mxg')?.parentElement ?? null;
const actions = {
fit: (api, value) => api.setOptions({ columnFit: value === 'none' ? 'none' : 'fill' }),
width: (_api, value) => {
const el = host();
if (el) {
el.style.maxWidth = '100%';
el.style.width = `${value ?? 100}%`;
}
},
};
options.ariaLabel = "표가 화면 폭에 딱 맞게 늘어나요";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
document.getElementById('fit').addEventListener('change', (event) => actions.fit(api, event.target.value));
document.getElementById('width').addEventListener('change', (event) => actions.width(api, event.target.value));
</script>
</body>
</html>버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
return (
<>
<div className="toolbar">
<select
aria-label="채우기"
disabled={!api}
onChange={(event) => api && actions.fit(api, event.target.value)}
>
<option value="fill">꽉 채우기 (기본)</option>
<option value="none">꽉 채우지 않음</option>
</select>
<select defaultValue="fill"
aria-label="그리드 폭"
disabled={!api}
onChange={(event) => api && actions.width(api, event.target.value)}
>
<option value="100">화면 폭 그대로 (100%)</option>
<option value="75">75%</option>
<option value="50">50%</option>
</select>
</div>
<Grid style={{ height: 380 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
month: string;
region: string;
product: string;
quantity: number;
amount: number;
}
const regions = ['서울', '경기', '부산', '대구', '광주'];
const products = [
'프리미엄 원두 1kg',
'드립백 세트',
'텀블러 500ml',
'핸드밀 그라인더',
'시즈널 블렌드',
];
// columnFit: 'fill'(기본값)이면 컬럼 폭의 합이 그리드보다 작을 때 남는 폭을 폭 비율대로 나눠 가져서
// 오른쪽에 빈 공간 없이 꽉 차요. 'none'으로 바꾸면 정의한 폭 그대로 두고 오른쪽을 비워 둬요.
export const options: GridOptions<Row> = {
columns: [
// fixed: true인 컬럼은 남는 폭을 받지 않고 정한 폭을 지켜요.
{ id: 'id', title: 'No.', width: 50, align: 'end', fixed: true },
{ id: 'month', title: '월', width: 60 },
{ id: 'region', title: '지역', width: 70 },
{ id: 'product', title: '상품', width: 130 },
{ id: 'quantity', title: '수량', kind: 'number', width: 60, align: 'end' },
{ id: 'amount', title: '금액(원)', kind: 'number', width: 90, align: 'end' },
],
rows: Array.from({ length: 14 }, (_, i) => ({
id: i + 1,
month: `${1 + (i % 12)}월`,
region: regions[i % 5] as string,
product: products[(i * 3) % 5] as string,
quantity: 8 + ((i * 17) % 60),
amount: 120000 + ((i * 183000) % 1800000),
})),
rowKey: 'id',
columnFit: 'fill',
};
// 적어 둔 폭의 합(460px)과 세로 스크롤바 폭보다 그리드가 좁아지면 줄어들지는 않고 가로 스크롤이 생겨요.
// 그리드가 놓인 칸(createGrid에 넘긴 요소)의 폭을 바꿔서 "창 크기가 달라져도 꽉 찬다"를 보여 줘요.
const host = (): HTMLElement | null =>
document.querySelector<HTMLElement>('.mxg')?.parentElement ?? null;
export const actions = {
fit: (api: GridApi<Row>, value?: string) =>
api.setOptions({ columnFit: value === 'none' ? 'none' : 'fill' }),
width: (_api: GridApi<Row>, value?: string) => {
const el = host();
if (el) {
el.style.maxWidth = '100%';
el.style.width = `${value ?? 100}%`;
}
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<script setup lang="ts">
import { shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
}
</script>
<template>
<div class="toolbar">
<select
aria-label="채우기"
:disabled="!api"
@change="api && actions.fit(api, ($event.target as HTMLSelectElement).value)"
>
<option value="fill" selected>꽉 채우기 (기본)</option>
<option value="none">꽉 채우지 않음</option>
</select>
<select
aria-label="그리드 폭"
:disabled="!api"
@change="api && actions.width(api, ($event.target as HTMLSelectElement).value)"
>
<option value="100" selected>화면 폭 그대로 (100%)</option>
<option value="75">75%</option>
<option value="50">50%</option>
</select>
</div>
<Grid style="height: 380px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
month: string;
region: string;
product: string;
quantity: number;
amount: number;
}
const regions = ['서울', '경기', '부산', '대구', '광주'];
const products = [
'프리미엄 원두 1kg',
'드립백 세트',
'텀블러 500ml',
'핸드밀 그라인더',
'시즈널 블렌드',
];
// columnFit: 'fill'(기본값)이면 컬럼 폭의 합이 그리드보다 작을 때 남는 폭을 폭 비율대로 나눠 가져서
// 오른쪽에 빈 공간 없이 꽉 차요. 'none'으로 바꾸면 정의한 폭 그대로 두고 오른쪽을 비워 둬요.
export const options: GridOptions<Row> = {
columns: [
// fixed: true인 컬럼은 남는 폭을 받지 않고 정한 폭을 지켜요.
{ id: 'id', title: 'No.', width: 50, align: 'end', fixed: true },
{ id: 'month', title: '월', width: 60 },
{ id: 'region', title: '지역', width: 70 },
{ id: 'product', title: '상품', width: 130 },
{ id: 'quantity', title: '수량', kind: 'number', width: 60, align: 'end' },
{ id: 'amount', title: '금액(원)', kind: 'number', width: 90, align: 'end' },
],
rows: Array.from({ length: 14 }, (_, i) => ({
id: i + 1,
month: `${1 + (i % 12)}월`,
region: regions[i % 5] as string,
product: products[(i * 3) % 5] as string,
quantity: 8 + ((i * 17) % 60),
amount: 120000 + ((i * 183000) % 1800000),
})),
rowKey: 'id',
columnFit: 'fill',
};
// 적어 둔 폭의 합(460px)과 세로 스크롤바 폭보다 그리드가 좁아지면 줄어들지는 않고 가로 스크롤이 생겨요.
// 그리드가 놓인 칸(createGrid에 넘긴 요소)의 폭을 바꿔서 "창 크기가 달라져도 꽉 찬다"를 보여 줘요.
const host = (): HTMLElement | null =>
document.querySelector<HTMLElement>('.mxg')?.parentElement ?? null;
export const actions = {
fit: (api: GridApi<Row>, value?: string) =>
api.setOptions({ columnFit: value === 'none' ? 'none' : 'fill' }),
width: (_api: GridApi<Row>, value?: string) => {
const el = host();
if (el) {
el.style.maxWidth = '100%';
el.style.width = `${value ?? 100}%`;
}
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Component } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<select
aria-label="채우기"
[disabled]="!api"
(change)="api && actions.fit(api, $any($event.target).value)"
>
<option value="fill" selected>꽉 채우기 (기본)</option>
<option value="none">꽉 채우지 않음</option>
</select>
<select
aria-label="그리드 폭"
[disabled]="!api"
(change)="api && actions.width(api, $any($event.target).value)"
>
<option value="100" selected>화면 폭 그대로 (100%)</option>
<option value="75">75%</option>
<option value="50">50%</option>
</select>
</div>
<mx-grid style="height: 380px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
}
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export interface Row {
id: number;
month: string;
region: string;
product: string;
quantity: number;
amount: number;
}
const regions = ['서울', '경기', '부산', '대구', '광주'];
const products = [
'프리미엄 원두 1kg',
'드립백 세트',
'텀블러 500ml',
'핸드밀 그라인더',
'시즈널 블렌드',
];
// columnFit: 'fill'(기본값)이면 컬럼 폭의 합이 그리드보다 작을 때 남는 폭을 폭 비율대로 나눠 가져서
// 오른쪽에 빈 공간 없이 꽉 차요. 'none'으로 바꾸면 정의한 폭 그대로 두고 오른쪽을 비워 둬요.
export const options: GridOptions<Row> = {
columns: [
// fixed: true인 컬럼은 남는 폭을 받지 않고 정한 폭을 지켜요.
{ id: 'id', title: 'No.', width: 50, align: 'end', fixed: true },
{ id: 'month', title: '월', width: 60 },
{ id: 'region', title: '지역', width: 70 },
{ id: 'product', title: '상품', width: 130 },
{ id: 'quantity', title: '수량', kind: 'number', width: 60, align: 'end' },
{ id: 'amount', title: '금액(원)', kind: 'number', width: 90, align: 'end' },
],
rows: Array.from({ length: 14 }, (_, i) => ({
id: i + 1,
month: `${1 + (i % 12)}월`,
region: regions[i % 5] as string,
product: products[(i * 3) % 5] as string,
quantity: 8 + ((i * 17) % 60),
amount: 120000 + ((i * 183000) % 1800000),
})),
rowKey: 'id',
columnFit: 'fill',
};
// 적어 둔 폭의 합(460px)과 세로 스크롤바 폭보다 그리드가 좁아지면 줄어들지는 않고 가로 스크롤이 생겨요.
// 그리드가 놓인 칸(createGrid에 넘긴 요소)의 폭을 바꿔서 "창 크기가 달라져도 꽉 찬다"를 보여 줘요.
const host = (): HTMLElement | null =>
document.querySelector<HTMLElement>('.mxg')?.parentElement ?? null;
export const actions = {
fit: (api: GridApi<Row>, value?: string) =>
api.setOptions({ columnFit: value === 'none' ? 'none' : 'fill' }),
width: (_api: GridApi<Row>, value?: string) => {
const el = host();
if (el) {
el.style.maxWidth = '100%';
el.style.width = `${value ?? 100}%`;
}
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<div id="app">
<div class="toolbar">
<select aria-label="Fill" id="fit">
<option value="fill" selected>Fill the width (default)</option>
<option value="none">Off</option>
</select>
<select aria-label="Grid width" id="width">
<option value="100" selected>Full width (100%)</option>
<option value="75">75%</option>
<option value="50">50%</option>
</select>
</div>
<div id="grid" style="height: 380px"></div>
</div>
<script type="module" src="./main.js"></script>import { createGrid } from 'maxgrid';
import { actions, options } from './example';
const api = createGrid(document.getElementById('grid'), options);
document.getElementById('fit').addEventListener('change', (event) => actions.fit(api, event.target.value));
document.getElementById('width').addEventListener('change', (event) => actions.width(api, event.target.value));const regions = ['Seoul', 'Incheon', 'Busan', 'Daegu', 'Ulsan'];
const products = [
'Premium beans 1kg',
'Drip bag set',
'Tumbler 500ml',
'Hand grinder',
'Seasonal blend',
];
// With columnFit: 'fill' (the default), when the column widths add up to less than the grid, the spare width is shared in proportion to the widths
// so the grid fills to the right edge with no empty space. With 'none', the defined widths stay as they are and the right side is left empty.
export const options = {
columns: [
// A column with fixed: true gets no spare width and keeps its set width.
{ id: 'id', title: 'No.', width: 50, align: 'end', fixed: true },
{ id: 'month', title: 'Month', width: 60 },
{ id: 'region', title: 'Region', width: 70 },
{ id: 'product', title: 'Product', width: 130 },
{ id: 'quantity', title: 'Qty', kind: 'number', width: 60, align: 'end' },
{ id: 'amount', title: 'Amount', kind: 'number', width: 90, align: 'end' },
],
rows: Array.from({ length: 14 }, (_, i) => ({
id: i + 1,
month: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][i % 12],
region: regions[i % 5],
product: products[(i * 3) % 5],
quantity: 8 + ((i * 17) % 60),
amount: 120 + ((i * 183) % 1800),
})),
rowKey: 'id',
columnFit: 'fill',
};
// If the grid gets narrower than the sum of the set widths (460px) plus the vertical scrollbar width, columns do not shrink; a horizontal scrollbar appears.
// Changing the width of the element holding the grid (the one passed to createGrid) shows that "it still fills when the window size changes".
const host = () => document.querySelector('.mxg')?.parentElement ?? null;
export const actions = {
fit: (api, value) => api.setOptions({ columnFit: value === 'none' ? 'none' : 'fill' }),
width: (_api, value) => {
const el = host();
if (el) {
el.style.maxWidth = '100%';
el.style.width = `${value ?? 100}%`;
}
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Columns stretch to fill the grid width</title>
</head>
<body>
<div class="toolbar">
<select aria-label="Fill" id="fit">
<option value="fill" selected>Fill the width (default)</option>
<option value="none">Off</option>
</select>
<select aria-label="Grid width" id="width">
<option value="100" selected>Full width (100%)</option>
<option value="75">75%</option>
<option value="50">50%</option>
</select>
</div>
<div id="grid" style="height: 380px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const regions = ['Seoul', 'Incheon', 'Busan', 'Daegu', 'Ulsan'];
const products = [
'Premium beans 1kg',
'Drip bag set',
'Tumbler 500ml',
'Hand grinder',
'Seasonal blend',
];
// With columnFit: 'fill' (the default), when the column widths add up to less than the grid, the spare width is shared in proportion to the widths
// so the grid fills to the right edge with no empty space. With 'none', the defined widths stay as they are and the right side is left empty.
const options = {
columns: [
// A column with fixed: true gets no spare width and keeps its set width.
{ id: 'id', title: 'No.', width: 50, align: 'end', fixed: true },
{ id: 'month', title: 'Month', width: 60 },
{ id: 'region', title: 'Region', width: 70 },
{ id: 'product', title: 'Product', width: 130 },
{ id: 'quantity', title: 'Qty', kind: 'number', width: 60, align: 'end' },
{ id: 'amount', title: 'Amount', kind: 'number', width: 90, align: 'end' },
],
rows: Array.from({ length: 14 }, (_, i) => ({
id: i + 1,
month: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][i % 12],
region: regions[i % 5],
product: products[(i * 3) % 5],
quantity: 8 + ((i * 17) % 60),
amount: 120 + ((i * 183) % 1800),
})),
rowKey: 'id',
columnFit: 'fill',
};
// If the grid gets narrower than the sum of the set widths (460px) plus the vertical scrollbar width, columns do not shrink; a horizontal scrollbar appears.
// Changing the width of the element holding the grid (the one passed to createGrid) shows that "it still fills when the window size changes".
const host = () => document.querySelector('.mxg')?.parentElement ?? null;
const actions = {
fit: (api, value) => api.setOptions({ columnFit: value === 'none' ? 'none' : 'fill' }),
width: (_api, value) => {
const el = host();
if (el) {
el.style.maxWidth = '100%';
el.style.width = `${value ?? 100}%`;
}
},
};
options.ariaLabel = "Columns stretch to fill the grid width";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
document.getElementById('fit').addEventListener('change', (event) => actions.fit(api, event.target.value));
document.getElementById('width').addEventListener('change', (event) => actions.width(api, event.target.value));
</script>
</body>
</html>When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
return (
<>
<div className="toolbar">
<select
aria-label="Fill"
disabled={!api}
onChange={(event) => api && actions.fit(api, event.target.value)}
>
<option value="fill">Fill the width (default)</option>
<option value="none">Off</option>
</select>
<select defaultValue="fill"
aria-label="Grid width"
disabled={!api}
onChange={(event) => api && actions.width(api, event.target.value)}
>
<option value="100">Full width (100%)</option>
<option value="75">75%</option>
<option value="50">50%</option>
</select>
</div>
<Grid style={{ height: 380 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
month: string;
region: string;
product: string;
quantity: number;
amount: number;
}
const regions = ['Seoul', 'Incheon', 'Busan', 'Daegu', 'Ulsan'];
const products = [
'Premium beans 1kg',
'Drip bag set',
'Tumbler 500ml',
'Hand grinder',
'Seasonal blend',
];
// With columnFit: 'fill' (the default), when the column widths add up to less than the grid, the spare width is shared in proportion to the widths
// so the grid fills to the right edge with no empty space. With 'none', the defined widths stay as they are and the right side is left empty.
export const options: GridOptions<Row> = {
columns: [
// A column with fixed: true gets no spare width and keeps its set width.
{ id: 'id', title: 'No.', width: 50, align: 'end', fixed: true },
{ id: 'month', title: 'Month', width: 60 },
{ id: 'region', title: 'Region', width: 70 },
{ id: 'product', title: 'Product', width: 130 },
{ id: 'quantity', title: 'Qty', kind: 'number', width: 60, align: 'end' },
{ id: 'amount', title: 'Amount', kind: 'number', width: 90, align: 'end' },
],
rows: Array.from({ length: 14 }, (_, i) => ({
id: i + 1,
month: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][
i % 12
] as string,
region: regions[i % 5] as string,
product: products[(i * 3) % 5] as string,
quantity: 8 + ((i * 17) % 60),
amount: 120 + ((i * 183) % 1800),
})),
rowKey: 'id',
columnFit: 'fill',
};
// If the grid gets narrower than the sum of the set widths (460px) plus the vertical scrollbar width, columns do not shrink; a horizontal scrollbar appears.
// Changing the width of the element holding the grid (the one passed to createGrid) shows that "it still fills when the window size changes".
const host = (): HTMLElement | null =>
document.querySelector<HTMLElement>('.mxg')?.parentElement ?? null;
export const actions = {
fit: (api: GridApi<Row>, value?: string) =>
api.setOptions({ columnFit: value === 'none' ? 'none' : 'fill' }),
width: (_api: GridApi<Row>, value?: string) => {
const el = host();
if (el) {
el.style.maxWidth = '100%';
el.style.width = `${value ?? 100}%`;
}
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<script setup lang="ts">
import { shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
}
</script>
<template>
<div class="toolbar">
<select
aria-label="Fill"
:disabled="!api"
@change="api && actions.fit(api, ($event.target as HTMLSelectElement).value)"
>
<option value="fill" selected>Fill the width (default)</option>
<option value="none">Off</option>
</select>
<select
aria-label="Grid width"
:disabled="!api"
@change="api && actions.width(api, ($event.target as HTMLSelectElement).value)"
>
<option value="100" selected>Full width (100%)</option>
<option value="75">75%</option>
<option value="50">50%</option>
</select>
</div>
<Grid style="height: 380px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
month: string;
region: string;
product: string;
quantity: number;
amount: number;
}
const regions = ['Seoul', 'Incheon', 'Busan', 'Daegu', 'Ulsan'];
const products = [
'Premium beans 1kg',
'Drip bag set',
'Tumbler 500ml',
'Hand grinder',
'Seasonal blend',
];
// With columnFit: 'fill' (the default), when the column widths add up to less than the grid, the spare width is shared in proportion to the widths
// so the grid fills to the right edge with no empty space. With 'none', the defined widths stay as they are and the right side is left empty.
export const options: GridOptions<Row> = {
columns: [
// A column with fixed: true gets no spare width and keeps its set width.
{ id: 'id', title: 'No.', width: 50, align: 'end', fixed: true },
{ id: 'month', title: 'Month', width: 60 },
{ id: 'region', title: 'Region', width: 70 },
{ id: 'product', title: 'Product', width: 130 },
{ id: 'quantity', title: 'Qty', kind: 'number', width: 60, align: 'end' },
{ id: 'amount', title: 'Amount', kind: 'number', width: 90, align: 'end' },
],
rows: Array.from({ length: 14 }, (_, i) => ({
id: i + 1,
month: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][
i % 12
] as string,
region: regions[i % 5] as string,
product: products[(i * 3) % 5] as string,
quantity: 8 + ((i * 17) % 60),
amount: 120 + ((i * 183) % 1800),
})),
rowKey: 'id',
columnFit: 'fill',
};
// If the grid gets narrower than the sum of the set widths (460px) plus the vertical scrollbar width, columns do not shrink; a horizontal scrollbar appears.
// Changing the width of the element holding the grid (the one passed to createGrid) shows that "it still fills when the window size changes".
const host = (): HTMLElement | null =>
document.querySelector<HTMLElement>('.mxg')?.parentElement ?? null;
export const actions = {
fit: (api: GridApi<Row>, value?: string) =>
api.setOptions({ columnFit: value === 'none' ? 'none' : 'fill' }),
width: (_api: GridApi<Row>, value?: string) => {
const el = host();
if (el) {
el.style.maxWidth = '100%';
el.style.width = `${value ?? 100}%`;
}
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Component } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<select
aria-label="Fill"
[disabled]="!api"
(change)="api && actions.fit(api, $any($event.target).value)"
>
<option value="fill" selected>Fill the width (default)</option>
<option value="none">Off</option>
</select>
<select
aria-label="Grid width"
[disabled]="!api"
(change)="api && actions.width(api, $any($event.target).value)"
>
<option value="100" selected>Full width (100%)</option>
<option value="75">75%</option>
<option value="50">50%</option>
</select>
</div>
<mx-grid style="height: 380px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
}
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export interface Row {
id: number;
month: string;
region: string;
product: string;
quantity: number;
amount: number;
}
const regions = ['Seoul', 'Incheon', 'Busan', 'Daegu', 'Ulsan'];
const products = [
'Premium beans 1kg',
'Drip bag set',
'Tumbler 500ml',
'Hand grinder',
'Seasonal blend',
];
// With columnFit: 'fill' (the default), when the column widths add up to less than the grid, the spare width is shared in proportion to the widths
// so the grid fills to the right edge with no empty space. With 'none', the defined widths stay as they are and the right side is left empty.
export const options: GridOptions<Row> = {
columns: [
// A column with fixed: true gets no spare width and keeps its set width.
{ id: 'id', title: 'No.', width: 50, align: 'end', fixed: true },
{ id: 'month', title: 'Month', width: 60 },
{ id: 'region', title: 'Region', width: 70 },
{ id: 'product', title: 'Product', width: 130 },
{ id: 'quantity', title: 'Qty', kind: 'number', width: 60, align: 'end' },
{ id: 'amount', title: 'Amount', kind: 'number', width: 90, align: 'end' },
],
rows: Array.from({ length: 14 }, (_, i) => ({
id: i + 1,
month: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][
i % 12
] as string,
region: regions[i % 5] as string,
product: products[(i * 3) % 5] as string,
quantity: 8 + ((i * 17) % 60),
amount: 120 + ((i * 183) % 1800),
})),
rowKey: 'id',
columnFit: 'fill',
};
// If the grid gets narrower than the sum of the set widths (460px) plus the vertical scrollbar width, columns do not shrink; a horizontal scrollbar appears.
// Changing the width of the element holding the grid (the one passed to createGrid) shows that "it still fills when the window size changes".
const host = (): HTMLElement | null =>
document.querySelector<HTMLElement>('.mxg')?.parentElement ?? null;
export const actions = {
fit: (api: GridApi<Row>, value?: string) =>
api.setOptions({ columnFit: value === 'none' ? 'none' : 'fill' }),
width: (_api: GridApi<Row>, value?: string) => {
const el = host();
if (el) {
el.style.maxWidth = '100%';
el.style.width = `${value ?? 100}%`;
}
},
};