좁아지면 일부 컬럼 숨기기Hide some columns when the grid gets narrow
responsiveColumns를 켜고 컬럼에 hideBelow(px)를 주면, 그리드가 담긴 상자가 그 폭보다 좁아질 때 그 컬럼이 숨고 다시 넓어지면 돌아와요. 사용자가 숨긴 컬럼은 그대로 숨어 있고, 폭 때문에 숨은 컬럼은 저장본에 남지 않아요.Turn on responsiveColumns and give columns hideBelow (px). When the box that holds the grid gets narrower than that, the column hides, and it returns when there is room again. Columns the user hid stay hidden, and columns hidden by width are not written to the saved state.
코드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="width">
<option value="full" selected>가득 (100%)</option>
<option value="900">900px</option>
<option value="700">700px</option>
<option value="550">550px</option>
<option value="400">400px</option>
</select>
<button type="button" id="hideTeam">팀 숨기기</button>
<button type="button" id="showTeam">팀 보이기</button>
<button type="button" id="state">저장본 보기</button>
</div>
<div id="grid" style="height: 330px"></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('width').addEventListener('change', (event) => actions.width(api, event.target.value));
document.getElementById('hideTeam').addEventListener('click', () => actions.hideTeam(api));
document.getElementById('showTeam').addEventListener('click', () => actions.showTeam(api));
document.getElementById('state').addEventListener('click', () => actions.state(api));const names = ['김서연', '이준호', '박민지', '최도윤', '정하은', '강지후', '윤서아', '임태민'];
const teams = ['개발', '디자인', '영업', '인사'];
// responsiveColumns를 켜면 hideBelow가 있는 컬럼은 그리드 상자가 그 폭(px)보다 좁을 때 숨어요.
// 사용자가 숨긴 컬럼은 계속 숨겨져 있고, 폭 때문에 숨은 컬럼은 getColumnState()에 남지 않아요.
export const options = {
columns: [
{ id: 'id', title: '번호', width: 70, align: 'end' },
{ id: 'name', title: '이름', width: 120 },
{ id: 'team', title: '팀', width: 110, hideBelow: 620 },
{ id: 'email', title: '이메일', width: 210, hideBelow: 780 },
{ id: 'joined', title: '입사일', width: 120, hideBelow: 920 },
],
rows: Array.from({ length: 12 }, (_, i) => ({
id: i + 1,
name: names[i % names.length],
team: teams[(i * 3) % teams.length],
email: `user${i + 1}@example.com`,
joined: `2023-${String(1 + (i % 12)).padStart(2, '0')}-${String(3 + i).padStart(2, '0')}`,
})),
rowKey: 'id',
responsiveColumns: { debounce: 60 },
};
let say = () => { };
function box() {
return document.querySelector('.mxg')?.parentElement ?? null;
}
function resizeTo(width) {
const el = box();
if (!el)
return;
el.style.maxWidth = '100%';
el.style.width = width === 'full' ? '100%' : `${width}px`;
}
const list = (ids) => (ids.length === 0 ? '(없음)' : ids.join(', '));
const listEn = (ids) => (ids.length === 0 ? '(none)' : ids.join(', '));
export function setup(api, log) {
say = log;
resizeTo('full');
log({
ko: '폭을 바꾸면 폭 때문에 숨은 컬럼이 로그에 나와요.',
en: 'When the width changes, the columns hidden by width are logged.',
});
const off = api.on('columnsResponsiveChanged', ({ hidden }) => log({ ko: `폭 때문에 숨은 컬럼: ${list(hidden)}`, en: `Hidden by width: ${listEn(hidden)}` }));
return () => off();
}
export const actions = {
// 그리드가 담긴 상자의 폭을 바꿔요. 그리드는 상자 폭을 따라가요.
width: (_api, value) => resizeTo(value ?? 'full'),
// 사용자가 숨긴 컬럼은 폭이 넓어져도 돌아오지 않아요.
hideTeam: (api) => {
api.setColumnVisible('team', false);
say({
ko: `팀을 숨겼어요. 폭 때문에 숨은 컬럼: ${list(api.getResponsiveHiddenColumns())}`,
en: `Team hidden by the user. Hidden by width: ${listEn(api.getResponsiveHiddenColumns())}`,
});
},
showTeam: (api) => api.setColumnVisible('team', true),
// 저장본에는 사용자가 숨긴 것만 남아요.
state: (api) => say({
ko: `저장본에서 숨김: ${list(api
.getColumnState()
.filter((s) => s.hidden)
.map((s) => s.id))}`,
en: `Hidden in the saved state: ${listEn(api
.getColumnState()
.filter((s) => s.hidden)
.map((s) => s.id))}`,
}),
};<!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="width">
<option value="full" selected>가득 (100%)</option>
<option value="900">900px</option>
<option value="700">700px</option>
<option value="550">550px</option>
<option value="400">400px</option>
</select>
<button type="button" id="hideTeam">팀 숨기기</button>
<button type="button" id="showTeam">팀 보이기</button>
<button type="button" id="state">저장본 보기</button>
</div>
<div id="grid" style="height: 330px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const names = ['김서연', '이준호', '박민지', '최도윤', '정하은', '강지후', '윤서아', '임태민'];
const teams = ['개발', '디자인', '영업', '인사'];
// responsiveColumns를 켜면 hideBelow가 있는 컬럼은 그리드 상자가 그 폭(px)보다 좁을 때 숨어요.
// 사용자가 숨긴 컬럼은 계속 숨겨져 있고, 폭 때문에 숨은 컬럼은 getColumnState()에 남지 않아요.
const options = {
columns: [
{ id: 'id', title: '번호', width: 70, align: 'end' },
{ id: 'name', title: '이름', width: 120 },
{ id: 'team', title: '팀', width: 110, hideBelow: 620 },
{ id: 'email', title: '이메일', width: 210, hideBelow: 780 },
{ id: 'joined', title: '입사일', width: 120, hideBelow: 920 },
],
rows: Array.from({ length: 12 }, (_, i) => ({
id: i + 1,
name: names[i % names.length],
team: teams[(i * 3) % teams.length],
email: `user${i + 1}@example.com`,
joined: `2023-${String(1 + (i % 12)).padStart(2, '0')}-${String(3 + i).padStart(2, '0')}`,
})),
rowKey: 'id',
responsiveColumns: { debounce: 60 },
};
let say = () => { };
function box() {
return document.querySelector('.mxg')?.parentElement ?? null;
}
function resizeTo(width) {
const el = box();
if (!el)
return;
el.style.maxWidth = '100%';
el.style.width = width === 'full' ? '100%' : `${width}px`;
}
const list = (ids) => (ids.length === 0 ? '(없음)' : ids.join(', '));
const listEn = (ids) => (ids.length === 0 ? '(none)' : ids.join(', '));
function setup(api, log) {
say = log;
resizeTo('full');
log({
ko: '폭을 바꾸면 폭 때문에 숨은 컬럼이 로그에 나와요.',
en: 'When the width changes, the columns hidden by width are logged.',
});
const off = api.on('columnsResponsiveChanged', ({ hidden }) => log({ ko: `폭 때문에 숨은 컬럼: ${list(hidden)}`, en: `Hidden by width: ${listEn(hidden)}` }));
return () => off();
}
const actions = {
// 그리드가 담긴 상자의 폭을 바꿔요. 그리드는 상자 폭을 따라가요.
width: (_api, value) => resizeTo(value ?? 'full'),
// 사용자가 숨긴 컬럼은 폭이 넓어져도 돌아오지 않아요.
hideTeam: (api) => {
api.setColumnVisible('team', false);
say({
ko: `팀을 숨겼어요. 폭 때문에 숨은 컬럼: ${list(api.getResponsiveHiddenColumns())}`,
en: `Team hidden by the user. Hidden by width: ${listEn(api.getResponsiveHiddenColumns())}`,
});
},
showTeam: (api) => api.setColumnVisible('team', true),
// 저장본에는 사용자가 숨긴 것만 남아요.
state: (api) => say({
ko: `저장본에서 숨김: ${list(api
.getColumnState()
.filter((s) => s.hidden)
.map((s) => s.id))}`,
en: `Hidden in the saved state: ${listEn(api
.getColumnState()
.filter((s) => s.hidden)
.map((s) => s.id))}`,
}),
};
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('width').addEventListener('change', (event) => actions.width(api, event.target.value));
document.getElementById('hideTeam').addEventListener('click', () => actions.hideTeam(api));
document.getElementById('showTeam').addEventListener('click', () => actions.showTeam(api));
document.getElementById('state').addEventListener('click', () => actions.state(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">
<select
aria-label="그리드 폭"
disabled={!api}
onChange={(event) => api && actions.width(api, event.target.value)}
>
<option value="full">가득 (100%)</option>
<option value="900">900px</option>
<option value="700">700px</option>
<option value="550">550px</option>
<option value="400">400px</option>
</select>
<button
type="button"
disabled={!api}
onClick={() => api && actions.hideTeam(api)}
>팀 숨기기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.showTeam(api)}
>팀 보이기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.state(api)}
>저장본 보기</button>
</div>
<Grid style={{ height: 330 }} 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;
team: string;
email: string;
joined: string;
}
const names = ['김서연', '이준호', '박민지', '최도윤', '정하은', '강지후', '윤서아', '임태민'];
const teams = ['개발', '디자인', '영업', '인사'];
// responsiveColumns를 켜면 hideBelow가 있는 컬럼은 그리드 상자가 그 폭(px)보다 좁을 때 숨어요.
// 사용자가 숨긴 컬럼은 계속 숨겨져 있고, 폭 때문에 숨은 컬럼은 getColumnState()에 남지 않아요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', width: 70, align: 'end' },
{ id: 'name', title: '이름', width: 120 },
{ id: 'team', title: '팀', width: 110, hideBelow: 620 },
{ id: 'email', title: '이메일', width: 210, hideBelow: 780 },
{ id: 'joined', title: '입사일', width: 120, hideBelow: 920 },
],
rows: Array.from({ length: 12 }, (_, i) => ({
id: i + 1,
name: names[i % names.length],
team: teams[(i * 3) % teams.length],
email: `user${i + 1}@example.com`,
joined: `2023-${String(1 + (i % 12)).padStart(2, '0')}-${String(3 + i).padStart(2, '0')}`,
})),
rowKey: 'id',
responsiveColumns: { debounce: 60 },
};
let say: (message: LogText) => void = () => {};
function box(): HTMLElement | null {
return document.querySelector('.mxg')?.parentElement ?? null;
}
function resizeTo(width: string): void {
const el = box();
if (!el) return;
el.style.maxWidth = '100%';
el.style.width = width === 'full' ? '100%' : `${width}px`;
}
const list = (ids: readonly string[]) => (ids.length === 0 ? '(없음)' : ids.join(', '));
const listEn = (ids: readonly string[]) => (ids.length === 0 ? '(none)' : ids.join(', '));
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
resizeTo('full');
log({
ko: '폭을 바꾸면 폭 때문에 숨은 컬럼이 로그에 나와요.',
en: 'When the width changes, the columns hidden by width are logged.',
});
const off = api.on('columnsResponsiveChanged', ({ hidden }) =>
log({ ko: `폭 때문에 숨은 컬럼: ${list(hidden)}`, en: `Hidden by width: ${listEn(hidden)}` }),
);
return () => off();
}
export const actions = {
// 그리드가 담긴 상자의 폭을 바꿔요. 그리드는 상자 폭을 따라가요.
width: (_api: GridApi<Row>, value?: string) => resizeTo(value ?? 'full'),
// 사용자가 숨긴 컬럼은 폭이 넓어져도 돌아오지 않아요.
hideTeam: (api: GridApi<Row>) => {
api.setColumnVisible('team', false);
say({
ko: `팀을 숨겼어요. 폭 때문에 숨은 컬럼: ${list(api.getResponsiveHiddenColumns())}`,
en: `Team hidden by the user. Hidden by width: ${listEn(api.getResponsiveHiddenColumns())}`,
});
},
showTeam: (api: GridApi<Row>) => api.setColumnVisible('team', true),
// 저장본에는 사용자가 숨긴 것만 남아요.
state: (api: GridApi<Row>) =>
say({
ko: `저장본에서 숨김: ${list(
api
.getColumnState()
.filter((s) => s.hidden)
.map((s) => s.id),
)}`,
en: `Hidden in the saved state: ${listEn(
api
.getColumnState()
.filter((s) => s.hidden)
.map((s) => s.id),
)}`,
}),
};버튼을 누르면 이 예제의 코드가 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">
<select
aria-label="그리드 폭"
:disabled="!api"
@change="api && actions.width(api, ($event.target as HTMLSelectElement).value)"
>
<option value="full" selected>가득 (100%)</option>
<option value="900">900px</option>
<option value="700">700px</option>
<option value="550">550px</option>
<option value="400">400px</option>
</select>
<button
type="button"
:disabled="!api"
@click="api && actions.hideTeam(api)"
>팀 숨기기</button>
<button
type="button"
:disabled="!api"
@click="api && actions.showTeam(api)"
>팀 보이기</button>
<button type="button" :disabled="!api" @click="api && actions.state(api)">저장본 보기</button>
</div>
<Grid style="height: 330px" :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;
team: string;
email: string;
joined: string;
}
const names = ['김서연', '이준호', '박민지', '최도윤', '정하은', '강지후', '윤서아', '임태민'];
const teams = ['개발', '디자인', '영업', '인사'];
// responsiveColumns를 켜면 hideBelow가 있는 컬럼은 그리드 상자가 그 폭(px)보다 좁을 때 숨어요.
// 사용자가 숨긴 컬럼은 계속 숨겨져 있고, 폭 때문에 숨은 컬럼은 getColumnState()에 남지 않아요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', width: 70, align: 'end' },
{ id: 'name', title: '이름', width: 120 },
{ id: 'team', title: '팀', width: 110, hideBelow: 620 },
{ id: 'email', title: '이메일', width: 210, hideBelow: 780 },
{ id: 'joined', title: '입사일', width: 120, hideBelow: 920 },
],
rows: Array.from({ length: 12 }, (_, i) => ({
id: i + 1,
name: names[i % names.length],
team: teams[(i * 3) % teams.length],
email: `user${i + 1}@example.com`,
joined: `2023-${String(1 + (i % 12)).padStart(2, '0')}-${String(3 + i).padStart(2, '0')}`,
})),
rowKey: 'id',
responsiveColumns: { debounce: 60 },
};
let say: (message: LogText) => void = () => {};
function box(): HTMLElement | null {
return document.querySelector('.mxg')?.parentElement ?? null;
}
function resizeTo(width: string): void {
const el = box();
if (!el) return;
el.style.maxWidth = '100%';
el.style.width = width === 'full' ? '100%' : `${width}px`;
}
const list = (ids: readonly string[]) => (ids.length === 0 ? '(없음)' : ids.join(', '));
const listEn = (ids: readonly string[]) => (ids.length === 0 ? '(none)' : ids.join(', '));
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
resizeTo('full');
log({
ko: '폭을 바꾸면 폭 때문에 숨은 컬럼이 로그에 나와요.',
en: 'When the width changes, the columns hidden by width are logged.',
});
const off = api.on('columnsResponsiveChanged', ({ hidden }) =>
log({ ko: `폭 때문에 숨은 컬럼: ${list(hidden)}`, en: `Hidden by width: ${listEn(hidden)}` }),
);
return () => off();
}
export const actions = {
// 그리드가 담긴 상자의 폭을 바꿔요. 그리드는 상자 폭을 따라가요.
width: (_api: GridApi<Row>, value?: string) => resizeTo(value ?? 'full'),
// 사용자가 숨긴 컬럼은 폭이 넓어져도 돌아오지 않아요.
hideTeam: (api: GridApi<Row>) => {
api.setColumnVisible('team', false);
say({
ko: `팀을 숨겼어요. 폭 때문에 숨은 컬럼: ${list(api.getResponsiveHiddenColumns())}`,
en: `Team hidden by the user. Hidden by width: ${listEn(api.getResponsiveHiddenColumns())}`,
});
},
showTeam: (api: GridApi<Row>) => api.setColumnVisible('team', true),
// 저장본에는 사용자가 숨긴 것만 남아요.
state: (api: GridApi<Row>) =>
say({
ko: `저장본에서 숨김: ${list(
api
.getColumnState()
.filter((s) => s.hidden)
.map((s) => s.id),
)}`,
en: `Hidden in the saved state: ${listEn(
api
.getColumnState()
.filter((s) => s.hidden)
.map((s) => s.id),
)}`,
}),
};버튼을 누르면 이 예제의 코드가 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">
<select
aria-label="그리드 폭"
[disabled]="!api"
(change)="api && actions.width(api, $any($event.target).value)"
>
<option value="full" selected>가득 (100%)</option>
<option value="900">900px</option>
<option value="700">700px</option>
<option value="550">550px</option>
<option value="400">400px</option>
</select>
<button
type="button"
[disabled]="!api"
(click)="api && actions.hideTeam(api)"
>팀 숨기기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.showTeam(api)"
>팀 보이기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.state(api)"
>저장본 보기</button>
</div>
<mx-grid style="height: 330px" [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;
team: string;
email: string;
joined: string;
}
const names = ['김서연', '이준호', '박민지', '최도윤', '정하은', '강지후', '윤서아', '임태민'];
const teams = ['개발', '디자인', '영업', '인사'];
// responsiveColumns를 켜면 hideBelow가 있는 컬럼은 그리드 상자가 그 폭(px)보다 좁을 때 숨어요.
// 사용자가 숨긴 컬럼은 계속 숨겨져 있고, 폭 때문에 숨은 컬럼은 getColumnState()에 남지 않아요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', width: 70, align: 'end' },
{ id: 'name', title: '이름', width: 120 },
{ id: 'team', title: '팀', width: 110, hideBelow: 620 },
{ id: 'email', title: '이메일', width: 210, hideBelow: 780 },
{ id: 'joined', title: '입사일', width: 120, hideBelow: 920 },
],
rows: Array.from({ length: 12 }, (_, i) => ({
id: i + 1,
name: names[i % names.length],
team: teams[(i * 3) % teams.length],
email: `user${i + 1}@example.com`,
joined: `2023-${String(1 + (i % 12)).padStart(2, '0')}-${String(3 + i).padStart(2, '0')}`,
})),
rowKey: 'id',
responsiveColumns: { debounce: 60 },
};
let say: (message: LogText) => void = () => {};
function box(): HTMLElement | null {
return document.querySelector('.mxg')?.parentElement ?? null;
}
function resizeTo(width: string): void {
const el = box();
if (!el) return;
el.style.maxWidth = '100%';
el.style.width = width === 'full' ? '100%' : `${width}px`;
}
const list = (ids: readonly string[]) => (ids.length === 0 ? '(없음)' : ids.join(', '));
const listEn = (ids: readonly string[]) => (ids.length === 0 ? '(none)' : ids.join(', '));
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
resizeTo('full');
log({
ko: '폭을 바꾸면 폭 때문에 숨은 컬럼이 로그에 나와요.',
en: 'When the width changes, the columns hidden by width are logged.',
});
const off = api.on('columnsResponsiveChanged', ({ hidden }) =>
log({ ko: `폭 때문에 숨은 컬럼: ${list(hidden)}`, en: `Hidden by width: ${listEn(hidden)}` }),
);
return () => off();
}
export const actions = {
// 그리드가 담긴 상자의 폭을 바꿔요. 그리드는 상자 폭을 따라가요.
width: (_api: GridApi<Row>, value?: string) => resizeTo(value ?? 'full'),
// 사용자가 숨긴 컬럼은 폭이 넓어져도 돌아오지 않아요.
hideTeam: (api: GridApi<Row>) => {
api.setColumnVisible('team', false);
say({
ko: `팀을 숨겼어요. 폭 때문에 숨은 컬럼: ${list(api.getResponsiveHiddenColumns())}`,
en: `Team hidden by the user. Hidden by width: ${listEn(api.getResponsiveHiddenColumns())}`,
});
},
showTeam: (api: GridApi<Row>) => api.setColumnVisible('team', true),
// 저장본에는 사용자가 숨긴 것만 남아요.
state: (api: GridApi<Row>) =>
say({
ko: `저장본에서 숨김: ${list(
api
.getColumnState()
.filter((s) => s.hidden)
.map((s) => s.id),
)}`,
en: `Hidden in the saved state: ${listEn(
api
.getColumnState()
.filter((s) => s.hidden)
.map((s) => s.id),
)}`,
}),
};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="Grid width" id="width">
<option value="full" selected>Full (100%)</option>
<option value="900">900px</option>
<option value="700">700px</option>
<option value="550">550px</option>
<option value="400">400px</option>
</select>
<button type="button" id="hideTeam">Hide Team</button>
<button type="button" id="showTeam">Show Team</button>
<button type="button" id="state">Show saved state</button>
</div>
<div id="grid" style="height: 330px"></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('width').addEventListener('change', (event) => actions.width(api, event.target.value));
document.getElementById('hideTeam').addEventListener('click', () => actions.hideTeam(api));
document.getElementById('showTeam').addEventListener('click', () => actions.showTeam(api));
document.getElementById('state').addEventListener('click', () => actions.state(api));const names = [
'Liam Carter',
'Emma Stone',
'Noah Hayes',
'Olivia Reed',
'Ethan Brooks',
'Ava Foster',
'Mason Clark',
'Mia Turner',
];
const teams = ['Dev', 'Design', 'Sales', 'HR'];
// With responsiveColumns on, a column that has hideBelow hides while the grid's box is narrower than that width (px).
// Columns the user hid stay hidden, and columns hidden by width are not written into getColumnState().
export const options = {
columns: [
{ id: 'id', title: 'No.', width: 70, align: 'end' },
{ id: 'name', title: 'Name', width: 130 },
{ id: 'team', title: 'Team', width: 110, hideBelow: 620 },
{ id: 'email', title: 'Email', width: 210, hideBelow: 780 },
{ id: 'joined', title: 'Joined', width: 120, hideBelow: 920 },
],
rows: Array.from({ length: 12 }, (_, i) => ({
id: i + 1,
name: names[i % names.length],
team: teams[(i * 3) % teams.length],
email: `user${i + 1}@example.com`,
joined: `2023-${String(1 + (i % 12)).padStart(2, '0')}-${String(3 + i).padStart(2, '0')}`,
})),
rowKey: 'id',
responsiveColumns: { debounce: 60 },
};
let say = () => { };
function box() {
return document.querySelector('.mxg')?.parentElement ?? null;
}
function resizeTo(width) {
const el = box();
if (!el)
return;
el.style.maxWidth = '100%';
el.style.width = width === 'full' ? '100%' : `${width}px`;
}
const list = (ids) => (ids.length === 0 ? '(none)' : ids.join(', '));
export function setup(api, log) {
say = log;
resizeTo('full');
log('When the width changes, the columns hidden by width are logged.');
const off = api.on('columnsResponsiveChanged', ({ hidden }) => log(`Hidden by width: ${list(hidden)}`));
return () => off();
}
export const actions = {
// Change the width of the box that holds the grid. The grid follows the box.
width: (_api, value) => resizeTo(value ?? 'full'),
// A column the user hid does not come back when the width grows.
hideTeam: (api) => {
api.setColumnVisible('team', false);
say(`Team hidden by the user. Hidden by width: ${list(api.getResponsiveHiddenColumns())}`);
},
showTeam: (api) => api.setColumnVisible('team', true),
// The saved state keeps only what the user hid.
state: (api) => say(`Hidden in the saved state: ${list(api
.getColumnState()
.filter((s) => s.hidden)
.map((s) => s.id))}`),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Hide some columns when the grid gets narrow</title>
</head>
<body>
<div class="toolbar">
<select aria-label="Grid width" id="width">
<option value="full" selected>Full (100%)</option>
<option value="900">900px</option>
<option value="700">700px</option>
<option value="550">550px</option>
<option value="400">400px</option>
</select>
<button type="button" id="hideTeam">Hide Team</button>
<button type="button" id="showTeam">Show Team</button>
<button type="button" id="state">Show saved state</button>
</div>
<div id="grid" style="height: 330px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const names = [
'Liam Carter',
'Emma Stone',
'Noah Hayes',
'Olivia Reed',
'Ethan Brooks',
'Ava Foster',
'Mason Clark',
'Mia Turner',
];
const teams = ['Dev', 'Design', 'Sales', 'HR'];
// With responsiveColumns on, a column that has hideBelow hides while the grid's box is narrower than that width (px).
// Columns the user hid stay hidden, and columns hidden by width are not written into getColumnState().
const options = {
columns: [
{ id: 'id', title: 'No.', width: 70, align: 'end' },
{ id: 'name', title: 'Name', width: 130 },
{ id: 'team', title: 'Team', width: 110, hideBelow: 620 },
{ id: 'email', title: 'Email', width: 210, hideBelow: 780 },
{ id: 'joined', title: 'Joined', width: 120, hideBelow: 920 },
],
rows: Array.from({ length: 12 }, (_, i) => ({
id: i + 1,
name: names[i % names.length],
team: teams[(i * 3) % teams.length],
email: `user${i + 1}@example.com`,
joined: `2023-${String(1 + (i % 12)).padStart(2, '0')}-${String(3 + i).padStart(2, '0')}`,
})),
rowKey: 'id',
responsiveColumns: { debounce: 60 },
};
let say = () => { };
function box() {
return document.querySelector('.mxg')?.parentElement ?? null;
}
function resizeTo(width) {
const el = box();
if (!el)
return;
el.style.maxWidth = '100%';
el.style.width = width === 'full' ? '100%' : `${width}px`;
}
const list = (ids) => (ids.length === 0 ? '(none)' : ids.join(', '));
function setup(api, log) {
say = log;
resizeTo('full');
log('When the width changes, the columns hidden by width are logged.');
const off = api.on('columnsResponsiveChanged', ({ hidden }) => log(`Hidden by width: ${list(hidden)}`));
return () => off();
}
const actions = {
// Change the width of the box that holds the grid. The grid follows the box.
width: (_api, value) => resizeTo(value ?? 'full'),
// A column the user hid does not come back when the width grows.
hideTeam: (api) => {
api.setColumnVisible('team', false);
say(`Team hidden by the user. Hidden by width: ${list(api.getResponsiveHiddenColumns())}`);
},
showTeam: (api) => api.setColumnVisible('team', true),
// The saved state keeps only what the user hid.
state: (api) => say(`Hidden in the saved state: ${list(api
.getColumnState()
.filter((s) => s.hidden)
.map((s) => s.id))}`),
};
options.ariaLabel = "Hide some columns when the grid gets narrow";
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('width').addEventListener('change', (event) => actions.width(api, event.target.value));
document.getElementById('hideTeam').addEventListener('click', () => actions.hideTeam(api));
document.getElementById('showTeam').addEventListener('click', () => actions.showTeam(api));
document.getElementById('state').addEventListener('click', () => actions.state(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">
<select
aria-label="Grid width"
disabled={!api}
onChange={(event) => api && actions.width(api, event.target.value)}
>
<option value="full">Full (100%)</option>
<option value="900">900px</option>
<option value="700">700px</option>
<option value="550">550px</option>
<option value="400">400px</option>
</select>
<button
type="button"
disabled={!api}
onClick={() => api && actions.hideTeam(api)}
>Hide Team</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.showTeam(api)}
>Show Team</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.state(api)}
>Show saved state</button>
</div>
<Grid style={{ height: 330 }} 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;
team: string;
email: string;
joined: string;
}
const names = [
'Liam Carter',
'Emma Stone',
'Noah Hayes',
'Olivia Reed',
'Ethan Brooks',
'Ava Foster',
'Mason Clark',
'Mia Turner',
];
const teams = ['Dev', 'Design', 'Sales', 'HR'];
// With responsiveColumns on, a column that has hideBelow hides while the grid's box is narrower than that width (px).
// Columns the user hid stay hidden, and columns hidden by width are not written into getColumnState().
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', width: 70, align: 'end' },
{ id: 'name', title: 'Name', width: 130 },
{ id: 'team', title: 'Team', width: 110, hideBelow: 620 },
{ id: 'email', title: 'Email', width: 210, hideBelow: 780 },
{ id: 'joined', title: 'Joined', width: 120, hideBelow: 920 },
],
rows: Array.from({ length: 12 }, (_, i) => ({
id: i + 1,
name: names[i % names.length],
team: teams[(i * 3) % teams.length],
email: `user${i + 1}@example.com`,
joined: `2023-${String(1 + (i % 12)).padStart(2, '0')}-${String(3 + i).padStart(2, '0')}`,
})),
rowKey: 'id',
responsiveColumns: { debounce: 60 },
};
let say: (message: LogText) => void = () => {};
function box(): HTMLElement | null {
return document.querySelector('.mxg')?.parentElement ?? null;
}
function resizeTo(width: string): void {
const el = box();
if (!el) return;
el.style.maxWidth = '100%';
el.style.width = width === 'full' ? '100%' : `${width}px`;
}
const list = (ids: readonly string[]) => (ids.length === 0 ? '(none)' : ids.join(', '));
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
resizeTo('full');
log('When the width changes, the columns hidden by width are logged.');
const off = api.on('columnsResponsiveChanged', ({ hidden }) =>
log(`Hidden by width: ${list(hidden)}`),
);
return () => off();
}
export const actions = {
// Change the width of the box that holds the grid. The grid follows the box.
width: (_api: GridApi<Row>, value?: string) => resizeTo(value ?? 'full'),
// A column the user hid does not come back when the width grows.
hideTeam: (api: GridApi<Row>) => {
api.setColumnVisible('team', false);
say(`Team hidden by the user. Hidden by width: ${list(api.getResponsiveHiddenColumns())}`);
},
showTeam: (api: GridApi<Row>) => api.setColumnVisible('team', true),
// The saved state keeps only what the user hid.
state: (api: GridApi<Row>) =>
say(
`Hidden in the saved state: ${list(
api
.getColumnState()
.filter((s) => s.hidden)
.map((s) => s.id),
)}`,
),
};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">
<select
aria-label="Grid width"
:disabled="!api"
@change="api && actions.width(api, ($event.target as HTMLSelectElement).value)"
>
<option value="full" selected>Full (100%)</option>
<option value="900">900px</option>
<option value="700">700px</option>
<option value="550">550px</option>
<option value="400">400px</option>
</select>
<button
type="button"
:disabled="!api"
@click="api && actions.hideTeam(api)"
>Hide Team</button>
<button
type="button"
:disabled="!api"
@click="api && actions.showTeam(api)"
>Show Team</button>
<button type="button" :disabled="!api" @click="api && actions.state(api)">Show saved state</button>
</div>
<Grid style="height: 330px" :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;
team: string;
email: string;
joined: string;
}
const names = [
'Liam Carter',
'Emma Stone',
'Noah Hayes',
'Olivia Reed',
'Ethan Brooks',
'Ava Foster',
'Mason Clark',
'Mia Turner',
];
const teams = ['Dev', 'Design', 'Sales', 'HR'];
// With responsiveColumns on, a column that has hideBelow hides while the grid's box is narrower than that width (px).
// Columns the user hid stay hidden, and columns hidden by width are not written into getColumnState().
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', width: 70, align: 'end' },
{ id: 'name', title: 'Name', width: 130 },
{ id: 'team', title: 'Team', width: 110, hideBelow: 620 },
{ id: 'email', title: 'Email', width: 210, hideBelow: 780 },
{ id: 'joined', title: 'Joined', width: 120, hideBelow: 920 },
],
rows: Array.from({ length: 12 }, (_, i) => ({
id: i + 1,
name: names[i % names.length],
team: teams[(i * 3) % teams.length],
email: `user${i + 1}@example.com`,
joined: `2023-${String(1 + (i % 12)).padStart(2, '0')}-${String(3 + i).padStart(2, '0')}`,
})),
rowKey: 'id',
responsiveColumns: { debounce: 60 },
};
let say: (message: LogText) => void = () => {};
function box(): HTMLElement | null {
return document.querySelector('.mxg')?.parentElement ?? null;
}
function resizeTo(width: string): void {
const el = box();
if (!el) return;
el.style.maxWidth = '100%';
el.style.width = width === 'full' ? '100%' : `${width}px`;
}
const list = (ids: readonly string[]) => (ids.length === 0 ? '(none)' : ids.join(', '));
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
resizeTo('full');
log('When the width changes, the columns hidden by width are logged.');
const off = api.on('columnsResponsiveChanged', ({ hidden }) =>
log(`Hidden by width: ${list(hidden)}`),
);
return () => off();
}
export const actions = {
// Change the width of the box that holds the grid. The grid follows the box.
width: (_api: GridApi<Row>, value?: string) => resizeTo(value ?? 'full'),
// A column the user hid does not come back when the width grows.
hideTeam: (api: GridApi<Row>) => {
api.setColumnVisible('team', false);
say(`Team hidden by the user. Hidden by width: ${list(api.getResponsiveHiddenColumns())}`);
},
showTeam: (api: GridApi<Row>) => api.setColumnVisible('team', true),
// The saved state keeps only what the user hid.
state: (api: GridApi<Row>) =>
say(
`Hidden in the saved state: ${list(
api
.getColumnState()
.filter((s) => s.hidden)
.map((s) => s.id),
)}`,
),
};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">
<select
aria-label="Grid width"
[disabled]="!api"
(change)="api && actions.width(api, $any($event.target).value)"
>
<option value="full" selected>Full (100%)</option>
<option value="900">900px</option>
<option value="700">700px</option>
<option value="550">550px</option>
<option value="400">400px</option>
</select>
<button
type="button"
[disabled]="!api"
(click)="api && actions.hideTeam(api)"
>Hide Team</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.showTeam(api)"
>Show Team</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.state(api)"
>Show saved state</button>
</div>
<mx-grid style="height: 330px" [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;
team: string;
email: string;
joined: string;
}
const names = [
'Liam Carter',
'Emma Stone',
'Noah Hayes',
'Olivia Reed',
'Ethan Brooks',
'Ava Foster',
'Mason Clark',
'Mia Turner',
];
const teams = ['Dev', 'Design', 'Sales', 'HR'];
// With responsiveColumns on, a column that has hideBelow hides while the grid's box is narrower than that width (px).
// Columns the user hid stay hidden, and columns hidden by width are not written into getColumnState().
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', width: 70, align: 'end' },
{ id: 'name', title: 'Name', width: 130 },
{ id: 'team', title: 'Team', width: 110, hideBelow: 620 },
{ id: 'email', title: 'Email', width: 210, hideBelow: 780 },
{ id: 'joined', title: 'Joined', width: 120, hideBelow: 920 },
],
rows: Array.from({ length: 12 }, (_, i) => ({
id: i + 1,
name: names[i % names.length],
team: teams[(i * 3) % teams.length],
email: `user${i + 1}@example.com`,
joined: `2023-${String(1 + (i % 12)).padStart(2, '0')}-${String(3 + i).padStart(2, '0')}`,
})),
rowKey: 'id',
responsiveColumns: { debounce: 60 },
};
let say: (message: LogText) => void = () => {};
function box(): HTMLElement | null {
return document.querySelector('.mxg')?.parentElement ?? null;
}
function resizeTo(width: string): void {
const el = box();
if (!el) return;
el.style.maxWidth = '100%';
el.style.width = width === 'full' ? '100%' : `${width}px`;
}
const list = (ids: readonly string[]) => (ids.length === 0 ? '(none)' : ids.join(', '));
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
resizeTo('full');
log('When the width changes, the columns hidden by width are logged.');
const off = api.on('columnsResponsiveChanged', ({ hidden }) =>
log(`Hidden by width: ${list(hidden)}`),
);
return () => off();
}
export const actions = {
// Change the width of the box that holds the grid. The grid follows the box.
width: (_api: GridApi<Row>, value?: string) => resizeTo(value ?? 'full'),
// A column the user hid does not come back when the width grows.
hideTeam: (api: GridApi<Row>) => {
api.setColumnVisible('team', false);
say(`Team hidden by the user. Hidden by width: ${list(api.getResponsiveHiddenColumns())}`);
},
showTeam: (api: GridApi<Row>) => api.setColumnVisible('team', true),
// The saved state keeps only what the user hid.
state: (api: GridApi<Row>) =>
say(
`Hidden in the saved state: ${list(
api
.getColumnState()
.filter((s) => s.hidden)
.map((s) => s.id),
)}`,
),
};