정렬·필터·묶기 전에 막기Block sort, filter and grouping
beforeSort, beforeFilter, beforeGroup은 모델이 바뀌기 직전에 불려요. false를 돌려주면 취소되고 이벤트도 나가지 않아요. 훅이 받는 source로 화면 조작('user')과 코드 호출('api')을 구분하고, model이나 columns로 바뀔 값을 미리 볼 수 있어요.beforeSort, beforeFilter and beforeGroup run just before the model changes. Returning false cancels it and no event is sent. The source tells a gesture ('user') from a code call ('api'), and model or columns shows the value about to be applied.
코드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="toggle">잠금 켜기·끄기</button>
<button type="button" id="sort">코드로 정렬</button>
<button type="button" id="filter">코드로 필터</button>
<button type="button" id="group">코드로 묶기</button>
</div>
<div id="grid" style="height: 400px"></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('toggle').addEventListener('click', () => actions.toggle(api));
document.getElementById('sort').addEventListener('click', () => actions.sort(api));
document.getElementById('filter').addEventListener('click', () => actions.filter(api));
document.getElementById('group').addEventListener('click', () => actions.group(api));const en = document.documentElement.dataset.lang === 'en';
const groups = en ? ['Fruit', 'Veg', 'Dairy'] : ['과일', '채소', '유제품'];
const items = en
? ['Apple', 'Pear', 'Carrot', 'Onion', 'Milk', 'Cheese', 'Grape', 'Kale']
: ['사과', '배', '당근', '양파', '우유', '치즈', '포도', '케일'];
let locked = true;
let say = () => { };
const ask = (ko, enText) => (ctx) => {
say({
ko: `${ko} 요청 (${ctx.source === 'user' ? '화면 조작' : '코드'}) → ${locked ? '막음' : '허용'}`,
en: `${enText} requested (${ctx.source === 'user' ? 'gesture' : 'code'}) -> ${locked ? 'blocked' : 'allowed'}`,
});
return !locked;
};
export const options = {
columns: [
{ id: 'item', title: en ? 'Item' : '품목', width: 120, sortable: true },
{ id: 'group', title: en ? 'Group' : '분류', width: 110, sortable: true, groupable: true },
{ id: 'qty', title: en ? 'Qty' : '수량', kind: 'number', width: 90, sortable: true },
],
rows: items.map((item, i) => ({
id: i + 1,
item,
group: groups[i % 3],
qty: 10 + i * 3,
})),
rowKey: 'id',
hooks: {
beforeSort: ask('정렬', 'Sort'),
beforeFilter: ask('필터', 'Filter'),
beforeGroup: ask('묶기', 'Grouping'),
},
};
export function setup(_api, log) {
say = log;
}
export const actions = {
toggle: () => {
locked = !locked;
say({
ko: locked ? '잠금: 모두 막아요' : '잠금 해제: 모두 허용해요',
en: locked ? 'Locked: everything is blocked' : 'Unlocked: everything is allowed',
});
},
sort: (api) => api.setSortModel([{ columnId: 'qty', direction: 'desc' }]),
filter: (api) => api.setFilterModel({ match: 'all', items: [{ columnId: 'qty', operator: 'gt', value: 20 }] }),
group: (api) => api.setGroupBy(['group']),
};<!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="toggle">잠금 켜기·끄기</button>
<button type="button" id="sort">코드로 정렬</button>
<button type="button" id="filter">코드로 필터</button>
<button type="button" id="group">코드로 묶기</button>
</div>
<div id="grid" style="height: 400px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const en = document.documentElement.dataset.lang === 'en';
const groups = en ? ['Fruit', 'Veg', 'Dairy'] : ['과일', '채소', '유제품'];
const items = en
? ['Apple', 'Pear', 'Carrot', 'Onion', 'Milk', 'Cheese', 'Grape', 'Kale']
: ['사과', '배', '당근', '양파', '우유', '치즈', '포도', '케일'];
let locked = true;
let say = () => { };
const ask = (ko, enText) => (ctx) => {
say({
ko: `${ko} 요청 (${ctx.source === 'user' ? '화면 조작' : '코드'}) → ${locked ? '막음' : '허용'}`,
en: `${enText} requested (${ctx.source === 'user' ? 'gesture' : 'code'}) -> ${locked ? 'blocked' : 'allowed'}`,
});
return !locked;
};
const options = {
columns: [
{ id: 'item', title: en ? 'Item' : '품목', width: 120, sortable: true },
{ id: 'group', title: en ? 'Group' : '분류', width: 110, sortable: true, groupable: true },
{ id: 'qty', title: en ? 'Qty' : '수량', kind: 'number', width: 90, sortable: true },
],
rows: items.map((item, i) => ({
id: i + 1,
item,
group: groups[i % 3],
qty: 10 + i * 3,
})),
rowKey: 'id',
hooks: {
beforeSort: ask('정렬', 'Sort'),
beforeFilter: ask('필터', 'Filter'),
beforeGroup: ask('묶기', 'Grouping'),
},
};
function setup(_api, log) {
say = log;
}
const actions = {
toggle: () => {
locked = !locked;
say({
ko: locked ? '잠금: 모두 막아요' : '잠금 해제: 모두 허용해요',
en: locked ? 'Locked: everything is blocked' : 'Unlocked: everything is allowed',
});
},
sort: (api) => api.setSortModel([{ columnId: 'qty', direction: 'desc' }]),
filter: (api) => api.setFilterModel({ match: 'all', items: [{ columnId: 'qty', operator: 'gt', value: 20 }] }),
group: (api) => api.setGroupBy(['group']),
};
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('toggle').addEventListener('click', () => actions.toggle(api));
document.getElementById('sort').addEventListener('click', () => actions.sort(api));
document.getElementById('filter').addEventListener('click', () => actions.filter(api));
document.getElementById('group').addEventListener('click', () => actions.group(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.toggle(api)}
>잠금 켜기·끄기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.sort(api)}
>코드로 정렬</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.filter(api)}
>코드로 필터</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.group(api)}
>코드로 묶기</button>
</div>
<Grid style={{ height: 400 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
item: string;
group: string;
qty: number;
}
const en = document.documentElement.dataset.lang === 'en';
const groups = en ? ['Fruit', 'Veg', 'Dairy'] : ['과일', '채소', '유제품'];
const items = en
? ['Apple', 'Pear', 'Carrot', 'Onion', 'Milk', 'Cheese', 'Grape', 'Kale']
: ['사과', '배', '당근', '양파', '우유', '치즈', '포도', '케일'];
let locked = true;
let say: (message: LogText) => void = () => {};
const ask =
(ko: string, enText: string) =>
(ctx: { source: string }): boolean => {
say({
ko: `${ko} 요청 (${ctx.source === 'user' ? '화면 조작' : '코드'}) → ${locked ? '막음' : '허용'}`,
en: `${enText} requested (${ctx.source === 'user' ? 'gesture' : 'code'}) -> ${locked ? 'blocked' : 'allowed'}`,
});
return !locked;
};
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: en ? 'Item' : '품목', width: 120, sortable: true },
{ id: 'group', title: en ? 'Group' : '분류', width: 110, sortable: true, groupable: true },
{ id: 'qty', title: en ? 'Qty' : '수량', kind: 'number', width: 90, sortable: true },
],
rows: items.map((item, i) => ({
id: i + 1,
item,
group: groups[i % 3] as string,
qty: 10 + i * 3,
})),
rowKey: 'id',
hooks: {
beforeSort: ask('정렬', 'Sort'),
beforeFilter: ask('필터', 'Filter'),
beforeGroup: ask('묶기', 'Grouping'),
},
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
}
export const actions = {
toggle: () => {
locked = !locked;
say({
ko: locked ? '잠금: 모두 막아요' : '잠금 해제: 모두 허용해요',
en: locked ? 'Locked: everything is blocked' : 'Unlocked: everything is allowed',
});
},
sort: (api: GridApi<Row>) => api.setSortModel([{ columnId: 'qty', direction: 'desc' }]),
filter: (api: GridApi<Row>) =>
api.setFilterModel({ match: 'all', items: [{ columnId: 'qty', operator: 'gt', value: 20 }] }),
group: (api: GridApi<Row>) => api.setGroupBy(['group']),
};버튼을 누르면 이 예제의 코드가 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.toggle(api)">잠금 켜기·끄기</button>
<button type="button" :disabled="!api" @click="api && actions.sort(api)">코드로 정렬</button>
<button type="button" :disabled="!api" @click="api && actions.filter(api)">코드로 필터</button>
<button type="button" :disabled="!api" @click="api && actions.group(api)">코드로 묶기</button>
</div>
<Grid style="height: 400px" :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;
item: string;
group: string;
qty: number;
}
const en = document.documentElement.dataset.lang === 'en';
const groups = en ? ['Fruit', 'Veg', 'Dairy'] : ['과일', '채소', '유제품'];
const items = en
? ['Apple', 'Pear', 'Carrot', 'Onion', 'Milk', 'Cheese', 'Grape', 'Kale']
: ['사과', '배', '당근', '양파', '우유', '치즈', '포도', '케일'];
let locked = true;
let say: (message: LogText) => void = () => {};
const ask =
(ko: string, enText: string) =>
(ctx: { source: string }): boolean => {
say({
ko: `${ko} 요청 (${ctx.source === 'user' ? '화면 조작' : '코드'}) → ${locked ? '막음' : '허용'}`,
en: `${enText} requested (${ctx.source === 'user' ? 'gesture' : 'code'}) -> ${locked ? 'blocked' : 'allowed'}`,
});
return !locked;
};
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: en ? 'Item' : '품목', width: 120, sortable: true },
{ id: 'group', title: en ? 'Group' : '분류', width: 110, sortable: true, groupable: true },
{ id: 'qty', title: en ? 'Qty' : '수량', kind: 'number', width: 90, sortable: true },
],
rows: items.map((item, i) => ({
id: i + 1,
item,
group: groups[i % 3] as string,
qty: 10 + i * 3,
})),
rowKey: 'id',
hooks: {
beforeSort: ask('정렬', 'Sort'),
beforeFilter: ask('필터', 'Filter'),
beforeGroup: ask('묶기', 'Grouping'),
},
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
}
export const actions = {
toggle: () => {
locked = !locked;
say({
ko: locked ? '잠금: 모두 막아요' : '잠금 해제: 모두 허용해요',
en: locked ? 'Locked: everything is blocked' : 'Unlocked: everything is allowed',
});
},
sort: (api: GridApi<Row>) => api.setSortModel([{ columnId: 'qty', direction: 'desc' }]),
filter: (api: GridApi<Row>) =>
api.setFilterModel({ match: 'all', items: [{ columnId: 'qty', operator: 'gt', value: 20 }] }),
group: (api: GridApi<Row>) => api.setGroupBy(['group']),
};버튼을 누르면 이 예제의 코드가 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.toggle(api)"
>잠금 켜기·끄기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.sort(api)"
>코드로 정렬</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.filter(api)"
>코드로 필터</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.group(api)"
>코드로 묶기</button>
</div>
<mx-grid style="height: 400px" [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;
item: string;
group: string;
qty: number;
}
const en = document.documentElement.dataset.lang === 'en';
const groups = en ? ['Fruit', 'Veg', 'Dairy'] : ['과일', '채소', '유제품'];
const items = en
? ['Apple', 'Pear', 'Carrot', 'Onion', 'Milk', 'Cheese', 'Grape', 'Kale']
: ['사과', '배', '당근', '양파', '우유', '치즈', '포도', '케일'];
let locked = true;
let say: (message: LogText) => void = () => {};
const ask =
(ko: string, enText: string) =>
(ctx: { source: string }): boolean => {
say({
ko: `${ko} 요청 (${ctx.source === 'user' ? '화면 조작' : '코드'}) → ${locked ? '막음' : '허용'}`,
en: `${enText} requested (${ctx.source === 'user' ? 'gesture' : 'code'}) -> ${locked ? 'blocked' : 'allowed'}`,
});
return !locked;
};
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: en ? 'Item' : '품목', width: 120, sortable: true },
{ id: 'group', title: en ? 'Group' : '분류', width: 110, sortable: true, groupable: true },
{ id: 'qty', title: en ? 'Qty' : '수량', kind: 'number', width: 90, sortable: true },
],
rows: items.map((item, i) => ({
id: i + 1,
item,
group: groups[i % 3] as string,
qty: 10 + i * 3,
})),
rowKey: 'id',
hooks: {
beforeSort: ask('정렬', 'Sort'),
beforeFilter: ask('필터', 'Filter'),
beforeGroup: ask('묶기', 'Grouping'),
},
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
}
export const actions = {
toggle: () => {
locked = !locked;
say({
ko: locked ? '잠금: 모두 막아요' : '잠금 해제: 모두 허용해요',
en: locked ? 'Locked: everything is blocked' : 'Unlocked: everything is allowed',
});
},
sort: (api: GridApi<Row>) => api.setSortModel([{ columnId: 'qty', direction: 'desc' }]),
filter: (api: GridApi<Row>) =>
api.setFilterModel({ match: 'all', items: [{ columnId: 'qty', operator: 'gt', value: 20 }] }),
group: (api: GridApi<Row>) => api.setGroupBy(['group']),
};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="toggle">Toggle lock</button>
<button type="button" id="sort">Sort from code</button>
<button type="button" id="filter">Filter from code</button>
<button type="button" id="group">Group from code</button>
</div>
<div id="grid" style="height: 400px"></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('toggle').addEventListener('click', () => actions.toggle(api));
document.getElementById('sort').addEventListener('click', () => actions.sort(api));
document.getElementById('filter').addEventListener('click', () => actions.filter(api));
document.getElementById('group').addEventListener('click', () => actions.group(api));const groups = ['Fruit', 'Veg', 'Dairy'];
const items = ['Apple', 'Pear', 'Carrot', 'Onion', 'Milk', 'Cheese', 'Grape', 'Kale'];
let locked = true;
let say = () => { };
const ask = (label) => (ctx) => {
say(`${label} requested (${ctx.source === 'user' ? 'gesture' : 'code'}) -> ${locked ? 'blocked' : 'allowed'}`);
return !locked;
};
export const options = {
columns: [
{ id: 'item', title: 'Item', width: 120, sortable: true },
{ id: 'group', title: 'Group', width: 110, sortable: true, groupable: true },
{ id: 'qty', title: 'Qty', kind: 'number', width: 90, sortable: true },
],
rows: items.map((item, i) => ({
id: i + 1,
item,
group: groups[i % 3],
qty: 10 + i * 3,
})),
rowKey: 'id',
hooks: {
beforeSort: ask('Sort'),
beforeFilter: ask('Filter'),
beforeGroup: ask('Grouping'),
},
};
export function setup(_api, log) {
say = log;
}
export const actions = {
toggle: () => {
locked = !locked;
say(locked ? 'Locked: everything is blocked' : 'Unlocked: everything is allowed');
},
sort: (api) => api.setSortModel([{ columnId: 'qty', direction: 'desc' }]),
filter: (api) => api.setFilterModel({ match: 'all', items: [{ columnId: 'qty', operator: 'gt', value: 20 }] }),
group: (api) => api.setGroupBy(['group']),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Block sort, filter and grouping</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="toggle">Toggle lock</button>
<button type="button" id="sort">Sort from code</button>
<button type="button" id="filter">Filter from code</button>
<button type="button" id="group">Group from code</button>
</div>
<div id="grid" style="height: 400px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const groups = ['Fruit', 'Veg', 'Dairy'];
const items = ['Apple', 'Pear', 'Carrot', 'Onion', 'Milk', 'Cheese', 'Grape', 'Kale'];
let locked = true;
let say = () => { };
const ask = (label) => (ctx) => {
say(`${label} requested (${ctx.source === 'user' ? 'gesture' : 'code'}) -> ${locked ? 'blocked' : 'allowed'}`);
return !locked;
};
const options = {
columns: [
{ id: 'item', title: 'Item', width: 120, sortable: true },
{ id: 'group', title: 'Group', width: 110, sortable: true, groupable: true },
{ id: 'qty', title: 'Qty', kind: 'number', width: 90, sortable: true },
],
rows: items.map((item, i) => ({
id: i + 1,
item,
group: groups[i % 3],
qty: 10 + i * 3,
})),
rowKey: 'id',
hooks: {
beforeSort: ask('Sort'),
beforeFilter: ask('Filter'),
beforeGroup: ask('Grouping'),
},
};
function setup(_api, log) {
say = log;
}
const actions = {
toggle: () => {
locked = !locked;
say(locked ? 'Locked: everything is blocked' : 'Unlocked: everything is allowed');
},
sort: (api) => api.setSortModel([{ columnId: 'qty', direction: 'desc' }]),
filter: (api) => api.setFilterModel({ match: 'all', items: [{ columnId: 'qty', operator: 'gt', value: 20 }] }),
group: (api) => api.setGroupBy(['group']),
};
options.ariaLabel = "Block sort, filter and grouping";
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('toggle').addEventListener('click', () => actions.toggle(api));
document.getElementById('sort').addEventListener('click', () => actions.sort(api));
document.getElementById('filter').addEventListener('click', () => actions.filter(api));
document.getElementById('group').addEventListener('click', () => actions.group(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.toggle(api)}
>Toggle lock</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.sort(api)}
>Sort from code</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.filter(api)}
>Filter from code</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.group(api)}
>Group from code</button>
</div>
<Grid style={{ height: 400 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
item: string;
group: string;
qty: number;
}
const groups = ['Fruit', 'Veg', 'Dairy'];
const items = ['Apple', 'Pear', 'Carrot', 'Onion', 'Milk', 'Cheese', 'Grape', 'Kale'];
let locked = true;
let say: (message: string) => void = () => {};
const ask =
(label: string) =>
(ctx: { source: string }): boolean => {
say(
`${label} requested (${ctx.source === 'user' ? 'gesture' : 'code'}) -> ${locked ? 'blocked' : 'allowed'}`,
);
return !locked;
};
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: 'Item', width: 120, sortable: true },
{ id: 'group', title: 'Group', width: 110, sortable: true, groupable: true },
{ id: 'qty', title: 'Qty', kind: 'number', width: 90, sortable: true },
],
rows: items.map((item, i) => ({
id: i + 1,
item,
group: groups[i % 3] as string,
qty: 10 + i * 3,
})),
rowKey: 'id',
hooks: {
beforeSort: ask('Sort'),
beforeFilter: ask('Filter'),
beforeGroup: ask('Grouping'),
},
};
export function setup(_api: GridApi<Row>, log: (message: string) => void) {
say = log;
}
export const actions = {
toggle: () => {
locked = !locked;
say(locked ? 'Locked: everything is blocked' : 'Unlocked: everything is allowed');
},
sort: (api: GridApi<Row>) => api.setSortModel([{ columnId: 'qty', direction: 'desc' }]),
filter: (api: GridApi<Row>) =>
api.setFilterModel({ match: 'all', items: [{ columnId: 'qty', operator: 'gt', value: 20 }] }),
group: (api: GridApi<Row>) => api.setGroupBy(['group']),
};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.toggle(api)">Toggle lock</button>
<button type="button" :disabled="!api" @click="api && actions.sort(api)">Sort from code</button>
<button type="button" :disabled="!api" @click="api && actions.filter(api)">Filter from code</button>
<button type="button" :disabled="!api" @click="api && actions.group(api)">Group from code</button>
</div>
<Grid style="height: 400px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
item: string;
group: string;
qty: number;
}
const groups = ['Fruit', 'Veg', 'Dairy'];
const items = ['Apple', 'Pear', 'Carrot', 'Onion', 'Milk', 'Cheese', 'Grape', 'Kale'];
let locked = true;
let say: (message: string) => void = () => {};
const ask =
(label: string) =>
(ctx: { source: string }): boolean => {
say(
`${label} requested (${ctx.source === 'user' ? 'gesture' : 'code'}) -> ${locked ? 'blocked' : 'allowed'}`,
);
return !locked;
};
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: 'Item', width: 120, sortable: true },
{ id: 'group', title: 'Group', width: 110, sortable: true, groupable: true },
{ id: 'qty', title: 'Qty', kind: 'number', width: 90, sortable: true },
],
rows: items.map((item, i) => ({
id: i + 1,
item,
group: groups[i % 3] as string,
qty: 10 + i * 3,
})),
rowKey: 'id',
hooks: {
beforeSort: ask('Sort'),
beforeFilter: ask('Filter'),
beforeGroup: ask('Grouping'),
},
};
export function setup(_api: GridApi<Row>, log: (message: string) => void) {
say = log;
}
export const actions = {
toggle: () => {
locked = !locked;
say(locked ? 'Locked: everything is blocked' : 'Unlocked: everything is allowed');
},
sort: (api: GridApi<Row>) => api.setSortModel([{ columnId: 'qty', direction: 'desc' }]),
filter: (api: GridApi<Row>) =>
api.setFilterModel({ match: 'all', items: [{ columnId: 'qty', operator: 'gt', value: 20 }] }),
group: (api: GridApi<Row>) => api.setGroupBy(['group']),
};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.toggle(api)"
>Toggle lock</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.sort(api)"
>Sort from code</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.filter(api)"
>Filter from code</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.group(api)"
>Group from code</button>
</div>
<mx-grid style="height: 400px" [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;
item: string;
group: string;
qty: number;
}
const groups = ['Fruit', 'Veg', 'Dairy'];
const items = ['Apple', 'Pear', 'Carrot', 'Onion', 'Milk', 'Cheese', 'Grape', 'Kale'];
let locked = true;
let say: (message: string) => void = () => {};
const ask =
(label: string) =>
(ctx: { source: string }): boolean => {
say(
`${label} requested (${ctx.source === 'user' ? 'gesture' : 'code'}) -> ${locked ? 'blocked' : 'allowed'}`,
);
return !locked;
};
export const options: GridOptions<Row> = {
columns: [
{ id: 'item', title: 'Item', width: 120, sortable: true },
{ id: 'group', title: 'Group', width: 110, sortable: true, groupable: true },
{ id: 'qty', title: 'Qty', kind: 'number', width: 90, sortable: true },
],
rows: items.map((item, i) => ({
id: i + 1,
item,
group: groups[i % 3] as string,
qty: 10 + i * 3,
})),
rowKey: 'id',
hooks: {
beforeSort: ask('Sort'),
beforeFilter: ask('Filter'),
beforeGroup: ask('Grouping'),
},
};
export function setup(_api: GridApi<Row>, log: (message: string) => void) {
say = log;
}
export const actions = {
toggle: () => {
locked = !locked;
say(locked ? 'Locked: everything is blocked' : 'Unlocked: everything is allowed');
},
sort: (api: GridApi<Row>) => api.setSortModel([{ columnId: 'qty', direction: 'desc' }]),
filter: (api: GridApi<Row>) =>
api.setFilterModel({ match: 'all', items: [{ columnId: 'qty', operator: 'gt', value: 20 }] }),
group: (api: GridApi<Row>) => api.setGroupBy(['group']),
};