표와 바깥 검색 상자·입력 폼 연결하기Link the grid to a search box and form fields
그리드 밖의 검색 상자와 선택 목록을 표와 이어요. 표에서 행을 고르면 어느 직원을 다루는지 바깥에 알리고(그리드 → 폼), 검색 상자는 필터로, 선택 목록은 고른 행의 값으로 되돌려 넣어요(폼 → 그리드).Connect a search box and selects outside the grid. Selecting a row tells the outside which employee is in play (grid to form); the search box becomes a filter and the selects write back into the chosen row (form to grid).
코드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">
<input
type="text"
aria-label="이름 검색"
placeholder="이름 검색 (예: 김)"
id="search"
/>
<select aria-label="부서" id="dept">
<option value="keep" selected>바꾸지 않음 (기본)</option>
<option value="dev">개발팀</option>
<option value="design">디자인팀</option>
<option value="sales">영업팀</option>
</select>
<select aria-label="상태" id="status">
<option value="keep" selected>바꾸지 않음 (기본)</option>
<option value="working">근무</option>
<option value="leave">휴직</option>
<option value="trip">출장</option>
</select>
</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('search').addEventListener('input', (event) => actions.search(api, event.target.value));
document.getElementById('dept').addEventListener('change', (event) => actions.dept(api, event.target.value));
document.getElementById('status').addEventListener('change', (event) => actions.status(api, event.target.value));export const options = {
columns: [
{ id: 'name', title: '이름', width: 130 },
{ id: 'dept', title: '부서', width: 120 },
{ id: 'status', title: '상태', width: 100 },
],
rows: [
{ id: 1, name: '김민준', dept: '개발팀', status: '근무' },
{ id: 2, name: '이서연', dept: '디자인팀', status: '휴직' },
{ id: 3, name: '박지호', dept: '영업팀', status: '근무' },
{ id: 4, name: '최유나', dept: '개발팀', status: '출장' },
{ id: 5, name: '정도윤', dept: '영업팀', status: '근무' },
],
rowKey: 'id',
selection: 'single',
};
let say = () => { };
// 선택 목록은 영문 id를 넘겨요. 표에 쓰는 값과 서로 바꿔요.
const DEPT_VALUES = { dev: '개발팀', design: '디자인팀', sales: '영업팀' };
const STATUS_VALUES = { working: '근무', leave: '휴직', trip: '출장' };
const invert = (map) => Object.fromEntries(Object.entries(map).map(([id, value]) => [value, id]));
const DEPT_IDS = invert(DEPT_VALUES);
const STATUS_IDS = invert(STATUS_VALUES);
const put = (id, value) => {
const el = document.getElementById(id);
if (el instanceof HTMLSelectElement)
el.value = value;
};
// 그리드 → 폼: 선택한 행의 값을 위 선택 목록에 그대로 보여 주고, 기록 창에도 알려 줘요(고른 행이 없으면 '바꾸지 않음').
export function setup(api, log) {
say = log;
return api.on('selectionChanged', ({ selectedKeys }) => {
const row = selectedKeys[0] === undefined ? undefined : api.getRowByKey(selectedKeys[0]);
put('dept', (row && DEPT_IDS[row.dept]) ?? 'keep');
put('status', (row && STATUS_IDS[row.status]) ?? 'keep');
log({
ko: row ? `${row.name}님을 편집 중 (${row.dept}, ${row.status})` : '선택한 직원이 없어요',
en: row ? `Editing ${row.name} (${row.dept}, ${row.status})` : 'No employee selected',
});
});
}
function patch(api, changes) {
const key = api.getSelectedKeys()[0];
const row = key === undefined ? undefined : api.getRowByKey(key);
if (!row) {
put('dept', 'keep');
put('status', 'keep');
return say({
ko: '먼저 표에서 직원을 한 명 골라 주세요',
en: 'Select an employee in the table first',
});
}
api.updateRows([{ ...row, ...changes }]);
say({
ko: `${row.name}님을 바꿨어요 (${changes.dept ?? row.dept}, ${changes.status ?? row.status})`,
en: `Changed ${row.name} (${changes.dept ?? row.dept}, ${changes.status ?? row.status})`,
});
}
// 폼 → 그리드: 검색 상자는 필터로, 아래 선택 목록은 고른 행의 값을 바꿔요.
export const actions = {
search: (api, value) => {
const text = (value ?? '').trim();
api.setFilterModel(text
? { match: 'all', items: [{ columnId: 'name', operator: 'contains', value: text }] }
: null);
},
dept: (api, value) => {
if (value && value !== 'keep')
patch(api, { dept: DEPT_VALUES[value] ?? value });
},
status: (api, value) => {
if (value && value !== 'keep')
patch(api, { status: STATUS_VALUES[value] ?? value });
},
};<!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">
<input
type="text"
aria-label="이름 검색"
placeholder="이름 검색 (예: 김)"
id="search"
/>
<select aria-label="부서" id="dept">
<option value="keep" selected>바꾸지 않음 (기본)</option>
<option value="dev">개발팀</option>
<option value="design">디자인팀</option>
<option value="sales">영업팀</option>
</select>
<select aria-label="상태" id="status">
<option value="keep" selected>바꾸지 않음 (기본)</option>
<option value="working">근무</option>
<option value="leave">휴직</option>
<option value="trip">출장</option>
</select>
</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 options = {
columns: [
{ id: 'name', title: '이름', width: 130 },
{ id: 'dept', title: '부서', width: 120 },
{ id: 'status', title: '상태', width: 100 },
],
rows: [
{ id: 1, name: '김민준', dept: '개발팀', status: '근무' },
{ id: 2, name: '이서연', dept: '디자인팀', status: '휴직' },
{ id: 3, name: '박지호', dept: '영업팀', status: '근무' },
{ id: 4, name: '최유나', dept: '개발팀', status: '출장' },
{ id: 5, name: '정도윤', dept: '영업팀', status: '근무' },
],
rowKey: 'id',
selection: 'single',
};
let say = () => { };
// 선택 목록은 영문 id를 넘겨요. 표에 쓰는 값과 서로 바꿔요.
const DEPT_VALUES = { dev: '개발팀', design: '디자인팀', sales: '영업팀' };
const STATUS_VALUES = { working: '근무', leave: '휴직', trip: '출장' };
const invert = (map) => Object.fromEntries(Object.entries(map).map(([id, value]) => [value, id]));
const DEPT_IDS = invert(DEPT_VALUES);
const STATUS_IDS = invert(STATUS_VALUES);
const put = (id, value) => {
const el = document.getElementById(id);
if (el instanceof HTMLSelectElement)
el.value = value;
};
// 그리드 → 폼: 선택한 행의 값을 위 선택 목록에 그대로 보여 주고, 기록 창에도 알려 줘요(고른 행이 없으면 '바꾸지 않음').
function setup(api, log) {
say = log;
return api.on('selectionChanged', ({ selectedKeys }) => {
const row = selectedKeys[0] === undefined ? undefined : api.getRowByKey(selectedKeys[0]);
put('dept', (row && DEPT_IDS[row.dept]) ?? 'keep');
put('status', (row && STATUS_IDS[row.status]) ?? 'keep');
log({
ko: row ? `${row.name}님을 편집 중 (${row.dept}, ${row.status})` : '선택한 직원이 없어요',
en: row ? `Editing ${row.name} (${row.dept}, ${row.status})` : 'No employee selected',
});
});
}
function patch(api, changes) {
const key = api.getSelectedKeys()[0];
const row = key === undefined ? undefined : api.getRowByKey(key);
if (!row) {
put('dept', 'keep');
put('status', 'keep');
return say({
ko: '먼저 표에서 직원을 한 명 골라 주세요',
en: 'Select an employee in the table first',
});
}
api.updateRows([{ ...row, ...changes }]);
say({
ko: `${row.name}님을 바꿨어요 (${changes.dept ?? row.dept}, ${changes.status ?? row.status})`,
en: `Changed ${row.name} (${changes.dept ?? row.dept}, ${changes.status ?? row.status})`,
});
}
// 폼 → 그리드: 검색 상자는 필터로, 아래 선택 목록은 고른 행의 값을 바꿔요.
const actions = {
search: (api, value) => {
const text = (value ?? '').trim();
api.setFilterModel(text
? { match: 'all', items: [{ columnId: 'name', operator: 'contains', value: text }] }
: null);
},
dept: (api, value) => {
if (value && value !== 'keep')
patch(api, { dept: DEPT_VALUES[value] ?? value });
},
status: (api, value) => {
if (value && value !== 'keep')
patch(api, { status: STATUS_VALUES[value] ?? value });
},
};
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('search').addEventListener('input', (event) => actions.search(api, event.target.value));
document.getElementById('dept').addEventListener('change', (event) => actions.dept(api, event.target.value));
document.getElementById('status').addEventListener('change', (event) => actions.status(api, event.target.value));
</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">
<input
type="text"
aria-label="이름 검색"
placeholder="이름 검색 (예: 김)"
disabled={!api}
onChange={(event) => api && actions.search(api, event.target.value)}
/>
<select
aria-label="부서"
disabled={!api}
onChange={(event) => api && actions.dept(api, event.target.value)}
>
<option value="keep">바꾸지 않음 (기본)</option>
<option value="dev">개발팀</option>
<option value="design">디자인팀</option>
<option value="sales">영업팀</option>
</select>
<select defaultValue="keep"
aria-label="상태"
disabled={!api}
onChange={(event) => api && actions.status(api, event.target.value)}
>
<option value="keep">바꾸지 않음 (기본)</option>
<option value="working">근무</option>
<option value="leave">휴직</option>
<option value="trip">출장</option>
</select>
</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;
dept: string;
status: string;
}
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: '이름', width: 130 },
{ id: 'dept', title: '부서', width: 120 },
{ id: 'status', title: '상태', width: 100 },
],
rows: [
{ id: 1, name: '김민준', dept: '개발팀', status: '근무' },
{ id: 2, name: '이서연', dept: '디자인팀', status: '휴직' },
{ id: 3, name: '박지호', dept: '영업팀', status: '근무' },
{ id: 4, name: '최유나', dept: '개발팀', status: '출장' },
{ id: 5, name: '정도윤', dept: '영업팀', status: '근무' },
],
rowKey: 'id',
selection: 'single',
};
let say: (message: LogText) => void = () => {};
// 선택 목록은 영문 id를 넘겨요. 표에 쓰는 값과 서로 바꿔요.
const DEPT_VALUES: Record<string, string> = { dev: '개발팀', design: '디자인팀', sales: '영업팀' };
const STATUS_VALUES: Record<string, string> = { working: '근무', leave: '휴직', trip: '출장' };
const invert = (map: Record<string, string>): Record<string, string> =>
Object.fromEntries(Object.entries(map).map(([id, value]) => [value, id]));
const DEPT_IDS = invert(DEPT_VALUES);
const STATUS_IDS = invert(STATUS_VALUES);
const put = (id: string, value: string): void => {
const el = document.getElementById(id);
if (el instanceof HTMLSelectElement) el.value = value;
};
// 그리드 → 폼: 선택한 행의 값을 위 선택 목록에 그대로 보여 주고, 기록 창에도 알려 줘요(고른 행이 없으면 '바꾸지 않음').
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return api.on('selectionChanged', ({ selectedKeys }) => {
const row = selectedKeys[0] === undefined ? undefined : api.getRowByKey(selectedKeys[0]);
put('dept', (row && DEPT_IDS[row.dept]) ?? 'keep');
put('status', (row && STATUS_IDS[row.status]) ?? 'keep');
log({
ko: row ? `${row.name}님을 편집 중 (${row.dept}, ${row.status})` : '선택한 직원이 없어요',
en: row ? `Editing ${row.name} (${row.dept}, ${row.status})` : 'No employee selected',
});
});
}
function patch(api: GridApi<Row>, changes: Partial<Row>): void {
const key = api.getSelectedKeys()[0];
const row = key === undefined ? undefined : api.getRowByKey(key);
if (!row) {
put('dept', 'keep');
put('status', 'keep');
return say({
ko: '먼저 표에서 직원을 한 명 골라 주세요',
en: 'Select an employee in the table first',
});
}
api.updateRows([{ ...row, ...changes }]);
say({
ko: `${row.name}님을 바꿨어요 (${changes.dept ?? row.dept}, ${changes.status ?? row.status})`,
en: `Changed ${row.name} (${changes.dept ?? row.dept}, ${changes.status ?? row.status})`,
});
}
// 폼 → 그리드: 검색 상자는 필터로, 아래 선택 목록은 고른 행의 값을 바꿔요.
export const actions = {
search: (api: GridApi<Row>, value?: string) => {
const text = (value ?? '').trim();
api.setFilterModel(
text
? { match: 'all', items: [{ columnId: 'name', operator: 'contains', value: text }] }
: null,
);
},
dept: (api: GridApi<Row>, value?: string) => {
if (value && value !== 'keep') patch(api, { dept: DEPT_VALUES[value] ?? value });
},
status: (api: GridApi<Row>, value?: string) => {
if (value && value !== 'keep') patch(api, { status: STATUS_VALUES[value] ?? value });
},
};버튼을 누르면 이 예제의 코드가 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">
<input
type="text"
aria-label="이름 검색"
placeholder="이름 검색 (예: 김)"
:disabled="!api"
@input="api && actions.search(api, ($event.target as HTMLInputElement).value)"
/>
<select
aria-label="부서"
:disabled="!api"
@change="api && actions.dept(api, ($event.target as HTMLSelectElement).value)"
>
<option value="keep" selected>바꾸지 않음 (기본)</option>
<option value="dev">개발팀</option>
<option value="design">디자인팀</option>
<option value="sales">영업팀</option>
</select>
<select
aria-label="상태"
:disabled="!api"
@change="api && actions.status(api, ($event.target as HTMLSelectElement).value)"
>
<option value="keep" selected>바꾸지 않음 (기본)</option>
<option value="working">근무</option>
<option value="leave">휴직</option>
<option value="trip">출장</option>
</select>
</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;
dept: string;
status: string;
}
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: '이름', width: 130 },
{ id: 'dept', title: '부서', width: 120 },
{ id: 'status', title: '상태', width: 100 },
],
rows: [
{ id: 1, name: '김민준', dept: '개발팀', status: '근무' },
{ id: 2, name: '이서연', dept: '디자인팀', status: '휴직' },
{ id: 3, name: '박지호', dept: '영업팀', status: '근무' },
{ id: 4, name: '최유나', dept: '개발팀', status: '출장' },
{ id: 5, name: '정도윤', dept: '영업팀', status: '근무' },
],
rowKey: 'id',
selection: 'single',
};
let say: (message: LogText) => void = () => {};
// 선택 목록은 영문 id를 넘겨요. 표에 쓰는 값과 서로 바꿔요.
const DEPT_VALUES: Record<string, string> = { dev: '개발팀', design: '디자인팀', sales: '영업팀' };
const STATUS_VALUES: Record<string, string> = { working: '근무', leave: '휴직', trip: '출장' };
const invert = (map: Record<string, string>): Record<string, string> =>
Object.fromEntries(Object.entries(map).map(([id, value]) => [value, id]));
const DEPT_IDS = invert(DEPT_VALUES);
const STATUS_IDS = invert(STATUS_VALUES);
const put = (id: string, value: string): void => {
const el = document.getElementById(id);
if (el instanceof HTMLSelectElement) el.value = value;
};
// 그리드 → 폼: 선택한 행의 값을 위 선택 목록에 그대로 보여 주고, 기록 창에도 알려 줘요(고른 행이 없으면 '바꾸지 않음').
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return api.on('selectionChanged', ({ selectedKeys }) => {
const row = selectedKeys[0] === undefined ? undefined : api.getRowByKey(selectedKeys[0]);
put('dept', (row && DEPT_IDS[row.dept]) ?? 'keep');
put('status', (row && STATUS_IDS[row.status]) ?? 'keep');
log({
ko: row ? `${row.name}님을 편집 중 (${row.dept}, ${row.status})` : '선택한 직원이 없어요',
en: row ? `Editing ${row.name} (${row.dept}, ${row.status})` : 'No employee selected',
});
});
}
function patch(api: GridApi<Row>, changes: Partial<Row>): void {
const key = api.getSelectedKeys()[0];
const row = key === undefined ? undefined : api.getRowByKey(key);
if (!row) {
put('dept', 'keep');
put('status', 'keep');
return say({
ko: '먼저 표에서 직원을 한 명 골라 주세요',
en: 'Select an employee in the table first',
});
}
api.updateRows([{ ...row, ...changes }]);
say({
ko: `${row.name}님을 바꿨어요 (${changes.dept ?? row.dept}, ${changes.status ?? row.status})`,
en: `Changed ${row.name} (${changes.dept ?? row.dept}, ${changes.status ?? row.status})`,
});
}
// 폼 → 그리드: 검색 상자는 필터로, 아래 선택 목록은 고른 행의 값을 바꿔요.
export const actions = {
search: (api: GridApi<Row>, value?: string) => {
const text = (value ?? '').trim();
api.setFilterModel(
text
? { match: 'all', items: [{ columnId: 'name', operator: 'contains', value: text }] }
: null,
);
},
dept: (api: GridApi<Row>, value?: string) => {
if (value && value !== 'keep') patch(api, { dept: DEPT_VALUES[value] ?? value });
},
status: (api: GridApi<Row>, value?: string) => {
if (value && value !== 'keep') patch(api, { status: STATUS_VALUES[value] ?? value });
},
};버튼을 누르면 이 예제의 코드가 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">
<input
type="text"
aria-label="이름 검색"
placeholder="이름 검색 (예: 김)"
[disabled]="!api"
(input)="api && actions.search(api, $any($event.target).value)"
/>
<select
aria-label="부서"
[disabled]="!api"
(change)="api && actions.dept(api, $any($event.target).value)"
>
<option value="keep" selected>바꾸지 않음 (기본)</option>
<option value="dev">개발팀</option>
<option value="design">디자인팀</option>
<option value="sales">영업팀</option>
</select>
<select
aria-label="상태"
[disabled]="!api"
(change)="api && actions.status(api, $any($event.target).value)"
>
<option value="keep" selected>바꾸지 않음 (기본)</option>
<option value="working">근무</option>
<option value="leave">휴직</option>
<option value="trip">출장</option>
</select>
</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;
dept: string;
status: string;
}
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: '이름', width: 130 },
{ id: 'dept', title: '부서', width: 120 },
{ id: 'status', title: '상태', width: 100 },
],
rows: [
{ id: 1, name: '김민준', dept: '개발팀', status: '근무' },
{ id: 2, name: '이서연', dept: '디자인팀', status: '휴직' },
{ id: 3, name: '박지호', dept: '영업팀', status: '근무' },
{ id: 4, name: '최유나', dept: '개발팀', status: '출장' },
{ id: 5, name: '정도윤', dept: '영업팀', status: '근무' },
],
rowKey: 'id',
selection: 'single',
};
let say: (message: LogText) => void = () => {};
// 선택 목록은 영문 id를 넘겨요. 표에 쓰는 값과 서로 바꿔요.
const DEPT_VALUES: Record<string, string> = { dev: '개발팀', design: '디자인팀', sales: '영업팀' };
const STATUS_VALUES: Record<string, string> = { working: '근무', leave: '휴직', trip: '출장' };
const invert = (map: Record<string, string>): Record<string, string> =>
Object.fromEntries(Object.entries(map).map(([id, value]) => [value, id]));
const DEPT_IDS = invert(DEPT_VALUES);
const STATUS_IDS = invert(STATUS_VALUES);
const put = (id: string, value: string): void => {
const el = document.getElementById(id);
if (el instanceof HTMLSelectElement) el.value = value;
};
// 그리드 → 폼: 선택한 행의 값을 위 선택 목록에 그대로 보여 주고, 기록 창에도 알려 줘요(고른 행이 없으면 '바꾸지 않음').
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return api.on('selectionChanged', ({ selectedKeys }) => {
const row = selectedKeys[0] === undefined ? undefined : api.getRowByKey(selectedKeys[0]);
put('dept', (row && DEPT_IDS[row.dept]) ?? 'keep');
put('status', (row && STATUS_IDS[row.status]) ?? 'keep');
log({
ko: row ? `${row.name}님을 편집 중 (${row.dept}, ${row.status})` : '선택한 직원이 없어요',
en: row ? `Editing ${row.name} (${row.dept}, ${row.status})` : 'No employee selected',
});
});
}
function patch(api: GridApi<Row>, changes: Partial<Row>): void {
const key = api.getSelectedKeys()[0];
const row = key === undefined ? undefined : api.getRowByKey(key);
if (!row) {
put('dept', 'keep');
put('status', 'keep');
return say({
ko: '먼저 표에서 직원을 한 명 골라 주세요',
en: 'Select an employee in the table first',
});
}
api.updateRows([{ ...row, ...changes }]);
say({
ko: `${row.name}님을 바꿨어요 (${changes.dept ?? row.dept}, ${changes.status ?? row.status})`,
en: `Changed ${row.name} (${changes.dept ?? row.dept}, ${changes.status ?? row.status})`,
});
}
// 폼 → 그리드: 검색 상자는 필터로, 아래 선택 목록은 고른 행의 값을 바꿔요.
export const actions = {
search: (api: GridApi<Row>, value?: string) => {
const text = (value ?? '').trim();
api.setFilterModel(
text
? { match: 'all', items: [{ columnId: 'name', operator: 'contains', value: text }] }
: null,
);
},
dept: (api: GridApi<Row>, value?: string) => {
if (value && value !== 'keep') patch(api, { dept: DEPT_VALUES[value] ?? value });
},
status: (api: GridApi<Row>, value?: string) => {
if (value && value !== 'keep') patch(api, { status: STATUS_VALUES[value] ?? value });
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<div id="app">
<div class="toolbar">
<input
type="text"
aria-label="Name search"
placeholder="Name search (e.g. Liam)"
id="search"
/>
<select aria-label="Department" id="dept">
<option value="keep" selected>Keep (default)</option>
<option value="dev">Development</option>
<option value="design">Design</option>
<option value="sales">Sales</option>
</select>
<select aria-label="Status" id="status">
<option value="keep" selected>Keep (default)</option>
<option value="working">Working</option>
<option value="leave">On leave</option>
<option value="trip">Business trip</option>
</select>
</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('search').addEventListener('input', (event) => actions.search(api, event.target.value));
document.getElementById('dept').addEventListener('change', (event) => actions.dept(api, event.target.value));
document.getElementById('status').addEventListener('change', (event) => actions.status(api, event.target.value));export const options = {
columns: [
{ id: 'name', title: 'Name', width: 130 },
{ id: 'dept', title: 'Department', width: 120 },
{ id: 'status', title: 'Status', width: 100 },
],
rows: [
{ id: 1, name: 'Liam Carter', dept: 'Dev', status: 'Working' },
{ id: 2, name: 'Emma Stone', dept: 'Design', status: 'On leave' },
{ id: 3, name: 'Noah Reed', dept: 'Sales', status: 'Working' },
{ id: 4, name: 'Olivia Hayes', dept: 'Dev', status: 'Trip' },
{ id: 5, name: 'Ethan Brooks', dept: 'Sales', status: 'Working' },
],
rowKey: 'id',
selection: 'single',
};
let say = () => { };
// The select lists pass ASCII ids, so map them to the cell values used in the table and back.
const DEPT_VALUES = { dev: 'Dev', design: 'Design', sales: 'Sales' };
const STATUS_VALUES = {
working: 'Working',
leave: 'On leave',
trip: 'Trip',
};
const invert = (map) => Object.fromEntries(Object.entries(map).map(([id, value]) => [value, id]));
const DEPT_IDS = invert(DEPT_VALUES);
const STATUS_IDS = invert(STATUS_VALUES);
const put = (id, value) => {
const el = document.getElementById(id);
if (el instanceof HTMLSelectElement)
el.value = value;
};
// Grid -> form: show the selected row's values in the select lists above and tell the log ('keep' when no row is selected).
export function setup(api, log) {
say = log;
return api.on('selectionChanged', ({ selectedKeys }) => {
const row = selectedKeys[0] === undefined ? undefined : api.getRowByKey(selectedKeys[0]);
put('dept', (row && DEPT_IDS[row.dept]) ?? 'keep');
put('status', (row && STATUS_IDS[row.status]) ?? 'keep');
log(row ? `Editing ${row.name} (${row.dept}, ${row.status})` : 'No employee selected');
});
}
function patch(api, changes) {
const key = api.getSelectedKeys()[0];
const row = key === undefined ? undefined : api.getRowByKey(key);
if (!row) {
put('dept', 'keep');
put('status', 'keep');
return say('Select an employee in the table first');
}
api.updateRows([{ ...row, ...changes }]);
say(`Changed ${row.name} (${changes.dept ?? row.dept}, ${changes.status ?? row.status})`);
}
// Form -> grid: the search box becomes a filter, and the select lists below change the selected row's values.
export const actions = {
search: (api, value) => {
const text = (value ?? '').trim();
api.setFilterModel(text
? { match: 'all', items: [{ columnId: 'name', operator: 'contains', value: text }] }
: null);
},
dept: (api, value) => {
if (value && value !== 'keep')
patch(api, { dept: DEPT_VALUES[value] ?? value });
},
status: (api, value) => {
if (value && value !== 'keep')
patch(api, { status: STATUS_VALUES[value] ?? value });
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Link the grid to a search box and form fields</title>
</head>
<body>
<div class="toolbar">
<input
type="text"
aria-label="Name search"
placeholder="Name search (e.g. Liam)"
id="search"
/>
<select aria-label="Department" id="dept">
<option value="keep" selected>Keep (default)</option>
<option value="dev">Development</option>
<option value="design">Design</option>
<option value="sales">Sales</option>
</select>
<select aria-label="Status" id="status">
<option value="keep" selected>Keep (default)</option>
<option value="working">Working</option>
<option value="leave">On leave</option>
<option value="trip">Business trip</option>
</select>
</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 options = {
columns: [
{ id: 'name', title: 'Name', width: 130 },
{ id: 'dept', title: 'Department', width: 120 },
{ id: 'status', title: 'Status', width: 100 },
],
rows: [
{ id: 1, name: 'Liam Carter', dept: 'Dev', status: 'Working' },
{ id: 2, name: 'Emma Stone', dept: 'Design', status: 'On leave' },
{ id: 3, name: 'Noah Reed', dept: 'Sales', status: 'Working' },
{ id: 4, name: 'Olivia Hayes', dept: 'Dev', status: 'Trip' },
{ id: 5, name: 'Ethan Brooks', dept: 'Sales', status: 'Working' },
],
rowKey: 'id',
selection: 'single',
};
let say = () => { };
// The select lists pass ASCII ids, so map them to the cell values used in the table and back.
const DEPT_VALUES = { dev: 'Dev', design: 'Design', sales: 'Sales' };
const STATUS_VALUES = {
working: 'Working',
leave: 'On leave',
trip: 'Trip',
};
const invert = (map) => Object.fromEntries(Object.entries(map).map(([id, value]) => [value, id]));
const DEPT_IDS = invert(DEPT_VALUES);
const STATUS_IDS = invert(STATUS_VALUES);
const put = (id, value) => {
const el = document.getElementById(id);
if (el instanceof HTMLSelectElement)
el.value = value;
};
// Grid -> form: show the selected row's values in the select lists above and tell the log ('keep' when no row is selected).
function setup(api, log) {
say = log;
return api.on('selectionChanged', ({ selectedKeys }) => {
const row = selectedKeys[0] === undefined ? undefined : api.getRowByKey(selectedKeys[0]);
put('dept', (row && DEPT_IDS[row.dept]) ?? 'keep');
put('status', (row && STATUS_IDS[row.status]) ?? 'keep');
log(row ? `Editing ${row.name} (${row.dept}, ${row.status})` : 'No employee selected');
});
}
function patch(api, changes) {
const key = api.getSelectedKeys()[0];
const row = key === undefined ? undefined : api.getRowByKey(key);
if (!row) {
put('dept', 'keep');
put('status', 'keep');
return say('Select an employee in the table first');
}
api.updateRows([{ ...row, ...changes }]);
say(`Changed ${row.name} (${changes.dept ?? row.dept}, ${changes.status ?? row.status})`);
}
// Form -> grid: the search box becomes a filter, and the select lists below change the selected row's values.
const actions = {
search: (api, value) => {
const text = (value ?? '').trim();
api.setFilterModel(text
? { match: 'all', items: [{ columnId: 'name', operator: 'contains', value: text }] }
: null);
},
dept: (api, value) => {
if (value && value !== 'keep')
patch(api, { dept: DEPT_VALUES[value] ?? value });
},
status: (api, value) => {
if (value && value !== 'keep')
patch(api, { status: STATUS_VALUES[value] ?? value });
},
};
options.ariaLabel = "Link the grid to a search box and form fields";
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('search').addEventListener('input', (event) => actions.search(api, event.target.value));
document.getElementById('dept').addEventListener('change', (event) => actions.dept(api, event.target.value));
document.getElementById('status').addEventListener('change', (event) => actions.status(api, event.target.value));
</script>
</body>
</html>When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { 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">
<input
type="text"
aria-label="Name search"
placeholder="Name search (e.g. Liam)"
disabled={!api}
onChange={(event) => api && actions.search(api, event.target.value)}
/>
<select
aria-label="Department"
disabled={!api}
onChange={(event) => api && actions.dept(api, event.target.value)}
>
<option value="keep">Keep (default)</option>
<option value="dev">Development</option>
<option value="design">Design</option>
<option value="sales">Sales</option>
</select>
<select defaultValue="keep"
aria-label="Status"
disabled={!api}
onChange={(event) => api && actions.status(api, event.target.value)}
>
<option value="keep">Keep (default)</option>
<option value="working">Working</option>
<option value="leave">On leave</option>
<option value="trip">Business trip</option>
</select>
</div>
<Grid style={{ height: 330 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
name: string;
dept: string;
status: string;
}
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: 'Name', width: 130 },
{ id: 'dept', title: 'Department', width: 120 },
{ id: 'status', title: 'Status', width: 100 },
],
rows: [
{ id: 1, name: 'Liam Carter', dept: 'Dev', status: 'Working' },
{ id: 2, name: 'Emma Stone', dept: 'Design', status: 'On leave' },
{ id: 3, name: 'Noah Reed', dept: 'Sales', status: 'Working' },
{ id: 4, name: 'Olivia Hayes', dept: 'Dev', status: 'Trip' },
{ id: 5, name: 'Ethan Brooks', dept: 'Sales', status: 'Working' },
],
rowKey: 'id',
selection: 'single',
};
let say: (message: LogText) => void = () => {};
// The select lists pass ASCII ids, so map them to the cell values used in the table and back.
const DEPT_VALUES: Record<string, string> = { dev: 'Dev', design: 'Design', sales: 'Sales' };
const STATUS_VALUES: Record<string, string> = {
working: 'Working',
leave: 'On leave',
trip: 'Trip',
};
const invert = (map: Record<string, string>): Record<string, string> =>
Object.fromEntries(Object.entries(map).map(([id, value]) => [value, id]));
const DEPT_IDS = invert(DEPT_VALUES);
const STATUS_IDS = invert(STATUS_VALUES);
const put = (id: string, value: string): void => {
const el = document.getElementById(id);
if (el instanceof HTMLSelectElement) el.value = value;
};
// Grid -> form: show the selected row's values in the select lists above and tell the log ('keep' when no row is selected).
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return api.on('selectionChanged', ({ selectedKeys }) => {
const row = selectedKeys[0] === undefined ? undefined : api.getRowByKey(selectedKeys[0]);
put('dept', (row && DEPT_IDS[row.dept]) ?? 'keep');
put('status', (row && STATUS_IDS[row.status]) ?? 'keep');
log(row ? `Editing ${row.name} (${row.dept}, ${row.status})` : 'No employee selected');
});
}
function patch(api: GridApi<Row>, changes: Partial<Row>): void {
const key = api.getSelectedKeys()[0];
const row = key === undefined ? undefined : api.getRowByKey(key);
if (!row) {
put('dept', 'keep');
put('status', 'keep');
return say('Select an employee in the table first');
}
api.updateRows([{ ...row, ...changes }]);
say(`Changed ${row.name} (${changes.dept ?? row.dept}, ${changes.status ?? row.status})`);
}
// Form -> grid: the search box becomes a filter, and the select lists below change the selected row's values.
export const actions = {
search: (api: GridApi<Row>, value?: string) => {
const text = (value ?? '').trim();
api.setFilterModel(
text
? { match: 'all', items: [{ columnId: 'name', operator: 'contains', value: text }] }
: null,
);
},
dept: (api: GridApi<Row>, value?: string) => {
if (value && value !== 'keep') patch(api, { dept: DEPT_VALUES[value] ?? value });
},
status: (api: GridApi<Row>, value?: string) => {
if (value && value !== 'keep') patch(api, { status: STATUS_VALUES[value] ?? value });
},
};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">
<input
type="text"
aria-label="Name search"
placeholder="Name search (e.g. Liam)"
:disabled="!api"
@input="api && actions.search(api, ($event.target as HTMLInputElement).value)"
/>
<select
aria-label="Department"
:disabled="!api"
@change="api && actions.dept(api, ($event.target as HTMLSelectElement).value)"
>
<option value="keep" selected>Keep (default)</option>
<option value="dev">Development</option>
<option value="design">Design</option>
<option value="sales">Sales</option>
</select>
<select
aria-label="Status"
:disabled="!api"
@change="api && actions.status(api, ($event.target as HTMLSelectElement).value)"
>
<option value="keep" selected>Keep (default)</option>
<option value="working">Working</option>
<option value="leave">On leave</option>
<option value="trip">Business trip</option>
</select>
</div>
<Grid style="height: 330px" :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;
status: string;
}
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: 'Name', width: 130 },
{ id: 'dept', title: 'Department', width: 120 },
{ id: 'status', title: 'Status', width: 100 },
],
rows: [
{ id: 1, name: 'Liam Carter', dept: 'Dev', status: 'Working' },
{ id: 2, name: 'Emma Stone', dept: 'Design', status: 'On leave' },
{ id: 3, name: 'Noah Reed', dept: 'Sales', status: 'Working' },
{ id: 4, name: 'Olivia Hayes', dept: 'Dev', status: 'Trip' },
{ id: 5, name: 'Ethan Brooks', dept: 'Sales', status: 'Working' },
],
rowKey: 'id',
selection: 'single',
};
let say: (message: LogText) => void = () => {};
// The select lists pass ASCII ids, so map them to the cell values used in the table and back.
const DEPT_VALUES: Record<string, string> = { dev: 'Dev', design: 'Design', sales: 'Sales' };
const STATUS_VALUES: Record<string, string> = {
working: 'Working',
leave: 'On leave',
trip: 'Trip',
};
const invert = (map: Record<string, string>): Record<string, string> =>
Object.fromEntries(Object.entries(map).map(([id, value]) => [value, id]));
const DEPT_IDS = invert(DEPT_VALUES);
const STATUS_IDS = invert(STATUS_VALUES);
const put = (id: string, value: string): void => {
const el = document.getElementById(id);
if (el instanceof HTMLSelectElement) el.value = value;
};
// Grid -> form: show the selected row's values in the select lists above and tell the log ('keep' when no row is selected).
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return api.on('selectionChanged', ({ selectedKeys }) => {
const row = selectedKeys[0] === undefined ? undefined : api.getRowByKey(selectedKeys[0]);
put('dept', (row && DEPT_IDS[row.dept]) ?? 'keep');
put('status', (row && STATUS_IDS[row.status]) ?? 'keep');
log(row ? `Editing ${row.name} (${row.dept}, ${row.status})` : 'No employee selected');
});
}
function patch(api: GridApi<Row>, changes: Partial<Row>): void {
const key = api.getSelectedKeys()[0];
const row = key === undefined ? undefined : api.getRowByKey(key);
if (!row) {
put('dept', 'keep');
put('status', 'keep');
return say('Select an employee in the table first');
}
api.updateRows([{ ...row, ...changes }]);
say(`Changed ${row.name} (${changes.dept ?? row.dept}, ${changes.status ?? row.status})`);
}
// Form -> grid: the search box becomes a filter, and the select lists below change the selected row's values.
export const actions = {
search: (api: GridApi<Row>, value?: string) => {
const text = (value ?? '').trim();
api.setFilterModel(
text
? { match: 'all', items: [{ columnId: 'name', operator: 'contains', value: text }] }
: null,
);
},
dept: (api: GridApi<Row>, value?: string) => {
if (value && value !== 'keep') patch(api, { dept: DEPT_VALUES[value] ?? value });
},
status: (api: GridApi<Row>, value?: string) => {
if (value && value !== 'keep') patch(api, { status: STATUS_VALUES[value] ?? value });
},
};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">
<input
type="text"
aria-label="Name search"
placeholder="Name search (e.g. Liam)"
[disabled]="!api"
(input)="api && actions.search(api, $any($event.target).value)"
/>
<select
aria-label="Department"
[disabled]="!api"
(change)="api && actions.dept(api, $any($event.target).value)"
>
<option value="keep" selected>Keep (default)</option>
<option value="dev">Development</option>
<option value="design">Design</option>
<option value="sales">Sales</option>
</select>
<select
aria-label="Status"
[disabled]="!api"
(change)="api && actions.status(api, $any($event.target).value)"
>
<option value="keep" selected>Keep (default)</option>
<option value="working">Working</option>
<option value="leave">On leave</option>
<option value="trip">Business trip</option>
</select>
</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;
export interface Row {
id: number;
name: string;
dept: string;
status: string;
}
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: 'Name', width: 130 },
{ id: 'dept', title: 'Department', width: 120 },
{ id: 'status', title: 'Status', width: 100 },
],
rows: [
{ id: 1, name: 'Liam Carter', dept: 'Dev', status: 'Working' },
{ id: 2, name: 'Emma Stone', dept: 'Design', status: 'On leave' },
{ id: 3, name: 'Noah Reed', dept: 'Sales', status: 'Working' },
{ id: 4, name: 'Olivia Hayes', dept: 'Dev', status: 'Trip' },
{ id: 5, name: 'Ethan Brooks', dept: 'Sales', status: 'Working' },
],
rowKey: 'id',
selection: 'single',
};
let say: (message: LogText) => void = () => {};
// The select lists pass ASCII ids, so map them to the cell values used in the table and back.
const DEPT_VALUES: Record<string, string> = { dev: 'Dev', design: 'Design', sales: 'Sales' };
const STATUS_VALUES: Record<string, string> = {
working: 'Working',
leave: 'On leave',
trip: 'Trip',
};
const invert = (map: Record<string, string>): Record<string, string> =>
Object.fromEntries(Object.entries(map).map(([id, value]) => [value, id]));
const DEPT_IDS = invert(DEPT_VALUES);
const STATUS_IDS = invert(STATUS_VALUES);
const put = (id: string, value: string): void => {
const el = document.getElementById(id);
if (el instanceof HTMLSelectElement) el.value = value;
};
// Grid -> form: show the selected row's values in the select lists above and tell the log ('keep' when no row is selected).
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
return api.on('selectionChanged', ({ selectedKeys }) => {
const row = selectedKeys[0] === undefined ? undefined : api.getRowByKey(selectedKeys[0]);
put('dept', (row && DEPT_IDS[row.dept]) ?? 'keep');
put('status', (row && STATUS_IDS[row.status]) ?? 'keep');
log(row ? `Editing ${row.name} (${row.dept}, ${row.status})` : 'No employee selected');
});
}
function patch(api: GridApi<Row>, changes: Partial<Row>): void {
const key = api.getSelectedKeys()[0];
const row = key === undefined ? undefined : api.getRowByKey(key);
if (!row) {
put('dept', 'keep');
put('status', 'keep');
return say('Select an employee in the table first');
}
api.updateRows([{ ...row, ...changes }]);
say(`Changed ${row.name} (${changes.dept ?? row.dept}, ${changes.status ?? row.status})`);
}
// Form -> grid: the search box becomes a filter, and the select lists below change the selected row's values.
export const actions = {
search: (api: GridApi<Row>, value?: string) => {
const text = (value ?? '').trim();
api.setFilterModel(
text
? { match: 'all', items: [{ columnId: 'name', operator: 'contains', value: text }] }
: null,
);
},
dept: (api: GridApi<Row>, value?: string) => {
if (value && value !== 'keep') patch(api, { dept: DEPT_VALUES[value] ?? value });
},
status: (api: GridApi<Row>, value?: string) => {
if (value && value !== 'keep') patch(api, { status: STATUS_VALUES[value] ?? value });
},
};