프리셋에 정렬도 함께 저장하기Save the sort together with the preset
filterPresets에 includeSort: true를 주면 프리셋에 필터와 함께 정렬도 저장돼요. 프리셋을 적용하면 필터와 정렬이 같이 돌아와요.With includeSort: true in filterPresets a preset stores the sort along with the filter, so applying it restores both.
코드Code
탭을 골라 코드를 복사하세요. 위 화면은 이 코드를 그대로 실행한 결과예요.Pick a tab and copy the code. The preview above runs exactly this code.
이 화면은 새 모양 옵션을 켠 모양이에요: density: 'spacious', iconStyle: 'soft', motion: true (코드에 없으면 기본 모양으로 보여요)This preview has the new-look options on: density: 'spacious', iconStyle: 'soft', motion: true (without them in your code you get the default look)
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<div id="app">
<div class="toolbar">
<button type="button" id="sortSalary">연봉 내림차순</button>
<button type="button" id="filterDev">코드로 걸기: 개발팀만</button>
<button type="button" id="save">'개발팀 연봉순'으로 저장</button>
<button type="button" id="reset">필터·정렬 지우기</button>
<button type="button" id="applySaved">저장한 프리셋 적용</button>
</div>
<div id="grid" style="height: 520px"></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('sortSalary').addEventListener('click', () => actions.sortSalary(api));
document.getElementById('filterDev').addEventListener('click', () => actions.filterDev(api));
document.getElementById('save').addEventListener('click', () => actions.save(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));
document.getElementById('applySaved').addEventListener('click', () => actions.applySaved(api));const names = ['김민준', '이서연', '박지호', '최유나', '정도윤', '한소희', '오세훈', '윤지아'];
const depts = ['개발팀', '디자인팀', '영업팀', '인사팀', null];
// includeSort: true면 팝업의 "정렬도 저장"이 처음부터 체크돼 있고, 프리셋에 정렬이 같이 저장돼요.
export const options = {
filterMenu: true,
filterPresets: { includeSort: true },
columns: [
{ id: 'name', title: '이름', width: 120 },
{ id: 'dept', title: '부서', width: 130 },
{
id: 'salary',
title: '연봉(만원)',
kind: 'number',
width: 160,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
},
{
id: 'hired',
title: '입사일',
kind: 'date',
width: 130,
formatValue: (value) => (value instanceof Date ? value.toISOString().slice(0, 10) : ''),
},
],
rows: Array.from({ length: 24 }, (_, i) => ({
id: i + 1,
name: names[i % 8],
dept: depts[(i * 3) % 5] ?? null,
salary: 3600 + ((i * 530) % 4600),
hired: new Date(2019 + (i % 6), (i * 5) % 12, 3 + ((i * 7) % 24)),
})),
rowKey: 'id',
};
let say;
export function setup(api, log) {
say = log;
const off = api.on('filterPresetApplied', ({ preset }) => {
log({
ko: `프리셋 "${preset.name}" 적용 · ${api.getVisibleRowCount()}행 보임`,
en: `Preset "${preset.name}" applied · ${api.getVisibleRowCount()} rows visible`,
});
});
return () => off();
}
export const actions = {
sortSalary: (api) => api.setSortModel([{ columnId: 'salary', direction: 'desc' }]),
filterDev: (api) => api.setFilterModel({
match: 'all',
items: [{ columnId: 'dept', operator: 'equals', value: '개발팀' }],
}),
save: (api) => api.saveFilterPreset('개발팀 연봉순'),
reset: (api) => {
api.setFilterModel(null);
api.setSortModel([]);
},
applySaved: (api) => {
if (!api.applyFilterPreset('개발팀 연봉순'))
return;
const sorted = api.getSortModel().length > 0;
say?.({ ko: `정렬 ${sorted ? '복원됨' : '없음'}`, en: `Sort ${sorted ? 'restored' : 'none'}` });
},
};<!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="sortSalary">연봉 내림차순</button>
<button type="button" id="filterDev">코드로 걸기: 개발팀만</button>
<button type="button" id="save">'개발팀 연봉순'으로 저장</button>
<button type="button" id="reset">필터·정렬 지우기</button>
<button type="button" id="applySaved">저장한 프리셋 적용</button>
</div>
<div id="grid" style="height: 520px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const names = ['김민준', '이서연', '박지호', '최유나', '정도윤', '한소희', '오세훈', '윤지아'];
const depts = ['개발팀', '디자인팀', '영업팀', '인사팀', null];
// includeSort: true면 팝업의 "정렬도 저장"이 처음부터 체크돼 있고, 프리셋에 정렬이 같이 저장돼요.
const options = {
filterMenu: true,
filterPresets: { includeSort: true },
columns: [
{ id: 'name', title: '이름', width: 120 },
{ id: 'dept', title: '부서', width: 130 },
{
id: 'salary',
title: '연봉(만원)',
kind: 'number',
width: 160,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
},
{
id: 'hired',
title: '입사일',
kind: 'date',
width: 130,
formatValue: (value) => (value instanceof Date ? value.toISOString().slice(0, 10) : ''),
},
],
rows: Array.from({ length: 24 }, (_, i) => ({
id: i + 1,
name: names[i % 8],
dept: depts[(i * 3) % 5] ?? null,
salary: 3600 + ((i * 530) % 4600),
hired: new Date(2019 + (i % 6), (i * 5) % 12, 3 + ((i * 7) % 24)),
})),
rowKey: 'id',
};
let say;
function setup(api, log) {
say = log;
const off = api.on('filterPresetApplied', ({ preset }) => {
log({
ko: `프리셋 "${preset.name}" 적용 · ${api.getVisibleRowCount()}행 보임`,
en: `Preset "${preset.name}" applied · ${api.getVisibleRowCount()} rows visible`,
});
});
return () => off();
}
const actions = {
sortSalary: (api) => api.setSortModel([{ columnId: 'salary', direction: 'desc' }]),
filterDev: (api) => api.setFilterModel({
match: 'all',
items: [{ columnId: 'dept', operator: 'equals', value: '개발팀' }],
}),
save: (api) => api.saveFilterPreset('개발팀 연봉순'),
reset: (api) => {
api.setFilterModel(null);
api.setSortModel([]);
},
applySaved: (api) => {
if (!api.applyFilterPreset('개발팀 연봉순'))
return;
const sorted = api.getSortModel().length > 0;
say?.({ ko: `정렬 ${sorted ? '복원됨' : '없음'}`, en: `Sort ${sorted ? 'restored' : 'none'}` });
},
};
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('sortSalary').addEventListener('click', () => actions.sortSalary(api));
document.getElementById('filterDev').addEventListener('click', () => actions.filterDev(api));
document.getElementById('save').addEventListener('click', () => actions.save(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));
document.getElementById('applySaved').addEventListener('click', () => actions.applySaved(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.sortSalary(api)}
>연봉 내림차순</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.filterDev(api)}
>코드로 걸기: 개발팀만</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.save(api)}
>'개발팀 연봉순'으로 저장</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.reset(api)}
>필터·정렬 지우기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.applySaved(api)}
>저장한 프리셋 적용</button>
</div>
<Grid style={{ height: 520 }} 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;
dept: string | null;
salary: number;
hired: Date;
}
const names = ['김민준', '이서연', '박지호', '최유나', '정도윤', '한소희', '오세훈', '윤지아'];
const depts = ['개발팀', '디자인팀', '영업팀', '인사팀', null];
// includeSort: true면 팝업의 "정렬도 저장"이 처음부터 체크돼 있고, 프리셋에 정렬이 같이 저장돼요.
export const options: GridOptions<Row> = {
filterMenu: true,
filterPresets: { includeSort: true },
columns: [
{ id: 'name', title: '이름', width: 120 },
{ id: 'dept', title: '부서', width: 130 },
{
id: 'salary',
title: '연봉(만원)',
kind: 'number',
width: 160,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
},
{
id: 'hired',
title: '입사일',
kind: 'date',
width: 130,
formatValue: (value) => (value instanceof Date ? value.toISOString().slice(0, 10) : ''),
},
],
rows: Array.from({ length: 24 }, (_, i) => ({
id: i + 1,
name: names[i % 8] as string,
dept: depts[(i * 3) % 5] ?? null,
salary: 3600 + ((i * 530) % 4600),
hired: new Date(2019 + (i % 6), (i * 5) % 12, 3 + ((i * 7) % 24)),
})),
rowKey: 'id',
};
let say: ((message: LogText) => void) | undefined;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
const off = api.on('filterPresetApplied', ({ preset }) => {
log({
ko: `프리셋 "${preset.name}" 적용 · ${api.getVisibleRowCount()}행 보임`,
en: `Preset "${preset.name}" applied · ${api.getVisibleRowCount()} rows visible`,
});
});
return () => off();
}
export const actions = {
sortSalary: (api: GridApi<Row>) => api.setSortModel([{ columnId: 'salary', direction: 'desc' }]),
filterDev: (api: GridApi<Row>) =>
api.setFilterModel({
match: 'all',
items: [{ columnId: 'dept', operator: 'equals', value: '개발팀' }],
}),
save: (api: GridApi<Row>) => api.saveFilterPreset('개발팀 연봉순'),
reset: (api: GridApi<Row>) => {
api.setFilterModel(null);
api.setSortModel([]);
},
applySaved: (api: GridApi<Row>) => {
if (!api.applyFilterPreset('개발팀 연봉순')) return;
const sorted = api.getSortModel().length > 0;
say?.({ ko: `정렬 ${sorted ? '복원됨' : '없음'}`, en: `Sort ${sorted ? 'restored' : 'none'}` });
},
};버튼을 누르면 이 예제의 코드가 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.sortSalary(api)"
>연봉 내림차순</button>
<button
type="button"
:disabled="!api"
@click="api && actions.filterDev(api)"
>코드로 걸기: 개발팀만</button>
<button type="button" :disabled="!api" @click="api && actions.save(api)">'개발팀 연봉순'으로 저장</button>
<button type="button" :disabled="!api" @click="api && actions.reset(api)">필터·정렬 지우기</button>
<button
type="button"
:disabled="!api"
@click="api && actions.applySaved(api)"
>저장한 프리셋 적용</button>
</div>
<Grid style="height: 520px" :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;
dept: string | null;
salary: number;
hired: Date;
}
const names = ['김민준', '이서연', '박지호', '최유나', '정도윤', '한소희', '오세훈', '윤지아'];
const depts = ['개발팀', '디자인팀', '영업팀', '인사팀', null];
// includeSort: true면 팝업의 "정렬도 저장"이 처음부터 체크돼 있고, 프리셋에 정렬이 같이 저장돼요.
export const options: GridOptions<Row> = {
filterMenu: true,
filterPresets: { includeSort: true },
columns: [
{ id: 'name', title: '이름', width: 120 },
{ id: 'dept', title: '부서', width: 130 },
{
id: 'salary',
title: '연봉(만원)',
kind: 'number',
width: 160,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
},
{
id: 'hired',
title: '입사일',
kind: 'date',
width: 130,
formatValue: (value) => (value instanceof Date ? value.toISOString().slice(0, 10) : ''),
},
],
rows: Array.from({ length: 24 }, (_, i) => ({
id: i + 1,
name: names[i % 8] as string,
dept: depts[(i * 3) % 5] ?? null,
salary: 3600 + ((i * 530) % 4600),
hired: new Date(2019 + (i % 6), (i * 5) % 12, 3 + ((i * 7) % 24)),
})),
rowKey: 'id',
};
let say: ((message: LogText) => void) | undefined;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
const off = api.on('filterPresetApplied', ({ preset }) => {
log({
ko: `프리셋 "${preset.name}" 적용 · ${api.getVisibleRowCount()}행 보임`,
en: `Preset "${preset.name}" applied · ${api.getVisibleRowCount()} rows visible`,
});
});
return () => off();
}
export const actions = {
sortSalary: (api: GridApi<Row>) => api.setSortModel([{ columnId: 'salary', direction: 'desc' }]),
filterDev: (api: GridApi<Row>) =>
api.setFilterModel({
match: 'all',
items: [{ columnId: 'dept', operator: 'equals', value: '개발팀' }],
}),
save: (api: GridApi<Row>) => api.saveFilterPreset('개발팀 연봉순'),
reset: (api: GridApi<Row>) => {
api.setFilterModel(null);
api.setSortModel([]);
},
applySaved: (api: GridApi<Row>) => {
if (!api.applyFilterPreset('개발팀 연봉순')) return;
const sorted = api.getSortModel().length > 0;
say?.({ ko: `정렬 ${sorted ? '복원됨' : '없음'}`, en: `Sort ${sorted ? 'restored' : 'none'}` });
},
};버튼을 누르면 이 예제의 코드가 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.sortSalary(api)"
>연봉 내림차순</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.filterDev(api)"
>코드로 걸기: 개발팀만</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.save(api)"
>'개발팀 연봉순'으로 저장</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.reset(api)"
>필터·정렬 지우기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.applySaved(api)"
>저장한 프리셋 적용</button>
</div>
<mx-grid style="height: 520px" [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;
dept: string | null;
salary: number;
hired: Date;
}
const names = ['김민준', '이서연', '박지호', '최유나', '정도윤', '한소희', '오세훈', '윤지아'];
const depts = ['개발팀', '디자인팀', '영업팀', '인사팀', null];
// includeSort: true면 팝업의 "정렬도 저장"이 처음부터 체크돼 있고, 프리셋에 정렬이 같이 저장돼요.
export const options: GridOptions<Row> = {
filterMenu: true,
filterPresets: { includeSort: true },
columns: [
{ id: 'name', title: '이름', width: 120 },
{ id: 'dept', title: '부서', width: 130 },
{
id: 'salary',
title: '연봉(만원)',
kind: 'number',
width: 160,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
},
{
id: 'hired',
title: '입사일',
kind: 'date',
width: 130,
formatValue: (value) => (value instanceof Date ? value.toISOString().slice(0, 10) : ''),
},
],
rows: Array.from({ length: 24 }, (_, i) => ({
id: i + 1,
name: names[i % 8] as string,
dept: depts[(i * 3) % 5] ?? null,
salary: 3600 + ((i * 530) % 4600),
hired: new Date(2019 + (i % 6), (i * 5) % 12, 3 + ((i * 7) % 24)),
})),
rowKey: 'id',
};
let say: ((message: LogText) => void) | undefined;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
const off = api.on('filterPresetApplied', ({ preset }) => {
log({
ko: `프리셋 "${preset.name}" 적용 · ${api.getVisibleRowCount()}행 보임`,
en: `Preset "${preset.name}" applied · ${api.getVisibleRowCount()} rows visible`,
});
});
return () => off();
}
export const actions = {
sortSalary: (api: GridApi<Row>) => api.setSortModel([{ columnId: 'salary', direction: 'desc' }]),
filterDev: (api: GridApi<Row>) =>
api.setFilterModel({
match: 'all',
items: [{ columnId: 'dept', operator: 'equals', value: '개발팀' }],
}),
save: (api: GridApi<Row>) => api.saveFilterPreset('개발팀 연봉순'),
reset: (api: GridApi<Row>) => {
api.setFilterModel(null);
api.setSortModel([]);
},
applySaved: (api: GridApi<Row>) => {
if (!api.applyFilterPreset('개발팀 연봉순')) return;
const sorted = api.getSortModel().length > 0;
say?.({ ko: `정렬 ${sorted ? '복원됨' : '없음'}`, en: `Sort ${sorted ? 'restored' : 'none'}` });
},
};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="sortSalary">Salary descending</button>
<button type="button" id="filterDev">Set in code: Dev only</button>
<button type="button" id="save">Save as 'Dev by salary'</button>
<button type="button" id="reset">Clear filter and sort</button>
<button type="button" id="applySaved">Apply saved preset</button>
</div>
<div id="grid" style="height: 520px"></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('sortSalary').addEventListener('click', () => actions.sortSalary(api));
document.getElementById('filterDev').addEventListener('click', () => actions.filterDev(api));
document.getElementById('save').addEventListener('click', () => actions.save(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));
document.getElementById('applySaved').addEventListener('click', () => actions.applySaved(api));const names = [
'Liam Carter',
'Emma Stone',
'Noah Reed',
'Olivia Hayes',
'Ethan Brooks',
'Sophia Lane',
'Mason Clark',
'Ava Morgan',
];
const depts = ['Dev', 'Design', 'Sales', 'HR', null];
// With includeSort: true the "Include sort" box in the popup is ticked by default and the sort is saved with the preset.
export const options = {
filterMenu: true,
filterPresets: { includeSort: true },
columns: [
{ id: 'name', title: 'Name', width: 130 },
{ id: 'dept', title: 'Dept', width: 130 },
{
id: 'salary',
title: 'Salary ($)',
kind: 'number',
width: 160,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('en-US'),
},
{
id: 'hired',
title: 'Hired',
kind: 'date',
width: 130,
formatValue: (value) => (value instanceof Date ? value.toISOString().slice(0, 10) : ''),
},
],
rows: Array.from({ length: 24 }, (_, i) => ({
id: i + 1,
name: names[i % 8],
dept: depts[(i * 3) % 5] ?? null,
salary: 36000 + ((i * 5300) % 46000),
hired: new Date(2019 + (i % 6), (i * 5) % 12, 3 + ((i * 7) % 24)),
})),
rowKey: 'id',
};
let say;
export function setup(api, log) {
say = log;
const off = api.on('filterPresetApplied', ({ preset }) => {
log(`Preset "${preset.name}" applied · ${api.getVisibleRowCount()} rows visible`);
});
return () => off();
}
export const actions = {
sortSalary: (api) => api.setSortModel([{ columnId: 'salary', direction: 'desc' }]),
filterDev: (api) => api.setFilterModel({
match: 'all',
items: [{ columnId: 'dept', operator: 'equals', value: 'Dev' }],
}),
save: (api) => api.saveFilterPreset('Dev by salary'),
reset: (api) => {
api.setFilterModel(null);
api.setSortModel([]);
},
applySaved: (api) => {
if (!api.applyFilterPreset('Dev by salary'))
return;
const sorted = api.getSortModel().length > 0;
say?.(`Sort ${sorted ? 'restored' : 'none'}`);
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Save the sort together with the preset</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="sortSalary">Salary descending</button>
<button type="button" id="filterDev">Set in code: Dev only</button>
<button type="button" id="save">Save as 'Dev by salary'</button>
<button type="button" id="reset">Clear filter and sort</button>
<button type="button" id="applySaved">Apply saved preset</button>
</div>
<div id="grid" style="height: 520px"></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 Reed',
'Olivia Hayes',
'Ethan Brooks',
'Sophia Lane',
'Mason Clark',
'Ava Morgan',
];
const depts = ['Dev', 'Design', 'Sales', 'HR', null];
// With includeSort: true the "Include sort" box in the popup is ticked by default and the sort is saved with the preset.
const options = {
filterMenu: true,
filterPresets: { includeSort: true },
columns: [
{ id: 'name', title: 'Name', width: 130 },
{ id: 'dept', title: 'Dept', width: 130 },
{
id: 'salary',
title: 'Salary ($)',
kind: 'number',
width: 160,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('en-US'),
},
{
id: 'hired',
title: 'Hired',
kind: 'date',
width: 130,
formatValue: (value) => (value instanceof Date ? value.toISOString().slice(0, 10) : ''),
},
],
rows: Array.from({ length: 24 }, (_, i) => ({
id: i + 1,
name: names[i % 8],
dept: depts[(i * 3) % 5] ?? null,
salary: 36000 + ((i * 5300) % 46000),
hired: new Date(2019 + (i % 6), (i * 5) % 12, 3 + ((i * 7) % 24)),
})),
rowKey: 'id',
};
let say;
function setup(api, log) {
say = log;
const off = api.on('filterPresetApplied', ({ preset }) => {
log(`Preset "${preset.name}" applied · ${api.getVisibleRowCount()} rows visible`);
});
return () => off();
}
const actions = {
sortSalary: (api) => api.setSortModel([{ columnId: 'salary', direction: 'desc' }]),
filterDev: (api) => api.setFilterModel({
match: 'all',
items: [{ columnId: 'dept', operator: 'equals', value: 'Dev' }],
}),
save: (api) => api.saveFilterPreset('Dev by salary'),
reset: (api) => {
api.setFilterModel(null);
api.setSortModel([]);
},
applySaved: (api) => {
if (!api.applyFilterPreset('Dev by salary'))
return;
const sorted = api.getSortModel().length > 0;
say?.(`Sort ${sorted ? 'restored' : 'none'}`);
},
};
options.ariaLabel = "Save the sort together with the preset";
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('sortSalary').addEventListener('click', () => actions.sortSalary(api));
document.getElementById('filterDev').addEventListener('click', () => actions.filterDev(api));
document.getElementById('save').addEventListener('click', () => actions.save(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));
document.getElementById('applySaved').addEventListener('click', () => actions.applySaved(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.sortSalary(api)}
>Salary descending</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.filterDev(api)}
>Set in code: Dev only</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.save(api)}
>Save as 'Dev by salary'</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.reset(api)}
>Clear filter and sort</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.applySaved(api)}
>Apply saved preset</button>
</div>
<Grid style={{ height: 520 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
name: string;
dept: string | null;
salary: number;
hired: Date;
}
const names = [
'Liam Carter',
'Emma Stone',
'Noah Reed',
'Olivia Hayes',
'Ethan Brooks',
'Sophia Lane',
'Mason Clark',
'Ava Morgan',
];
const depts = ['Dev', 'Design', 'Sales', 'HR', null];
// With includeSort: true the "Include sort" box in the popup is ticked by default and the sort is saved with the preset.
export const options: GridOptions<Row> = {
filterMenu: true,
filterPresets: { includeSort: true },
columns: [
{ id: 'name', title: 'Name', width: 130 },
{ id: 'dept', title: 'Dept', width: 130 },
{
id: 'salary',
title: 'Salary ($)',
kind: 'number',
width: 160,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('en-US'),
},
{
id: 'hired',
title: 'Hired',
kind: 'date',
width: 130,
formatValue: (value) => (value instanceof Date ? value.toISOString().slice(0, 10) : ''),
},
],
rows: Array.from({ length: 24 }, (_, i) => ({
id: i + 1,
name: names[i % 8] as string,
dept: depts[(i * 3) % 5] ?? null,
salary: 36000 + ((i * 5300) % 46000),
hired: new Date(2019 + (i % 6), (i * 5) % 12, 3 + ((i * 7) % 24)),
})),
rowKey: 'id',
};
let say: ((message: LogText) => void) | undefined;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
const off = api.on('filterPresetApplied', ({ preset }) => {
log(`Preset "${preset.name}" applied · ${api.getVisibleRowCount()} rows visible`);
});
return () => off();
}
export const actions = {
sortSalary: (api: GridApi<Row>) => api.setSortModel([{ columnId: 'salary', direction: 'desc' }]),
filterDev: (api: GridApi<Row>) =>
api.setFilterModel({
match: 'all',
items: [{ columnId: 'dept', operator: 'equals', value: 'Dev' }],
}),
save: (api: GridApi<Row>) => api.saveFilterPreset('Dev by salary'),
reset: (api: GridApi<Row>) => {
api.setFilterModel(null);
api.setSortModel([]);
},
applySaved: (api: GridApi<Row>) => {
if (!api.applyFilterPreset('Dev by salary')) return;
const sorted = api.getSortModel().length > 0;
say?.(`Sort ${sorted ? 'restored' : 'none'}`);
},
};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.sortSalary(api)"
>Salary descending</button>
<button
type="button"
:disabled="!api"
@click="api && actions.filterDev(api)"
>Set in code: Dev only</button>
<button type="button" :disabled="!api" @click="api && actions.save(api)">Save as 'Dev by salary'</button>
<button type="button" :disabled="!api" @click="api && actions.reset(api)">Clear filter and sort</button>
<button
type="button"
:disabled="!api"
@click="api && actions.applySaved(api)"
>Apply saved preset</button>
</div>
<Grid style="height: 520px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
name: string;
dept: string | null;
salary: number;
hired: Date;
}
const names = [
'Liam Carter',
'Emma Stone',
'Noah Reed',
'Olivia Hayes',
'Ethan Brooks',
'Sophia Lane',
'Mason Clark',
'Ava Morgan',
];
const depts = ['Dev', 'Design', 'Sales', 'HR', null];
// With includeSort: true the "Include sort" box in the popup is ticked by default and the sort is saved with the preset.
export const options: GridOptions<Row> = {
filterMenu: true,
filterPresets: { includeSort: true },
columns: [
{ id: 'name', title: 'Name', width: 130 },
{ id: 'dept', title: 'Dept', width: 130 },
{
id: 'salary',
title: 'Salary ($)',
kind: 'number',
width: 160,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('en-US'),
},
{
id: 'hired',
title: 'Hired',
kind: 'date',
width: 130,
formatValue: (value) => (value instanceof Date ? value.toISOString().slice(0, 10) : ''),
},
],
rows: Array.from({ length: 24 }, (_, i) => ({
id: i + 1,
name: names[i % 8] as string,
dept: depts[(i * 3) % 5] ?? null,
salary: 36000 + ((i * 5300) % 46000),
hired: new Date(2019 + (i % 6), (i * 5) % 12, 3 + ((i * 7) % 24)),
})),
rowKey: 'id',
};
let say: ((message: LogText) => void) | undefined;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
const off = api.on('filterPresetApplied', ({ preset }) => {
log(`Preset "${preset.name}" applied · ${api.getVisibleRowCount()} rows visible`);
});
return () => off();
}
export const actions = {
sortSalary: (api: GridApi<Row>) => api.setSortModel([{ columnId: 'salary', direction: 'desc' }]),
filterDev: (api: GridApi<Row>) =>
api.setFilterModel({
match: 'all',
items: [{ columnId: 'dept', operator: 'equals', value: 'Dev' }],
}),
save: (api: GridApi<Row>) => api.saveFilterPreset('Dev by salary'),
reset: (api: GridApi<Row>) => {
api.setFilterModel(null);
api.setSortModel([]);
},
applySaved: (api: GridApi<Row>) => {
if (!api.applyFilterPreset('Dev by salary')) return;
const sorted = api.getSortModel().length > 0;
say?.(`Sort ${sorted ? 'restored' : 'none'}`);
},
};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.sortSalary(api)"
>Salary descending</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.filterDev(api)"
>Set in code: Dev only</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.save(api)"
>Save as 'Dev by salary'</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.reset(api)"
>Clear filter and sort</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.applySaved(api)"
>Apply saved preset</button>
</div>
<mx-grid style="height: 520px" [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;
export interface Row {
id: number;
name: string;
dept: string | null;
salary: number;
hired: Date;
}
const names = [
'Liam Carter',
'Emma Stone',
'Noah Reed',
'Olivia Hayes',
'Ethan Brooks',
'Sophia Lane',
'Mason Clark',
'Ava Morgan',
];
const depts = ['Dev', 'Design', 'Sales', 'HR', null];
// With includeSort: true the "Include sort" box in the popup is ticked by default and the sort is saved with the preset.
export const options: GridOptions<Row> = {
filterMenu: true,
filterPresets: { includeSort: true },
columns: [
{ id: 'name', title: 'Name', width: 130 },
{ id: 'dept', title: 'Dept', width: 130 },
{
id: 'salary',
title: 'Salary ($)',
kind: 'number',
width: 160,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('en-US'),
},
{
id: 'hired',
title: 'Hired',
kind: 'date',
width: 130,
formatValue: (value) => (value instanceof Date ? value.toISOString().slice(0, 10) : ''),
},
],
rows: Array.from({ length: 24 }, (_, i) => ({
id: i + 1,
name: names[i % 8] as string,
dept: depts[(i * 3) % 5] ?? null,
salary: 36000 + ((i * 5300) % 46000),
hired: new Date(2019 + (i % 6), (i * 5) % 12, 3 + ((i * 7) % 24)),
})),
rowKey: 'id',
};
let say: ((message: LogText) => void) | undefined;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
const off = api.on('filterPresetApplied', ({ preset }) => {
log(`Preset "${preset.name}" applied · ${api.getVisibleRowCount()} rows visible`);
});
return () => off();
}
export const actions = {
sortSalary: (api: GridApi<Row>) => api.setSortModel([{ columnId: 'salary', direction: 'desc' }]),
filterDev: (api: GridApi<Row>) =>
api.setFilterModel({
match: 'all',
items: [{ columnId: 'dept', operator: 'equals', value: 'Dev' }],
}),
save: (api: GridApi<Row>) => api.saveFilterPreset('Dev by salary'),
reset: (api: GridApi<Row>) => {
api.setFilterModel(null);
api.setSortModel([]);
},
applySaved: (api: GridApi<Row>) => {
if (!api.applyFilterPreset('Dev by salary')) return;
const sorted = api.getSortModel().length > 0;
say?.(`Sort ${sorted ? 'restored' : 'none'}`);
},
};