표에서 고른 행을 폼에서 고쳐 적용하기Edit the selected row in a form and apply it
표에서 행을 하나 고르면 그 값이 폼에 채워지고, 폼에서 고친 값은 '적용'을 눌러야 표의 그 행에 들어가요. 적용 전의 값(초안)은 화면이 따로 들고 있어서 표는 그대로예요. React, Vue, Angular 탭에는 각 프레임워크의 방식으로 쓴 같은 예제가 들어 있어요.Selecting a row fills a form with its values, and edits only reach that row when you press Apply. The draft is kept by the screen, so the grid does not change before then. The React, Vue and Angular tabs hold the same example written the way each framework does it.
실행 화면은 JavaScript 버전이에요. 아래 탭의 React·Vue·Angular 코드는 같은 예제를 각 프레임워크로 직접 쓴 코드예요.The preview runs the JavaScript version. The React, Vue, Angular tabs below hold the same example written for each framework.
코드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="title"
/>
<select aria-label="담당" id="owner">
<option value="p1" selected>김민준</option>
<option value="p2">이서연</option>
<option value="p3">박지호</option>
</select>
<button type="button" id="apply">적용</button>
</div>
<div id="grid" style="height: 420px"></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('title').addEventListener('input', (event) => actions.title(api, event.target.value));
document.getElementById('owner').addEventListener('change', (event) => actions.owner(api, event.target.value));
document.getElementById('apply').addEventListener('click', () => actions.apply(api));export const OWNERS = ['김민준', '이서연', '박지호'];
// 선택 목록의 값(영문 id)과 담당자 이름을 맞바꿔요.
const OWNER_BY_ID = { p1: '김민준', p2: '이서연', p3: '박지호' };
const ID_BY_OWNER = Object.fromEntries(Object.entries(OWNER_BY_ID).map(([id, name]) => [name, id]));
export const options = {
columns: [
{ id: 'title', title: '업무', width: 190 },
{ id: 'owner', title: '담당', width: 110 },
{ id: 'status', title: '상태', width: 90 },
],
rows: [
{ id: 1, title: '견적서 보내기', owner: '김민준', status: '진행' },
{ id: 2, title: '계약서 검토', owner: '이서연', status: '대기' },
{ id: 3, title: '샘플 발송', owner: '박지호', status: '진행' },
{ id: 4, title: '재고 점검', owner: '김민준', status: '완료' },
],
rowKey: 'id',
selection: 'single',
};
// 표에서 행을 하나 고르면 그 값이 폼에 채워지고, 폼에서 고친 값을 '적용'하면 그 행이 바뀌어요.
// 폼의 값(초안)은 화면이 들고 있고, 적용하기 전에는 표가 바뀌지 않아요. 아래는 JavaScript 버전이에요.
let say = () => { };
const draft = { title: '', owner: OWNERS[0] };
const put = (id, value) => {
const el = document.getElementById(id);
if (el instanceof HTMLInputElement || 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]);
draft.title = row?.title ?? '';
draft.owner = row?.owner ?? OWNERS[0] ?? '';
put('title', draft.title);
put('owner', ID_BY_OWNER[draft.owner] ?? draft.owner);
log({
ko: row ? `'${row.title}' 행을 폼에 채웠어요` : '선택한 행이 없어요',
en: row ? `Filled the form with row '${row.title}'` : 'No row selected',
});
});
}
export const actions = {
title: (_api, value) => {
draft.title = value ?? '';
},
owner: (_api, value) => {
draft.owner = value === undefined ? draft.owner : (OWNER_BY_ID[value] ?? value);
},
apply: (api) => {
const key = api.getSelectedKeys()[0];
const row = key === undefined ? undefined : api.getRowByKey(key);
if (!row)
return say({
ko: '먼저 표에서 행을 하나 골라 주세요',
en: 'Select a row in the table first',
});
if (draft.title.trim() === '')
return say({ ko: '업무 이름을 비워 둘 수 없어요', en: 'The task name cannot be empty' });
api.updateRows([{ ...row, title: draft.title.trim(), owner: draft.owner }]);
say({
ko: `'${draft.title.trim()}' 행에 적용했어요 (담당 ${draft.owner})`,
en: `Applied to row '${draft.title.trim()}' (owner ${draft.owner})`,
});
},
};<!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="title"
/>
<select aria-label="담당" id="owner">
<option value="p1" selected>김민준</option>
<option value="p2">이서연</option>
<option value="p3">박지호</option>
</select>
<button type="button" id="apply">적용</button>
</div>
<div id="grid" style="height: 420px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const OWNERS = ['김민준', '이서연', '박지호'];
// 선택 목록의 값(영문 id)과 담당자 이름을 맞바꿔요.
const OWNER_BY_ID = { p1: '김민준', p2: '이서연', p3: '박지호' };
const ID_BY_OWNER = Object.fromEntries(Object.entries(OWNER_BY_ID).map(([id, name]) => [name, id]));
const options = {
columns: [
{ id: 'title', title: '업무', width: 190 },
{ id: 'owner', title: '담당', width: 110 },
{ id: 'status', title: '상태', width: 90 },
],
rows: [
{ id: 1, title: '견적서 보내기', owner: '김민준', status: '진행' },
{ id: 2, title: '계약서 검토', owner: '이서연', status: '대기' },
{ id: 3, title: '샘플 발송', owner: '박지호', status: '진행' },
{ id: 4, title: '재고 점검', owner: '김민준', status: '완료' },
],
rowKey: 'id',
selection: 'single',
};
// 표에서 행을 하나 고르면 그 값이 폼에 채워지고, 폼에서 고친 값을 '적용'하면 그 행이 바뀌어요.
// 폼의 값(초안)은 화면이 들고 있고, 적용하기 전에는 표가 바뀌지 않아요. 아래는 JavaScript 버전이에요.
let say = () => { };
const draft = { title: '', owner: OWNERS[0] };
const put = (id, value) => {
const el = document.getElementById(id);
if (el instanceof HTMLInputElement || 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]);
draft.title = row?.title ?? '';
draft.owner = row?.owner ?? OWNERS[0] ?? '';
put('title', draft.title);
put('owner', ID_BY_OWNER[draft.owner] ?? draft.owner);
log({
ko: row ? `'${row.title}' 행을 폼에 채웠어요` : '선택한 행이 없어요',
en: row ? `Filled the form with row '${row.title}'` : 'No row selected',
});
});
}
const actions = {
title: (_api, value) => {
draft.title = value ?? '';
},
owner: (_api, value) => {
draft.owner = value === undefined ? draft.owner : (OWNER_BY_ID[value] ?? value);
},
apply: (api) => {
const key = api.getSelectedKeys()[0];
const row = key === undefined ? undefined : api.getRowByKey(key);
if (!row)
return say({
ko: '먼저 표에서 행을 하나 골라 주세요',
en: 'Select a row in the table first',
});
if (draft.title.trim() === '')
return say({ ko: '업무 이름을 비워 둘 수 없어요', en: 'The task name cannot be empty' });
api.updateRows([{ ...row, title: draft.title.trim(), owner: draft.owner }]);
say({
ko: `'${draft.title.trim()}' 행에 적용했어요 (담당 ${draft.owner})`,
en: `Applied to row '${draft.title.trim()}' (owner ${draft.owner})`,
});
},
};
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('title').addEventListener('input', (event) => actions.title(api, event.target.value));
document.getElementById('owner').addEventListener('change', (event) => actions.owner(api, event.target.value));
document.getElementById('apply').addEventListener('click', () => actions.apply(api));
</script>
</body>
</html>버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Grid, useGridApi } from '@maxgrid/react';
import { useState } from 'react';
import { OWNERS, options, type Row } from './example';
// 표에서 행을 고르면 폼 초안(draft)이 채워지고, '적용'을 누르면 표의 그 행이 바뀌어요.
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
const [row, setRow] = useState<Row | null>(null);
const [draft, setDraft] = useState({ title: '', owner: OWNERS[0] ?? '' });
const pick = (key: Row['id'] | undefined) => {
const next = key === undefined ? null : (api?.getRowByKey(key) ?? null);
setRow(next);
if (next) setDraft({ title: next.title, owner: next.owner });
};
const apply = () => {
if (!row || draft.title.trim() === '') return;
api?.updateRows([{ ...row, title: draft.title.trim(), owner: draft.owner }]);
};
return (
<>
<div style={{ height: 200 }}>
<Grid<Row>
ref={gridRef}
options={options}
onSelectionChanged={({ selectedKeys }) => pick(selectedKeys[0] as number | undefined)}
/>
</div>
<form
style={{ display: 'flex', gap: 8, marginTop: 8 }}
onSubmit={(event) => {
event.preventDefault();
apply();
}}
>
<input
aria-label="업무 이름"
disabled={!row}
value={draft.title}
onChange={(event) => setDraft({ ...draft, title: event.target.value })}
/>
<select
aria-label="담당"
disabled={!row}
value={draft.owner}
onChange={(event) => setDraft({ ...draft, owner: event.target.value })}
>
{OWNERS.map((owner) => (
<option key={owner}>{owner}</option>
))}
</select>
<button type="submit" disabled={!row}>
적용
</button>
</form>
{!row && <p>표에서 행을 하나 골라 주세요.</p>}
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
title: string;
owner: string;
status: string;
}
export const OWNERS = ['김민준', '이서연', '박지호'];
// 선택 목록의 값(영문 id)과 담당자 이름을 맞바꿔요.
const OWNER_BY_ID: Record<string, string> = { p1: '김민준', p2: '이서연', p3: '박지호' };
const ID_BY_OWNER = Object.fromEntries(Object.entries(OWNER_BY_ID).map(([id, name]) => [name, id]));
export const options: GridOptions<Row> = {
columns: [
{ id: 'title', title: '업무', width: 190 },
{ id: 'owner', title: '담당', width: 110 },
{ id: 'status', title: '상태', width: 90 },
],
rows: [
{ id: 1, title: '견적서 보내기', owner: '김민준', status: '진행' },
{ id: 2, title: '계약서 검토', owner: '이서연', status: '대기' },
{ id: 3, title: '샘플 발송', owner: '박지호', status: '진행' },
{ id: 4, title: '재고 점검', owner: '김민준', status: '완료' },
],
rowKey: 'id',
selection: 'single',
};
// 표에서 행을 하나 고르면 그 값이 폼에 채워지고, 폼에서 고친 값을 '적용'하면 그 행이 바뀌어요.
// 폼의 값(초안)은 화면이 들고 있고, 적용하기 전에는 표가 바뀌지 않아요. 아래는 JavaScript 버전이에요.
let say: (message: LogText) => void = () => {};
const draft = { title: '', owner: OWNERS[0] as string };
const put = (id: string, value: string): void => {
const el = document.getElementById(id);
if (el instanceof HTMLInputElement || 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]);
draft.title = row?.title ?? '';
draft.owner = row?.owner ?? OWNERS[0] ?? '';
put('title', draft.title);
put('owner', ID_BY_OWNER[draft.owner] ?? draft.owner);
log({
ko: row ? `'${row.title}' 행을 폼에 채웠어요` : '선택한 행이 없어요',
en: row ? `Filled the form with row '${row.title}'` : 'No row selected',
});
});
}
export const actions = {
title: (_api: GridApi<Row>, value?: string) => {
draft.title = value ?? '';
},
owner: (_api: GridApi<Row>, value?: string) => {
draft.owner = value === undefined ? draft.owner : (OWNER_BY_ID[value] ?? value);
},
apply: (api: GridApi<Row>) => {
const key = api.getSelectedKeys()[0];
const row = key === undefined ? undefined : api.getRowByKey(key);
if (!row)
return say({
ko: '먼저 표에서 행을 하나 골라 주세요',
en: 'Select a row in the table first',
});
if (draft.title.trim() === '')
return say({ ko: '업무 이름을 비워 둘 수 없어요', en: 'The task name cannot be empty' });
api.updateRows([{ ...row, title: draft.title.trim(), owner: draft.owner }]);
say({
ko: `'${draft.title.trim()}' 행에 적용했어요 (담당 ${draft.owner})`,
en: `Applied to row '${draft.title.trim()}' (owner ${draft.owner})`,
});
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<script setup lang="ts">
import { Grid, type GridInstance, type RowKey } from '@maxgrid/vue';
import { reactive, ref } from 'vue';
import { OWNERS, options, type Row } from './example';
// 표에서 행을 고르면 폼 초안(draft)이 채워지고, '적용'을 누르면 표의 그 행이 바뀌어요.
const grid = ref<GridInstance<Row> | null>(null);
const row = ref<Row | null>(null);
const draft = reactive({ title: '', owner: OWNERS[0] ?? '' });
function pick(keys: RowKey[]) {
const key = keys[0];
row.value = key === undefined ? null : (grid.value?.getApi()?.getRowByKey(key) ?? null);
if (row.value) Object.assign(draft, { title: row.value.title, owner: row.value.owner });
}
function apply() {
if (!row.value || draft.title.trim() === '') return;
grid.value
?.getApi()
?.updateRows([{ ...row.value, title: draft.title.trim(), owner: draft.owner }]);
}
</script>
<template>
<Grid
ref="grid"
style="height: 200px"
:options="options"
@selectionChanged="pick($event.selectedKeys)"
/>
<form class="form" @submit.prevent="apply">
<input v-model="draft.title" aria-label="업무 이름" :disabled="!row" />
<select v-model="draft.owner" aria-label="담당" :disabled="!row">
<option v-for="owner in OWNERS" :key="owner">{{ owner }}</option>
</select>
<button type="submit" :disabled="!row">적용</button>
</form>
<p v-if="!row">표에서 행을 하나 골라 주세요.</p>
</template>
<style scoped>
.form {
display: flex;
gap: 8px;
margin-top: 8px;
}
</style>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
title: string;
owner: string;
status: string;
}
export const OWNERS = ['김민준', '이서연', '박지호'];
// 선택 목록의 값(영문 id)과 담당자 이름을 맞바꿔요.
const OWNER_BY_ID: Record<string, string> = { p1: '김민준', p2: '이서연', p3: '박지호' };
const ID_BY_OWNER = Object.fromEntries(Object.entries(OWNER_BY_ID).map(([id, name]) => [name, id]));
export const options: GridOptions<Row> = {
columns: [
{ id: 'title', title: '업무', width: 190 },
{ id: 'owner', title: '담당', width: 110 },
{ id: 'status', title: '상태', width: 90 },
],
rows: [
{ id: 1, title: '견적서 보내기', owner: '김민준', status: '진행' },
{ id: 2, title: '계약서 검토', owner: '이서연', status: '대기' },
{ id: 3, title: '샘플 발송', owner: '박지호', status: '진행' },
{ id: 4, title: '재고 점검', owner: '김민준', status: '완료' },
],
rowKey: 'id',
selection: 'single',
};
// 표에서 행을 하나 고르면 그 값이 폼에 채워지고, 폼에서 고친 값을 '적용'하면 그 행이 바뀌어요.
// 폼의 값(초안)은 화면이 들고 있고, 적용하기 전에는 표가 바뀌지 않아요. 아래는 JavaScript 버전이에요.
let say: (message: LogText) => void = () => {};
const draft = { title: '', owner: OWNERS[0] as string };
const put = (id: string, value: string): void => {
const el = document.getElementById(id);
if (el instanceof HTMLInputElement || 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]);
draft.title = row?.title ?? '';
draft.owner = row?.owner ?? OWNERS[0] ?? '';
put('title', draft.title);
put('owner', ID_BY_OWNER[draft.owner] ?? draft.owner);
log({
ko: row ? `'${row.title}' 행을 폼에 채웠어요` : '선택한 행이 없어요',
en: row ? `Filled the form with row '${row.title}'` : 'No row selected',
});
});
}
export const actions = {
title: (_api: GridApi<Row>, value?: string) => {
draft.title = value ?? '';
},
owner: (_api: GridApi<Row>, value?: string) => {
draft.owner = value === undefined ? draft.owner : (OWNER_BY_ID[value] ?? value);
},
apply: (api: GridApi<Row>) => {
const key = api.getSelectedKeys()[0];
const row = key === undefined ? undefined : api.getRowByKey(key);
if (!row)
return say({
ko: '먼저 표에서 행을 하나 골라 주세요',
en: 'Select a row in the table first',
});
if (draft.title.trim() === '')
return say({ ko: '업무 이름을 비워 둘 수 없어요', en: 'The task name cannot be empty' });
api.updateRows([{ ...row, title: draft.title.trim(), owner: draft.owner }]);
say({
ko: `'${draft.title.trim()}' 행에 적용했어요 (담당 ${draft.owner})`,
en: `Applied to row '${draft.title.trim()}' (owner ${draft.owner})`,
});
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Component, ViewChild, signal } from '@angular/core';
import {
GridComponent,
isGridEvent,
type GridEventMessage,
type GridEventName,
} from '@maxgrid/angular';
import { OWNERS, options, type Row } from './example';
// 표에서 행을 고르면 폼 초안이 채워지고, '적용'을 누르면 표의 그 행이 바뀌어요.
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<mx-grid
style="height: 200px"
[options]="options"
[events]="events"
(gridEvent)="onEvent($event)"
/>
<form style="display: flex; gap: 8px; margin-top: 8px" (submit)="apply($event)">
<input
aria-label="업무 이름"
[disabled]="!row()"
[value]="title()"
(input)="title.set($any($event.target).value)"
/>
<select
aria-label="담당"
[disabled]="!row()"
[value]="owner()"
(change)="owner.set($any($event.target).value)"
>
@for (name of owners; track name) {
<option [value]="name" [selected]="name === owner()">{{ name }}</option>
}
</select>
<button type="submit" [disabled]="!row()">적용</button>
</form>
@if (!row()) {
<p>표에서 행을 하나 골라 주세요.</p>
}
`,
})
export class ExampleGridComponent {
readonly options = options;
readonly owners = OWNERS;
readonly events: GridEventName[] = ['selectionChanged']; // 이 알림만 받아요
readonly row = signal<Row | null>(null);
readonly title = signal('');
readonly owner = signal(OWNERS[0] ?? '');
@ViewChild(GridComponent) private readonly gridRef?: GridComponent<Row>;
onEvent(message: GridEventMessage<Row>): void {
if (!isGridEvent(message, 'selectionChanged')) return;
const key = message.payload.selectedKeys[0];
const next = key === undefined ? null : (this.gridRef?.getApi()?.getRowByKey(key) ?? null);
this.row.set(next);
if (next) {
this.title.set(next.title);
this.owner.set(next.owner);
}
}
apply(event: Event): void {
event.preventDefault();
const row = this.row();
if (!row || this.title().trim() === '') return;
this.gridRef
?.getApi()
?.updateRows([{ ...row, title: this.title().trim(), owner: this.owner() }]);
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
title: string;
owner: string;
status: string;
}
export const OWNERS = ['김민준', '이서연', '박지호'];
// 선택 목록의 값(영문 id)과 담당자 이름을 맞바꿔요.
const OWNER_BY_ID: Record<string, string> = { p1: '김민준', p2: '이서연', p3: '박지호' };
const ID_BY_OWNER = Object.fromEntries(Object.entries(OWNER_BY_ID).map(([id, name]) => [name, id]));
export const options: GridOptions<Row> = {
columns: [
{ id: 'title', title: '업무', width: 190 },
{ id: 'owner', title: '담당', width: 110 },
{ id: 'status', title: '상태', width: 90 },
],
rows: [
{ id: 1, title: '견적서 보내기', owner: '김민준', status: '진행' },
{ id: 2, title: '계약서 검토', owner: '이서연', status: '대기' },
{ id: 3, title: '샘플 발송', owner: '박지호', status: '진행' },
{ id: 4, title: '재고 점검', owner: '김민준', status: '완료' },
],
rowKey: 'id',
selection: 'single',
};
// 표에서 행을 하나 고르면 그 값이 폼에 채워지고, 폼에서 고친 값을 '적용'하면 그 행이 바뀌어요.
// 폼의 값(초안)은 화면이 들고 있고, 적용하기 전에는 표가 바뀌지 않아요. 아래는 JavaScript 버전이에요.
let say: (message: LogText) => void = () => {};
const draft = { title: '', owner: OWNERS[0] as string };
const put = (id: string, value: string): void => {
const el = document.getElementById(id);
if (el instanceof HTMLInputElement || 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]);
draft.title = row?.title ?? '';
draft.owner = row?.owner ?? OWNERS[0] ?? '';
put('title', draft.title);
put('owner', ID_BY_OWNER[draft.owner] ?? draft.owner);
log({
ko: row ? `'${row.title}' 행을 폼에 채웠어요` : '선택한 행이 없어요',
en: row ? `Filled the form with row '${row.title}'` : 'No row selected',
});
});
}
export const actions = {
title: (_api: GridApi<Row>, value?: string) => {
draft.title = value ?? '';
},
owner: (_api: GridApi<Row>, value?: string) => {
draft.owner = value === undefined ? draft.owner : (OWNER_BY_ID[value] ?? value);
},
apply: (api: GridApi<Row>) => {
const key = api.getSelectedKeys()[0];
const row = key === undefined ? undefined : api.getRowByKey(key);
if (!row)
return say({
ko: '먼저 표에서 행을 하나 골라 주세요',
en: 'Select a row in the table first',
});
if (draft.title.trim() === '')
return say({ ko: '업무 이름을 비워 둘 수 없어요', en: 'The task name cannot be empty' });
api.updateRows([{ ...row, title: draft.title.trim(), owner: draft.owner }]);
say({
ko: `'${draft.title.trim()}' 행에 적용했어요 (담당 ${draft.owner})`,
en: `Applied to row '${draft.title.trim()}' (owner ${draft.owner})`,
});
},
};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="Task name"
placeholder="Task name (filled when you pick a row)"
id="title"
/>
<select aria-label="Owner" id="owner">
<option value="p1" selected>Liam Carter</option>
<option value="p2">Emma Stone</option>
<option value="p3">Noah Reed</option>
</select>
<button type="button" id="apply">Apply</button>
</div>
<div id="grid" style="height: 420px"></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('title').addEventListener('input', (event) => actions.title(api, event.target.value));
document.getElementById('owner').addEventListener('change', (event) => actions.owner(api, event.target.value));
document.getElementById('apply').addEventListener('click', () => actions.apply(api));export const OWNERS = ['Liam Carter', 'Emma Stone', 'Noah Reed'];
// Map the select values (ASCII ids from meta.json) to the English owner names.
const OWNER_BY_ID = {
p1: 'Liam Carter',
p2: 'Emma Stone',
p3: 'Noah Reed',
};
const ID_BY_OWNER = Object.fromEntries(Object.entries(OWNER_BY_ID).map(([id, name]) => [name, id]));
export const options = {
columns: [
{ id: 'title', title: 'Task', width: 190 },
{ id: 'owner', title: 'Owner', width: 120 },
{ id: 'status', title: 'Status', width: 90 },
],
rows: [
{ id: 1, title: 'Send the quote', owner: 'Liam Carter', status: 'Active' },
{ id: 2, title: 'Review the contract', owner: 'Emma Stone', status: 'Pending' },
{ id: 3, title: 'Ship samples', owner: 'Noah Reed', status: 'Active' },
{ id: 4, title: 'Check inventory', owner: 'Liam Carter', status: 'Done' },
],
rowKey: 'id',
selection: 'single',
};
// Pick a row in the table and its values fill the form; 'Apply' the values edited in the form and that row changes.
// The form values (the draft) are held by the screen, and the table does not change before applying. Below is the JavaScript version.
let say = () => { };
const draft = { title: '', owner: OWNERS[0] };
const put = (id, value) => {
const el = document.getElementById(id);
if (el instanceof HTMLInputElement || 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]);
draft.title = row?.title ?? '';
draft.owner = row?.owner ?? OWNERS[0] ?? '';
put('title', draft.title);
put('owner', ID_BY_OWNER[draft.owner] ?? draft.owner);
log(row ? `Filled the form with row '${row.title}'` : 'No row selected');
});
}
export const actions = {
title: (_api, value) => {
draft.title = value ?? '';
},
owner: (_api, value) => {
draft.owner = value === undefined ? draft.owner : (OWNER_BY_ID[value] ?? value);
},
apply: (api) => {
const key = api.getSelectedKeys()[0];
const row = key === undefined ? undefined : api.getRowByKey(key);
if (!row)
return say('Select a row in the table first');
if (draft.title.trim() === '')
return say('The task name cannot be empty');
api.updateRows([{ ...row, title: draft.title.trim(), owner: draft.owner }]);
say(`Applied to row '${draft.title.trim()}' (owner ${draft.owner})`);
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Edit the selected row in a form and apply it</title>
</head>
<body>
<div class="toolbar">
<input
type="text"
aria-label="Task name"
placeholder="Task name (filled when you pick a row)"
id="title"
/>
<select aria-label="Owner" id="owner">
<option value="p1" selected>Liam Carter</option>
<option value="p2">Emma Stone</option>
<option value="p3">Noah Reed</option>
</select>
<button type="button" id="apply">Apply</button>
</div>
<div id="grid" style="height: 420px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const OWNERS = ['Liam Carter', 'Emma Stone', 'Noah Reed'];
// Map the select values (ASCII ids from meta.json) to the English owner names.
const OWNER_BY_ID = {
p1: 'Liam Carter',
p2: 'Emma Stone',
p3: 'Noah Reed',
};
const ID_BY_OWNER = Object.fromEntries(Object.entries(OWNER_BY_ID).map(([id, name]) => [name, id]));
const options = {
columns: [
{ id: 'title', title: 'Task', width: 190 },
{ id: 'owner', title: 'Owner', width: 120 },
{ id: 'status', title: 'Status', width: 90 },
],
rows: [
{ id: 1, title: 'Send the quote', owner: 'Liam Carter', status: 'Active' },
{ id: 2, title: 'Review the contract', owner: 'Emma Stone', status: 'Pending' },
{ id: 3, title: 'Ship samples', owner: 'Noah Reed', status: 'Active' },
{ id: 4, title: 'Check inventory', owner: 'Liam Carter', status: 'Done' },
],
rowKey: 'id',
selection: 'single',
};
// Pick a row in the table and its values fill the form; 'Apply' the values edited in the form and that row changes.
// The form values (the draft) are held by the screen, and the table does not change before applying. Below is the JavaScript version.
let say = () => { };
const draft = { title: '', owner: OWNERS[0] };
const put = (id, value) => {
const el = document.getElementById(id);
if (el instanceof HTMLInputElement || 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]);
draft.title = row?.title ?? '';
draft.owner = row?.owner ?? OWNERS[0] ?? '';
put('title', draft.title);
put('owner', ID_BY_OWNER[draft.owner] ?? draft.owner);
log(row ? `Filled the form with row '${row.title}'` : 'No row selected');
});
}
const actions = {
title: (_api, value) => {
draft.title = value ?? '';
},
owner: (_api, value) => {
draft.owner = value === undefined ? draft.owner : (OWNER_BY_ID[value] ?? value);
},
apply: (api) => {
const key = api.getSelectedKeys()[0];
const row = key === undefined ? undefined : api.getRowByKey(key);
if (!row)
return say('Select a row in the table first');
if (draft.title.trim() === '')
return say('The task name cannot be empty');
api.updateRows([{ ...row, title: draft.title.trim(), owner: draft.owner }]);
say(`Applied to row '${draft.title.trim()}' (owner ${draft.owner})`);
},
};
options.ariaLabel = "Edit the selected row in a form and apply it";
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('title').addEventListener('input', (event) => actions.title(api, event.target.value));
document.getElementById('owner').addEventListener('change', (event) => actions.owner(api, event.target.value));
document.getElementById('apply').addEventListener('click', () => actions.apply(api));
</script>
</body>
</html>When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Grid, useGridApi } from '@maxgrid/react';
import { useState } from 'react';
import { OWNERS, options, type Row } from './example';
// 표에서 행을 고르면 폼 초안(draft)이 채워지고, '적용'을 누르면 표의 그 행이 바뀌어요.
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
const [row, setRow] = useState<Row | null>(null);
const [draft, setDraft] = useState({ title: '', owner: OWNERS[0] ?? '' });
const pick = (key: Row['id'] | undefined) => {
const next = key === undefined ? null : (api?.getRowByKey(key) ?? null);
setRow(next);
if (next) setDraft({ title: next.title, owner: next.owner });
};
const apply = () => {
if (!row || draft.title.trim() === '') return;
api?.updateRows([{ ...row, title: draft.title.trim(), owner: draft.owner }]);
};
return (
<>
<div style={{ height: 200 }}>
<Grid<Row>
ref={gridRef}
options={options}
onSelectionChanged={({ selectedKeys }) => pick(selectedKeys[0] as number | undefined)}
/>
</div>
<form
style={{ display: 'flex', gap: 8, marginTop: 8 }}
onSubmit={(event) => {
event.preventDefault();
apply();
}}
>
<input
aria-label="업무 이름"
disabled={!row}
value={draft.title}
onChange={(event) => setDraft({ ...draft, title: event.target.value })}
/>
<select
aria-label="담당"
disabled={!row}
value={draft.owner}
onChange={(event) => setDraft({ ...draft, owner: event.target.value })}
>
{OWNERS.map((owner) => (
<option key={owner}>{owner}</option>
))}
</select>
<button type="submit" disabled={!row}>
적용
</button>
</form>
{!row && <p>표에서 행을 하나 골라 주세요.</p>}
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
title: string;
owner: string;
status: string;
}
export const OWNERS = ['Liam Carter', 'Emma Stone', 'Noah Reed'];
// Map the select values (ASCII ids from meta.json) to the English owner names.
const OWNER_BY_ID: Record<string, string> = {
p1: 'Liam Carter',
p2: 'Emma Stone',
p3: 'Noah Reed',
};
const ID_BY_OWNER = Object.fromEntries(Object.entries(OWNER_BY_ID).map(([id, name]) => [name, id]));
export const options: GridOptions<Row> = {
columns: [
{ id: 'title', title: 'Task', width: 190 },
{ id: 'owner', title: 'Owner', width: 120 },
{ id: 'status', title: 'Status', width: 90 },
],
rows: [
{ id: 1, title: 'Send the quote', owner: 'Liam Carter', status: 'Active' },
{ id: 2, title: 'Review the contract', owner: 'Emma Stone', status: 'Pending' },
{ id: 3, title: 'Ship samples', owner: 'Noah Reed', status: 'Active' },
{ id: 4, title: 'Check inventory', owner: 'Liam Carter', status: 'Done' },
],
rowKey: 'id',
selection: 'single',
};
// Pick a row in the table and its values fill the form; 'Apply' the values edited in the form and that row changes.
// The form values (the draft) are held by the screen, and the table does not change before applying. Below is the JavaScript version.
let say: (message: LogText) => void = () => {};
const draft = { title: '', owner: OWNERS[0] as string };
const put = (id: string, value: string): void => {
const el = document.getElementById(id);
if (el instanceof HTMLInputElement || 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]);
draft.title = row?.title ?? '';
draft.owner = row?.owner ?? OWNERS[0] ?? '';
put('title', draft.title);
put('owner', ID_BY_OWNER[draft.owner] ?? draft.owner);
log(row ? `Filled the form with row '${row.title}'` : 'No row selected');
});
}
export const actions = {
title: (_api: GridApi<Row>, value?: string) => {
draft.title = value ?? '';
},
owner: (_api: GridApi<Row>, value?: string) => {
draft.owner = value === undefined ? draft.owner : (OWNER_BY_ID[value] ?? value);
},
apply: (api: GridApi<Row>) => {
const key = api.getSelectedKeys()[0];
const row = key === undefined ? undefined : api.getRowByKey(key);
if (!row) return say('Select a row in the table first');
if (draft.title.trim() === '') return say('The task name cannot be empty');
api.updateRows([{ ...row, title: draft.title.trim(), owner: draft.owner }]);
say(`Applied to row '${draft.title.trim()}' (owner ${draft.owner})`);
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<script setup lang="ts">
import { Grid, type GridInstance, type RowKey } from '@maxgrid/vue';
import { reactive, ref } from 'vue';
import { OWNERS, options, type Row } from './example';
// 표에서 행을 고르면 폼 초안(draft)이 채워지고, '적용'을 누르면 표의 그 행이 바뀌어요.
const grid = ref<GridInstance<Row> | null>(null);
const row = ref<Row | null>(null);
const draft = reactive({ title: '', owner: OWNERS[0] ?? '' });
function pick(keys: RowKey[]) {
const key = keys[0];
row.value = key === undefined ? null : (grid.value?.getApi()?.getRowByKey(key) ?? null);
if (row.value) Object.assign(draft, { title: row.value.title, owner: row.value.owner });
}
function apply() {
if (!row.value || draft.title.trim() === '') return;
grid.value
?.getApi()
?.updateRows([{ ...row.value, title: draft.title.trim(), owner: draft.owner }]);
}
</script>
<template>
<Grid
ref="grid"
style="height: 200px"
:options="options"
@selectionChanged="pick($event.selectedKeys)"
/>
<form class="form" @submit.prevent="apply">
<input v-model="draft.title" aria-label="업무 이름" :disabled="!row" />
<select v-model="draft.owner" aria-label="담당" :disabled="!row">
<option v-for="owner in OWNERS" :key="owner">{{ owner }}</option>
</select>
<button type="submit" :disabled="!row">적용</button>
</form>
<p v-if="!row">표에서 행을 하나 골라 주세요.</p>
</template>
<style scoped>
.form {
display: flex;
gap: 8px;
margin-top: 8px;
}
</style>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
title: string;
owner: string;
status: string;
}
export const OWNERS = ['Liam Carter', 'Emma Stone', 'Noah Reed'];
// Map the select values (ASCII ids from meta.json) to the English owner names.
const OWNER_BY_ID: Record<string, string> = {
p1: 'Liam Carter',
p2: 'Emma Stone',
p3: 'Noah Reed',
};
const ID_BY_OWNER = Object.fromEntries(Object.entries(OWNER_BY_ID).map(([id, name]) => [name, id]));
export const options: GridOptions<Row> = {
columns: [
{ id: 'title', title: 'Task', width: 190 },
{ id: 'owner', title: 'Owner', width: 120 },
{ id: 'status', title: 'Status', width: 90 },
],
rows: [
{ id: 1, title: 'Send the quote', owner: 'Liam Carter', status: 'Active' },
{ id: 2, title: 'Review the contract', owner: 'Emma Stone', status: 'Pending' },
{ id: 3, title: 'Ship samples', owner: 'Noah Reed', status: 'Active' },
{ id: 4, title: 'Check inventory', owner: 'Liam Carter', status: 'Done' },
],
rowKey: 'id',
selection: 'single',
};
// Pick a row in the table and its values fill the form; 'Apply' the values edited in the form and that row changes.
// The form values (the draft) are held by the screen, and the table does not change before applying. Below is the JavaScript version.
let say: (message: LogText) => void = () => {};
const draft = { title: '', owner: OWNERS[0] as string };
const put = (id: string, value: string): void => {
const el = document.getElementById(id);
if (el instanceof HTMLInputElement || 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]);
draft.title = row?.title ?? '';
draft.owner = row?.owner ?? OWNERS[0] ?? '';
put('title', draft.title);
put('owner', ID_BY_OWNER[draft.owner] ?? draft.owner);
log(row ? `Filled the form with row '${row.title}'` : 'No row selected');
});
}
export const actions = {
title: (_api: GridApi<Row>, value?: string) => {
draft.title = value ?? '';
},
owner: (_api: GridApi<Row>, value?: string) => {
draft.owner = value === undefined ? draft.owner : (OWNER_BY_ID[value] ?? value);
},
apply: (api: GridApi<Row>) => {
const key = api.getSelectedKeys()[0];
const row = key === undefined ? undefined : api.getRowByKey(key);
if (!row) return say('Select a row in the table first');
if (draft.title.trim() === '') return say('The task name cannot be empty');
api.updateRows([{ ...row, title: draft.title.trim(), owner: draft.owner }]);
say(`Applied to row '${draft.title.trim()}' (owner ${draft.owner})`);
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Component, ViewChild, signal } from '@angular/core';
import {
GridComponent,
isGridEvent,
type GridEventMessage,
type GridEventName,
} from '@maxgrid/angular';
import { OWNERS, options, type Row } from './example';
// 표에서 행을 고르면 폼 초안이 채워지고, '적용'을 누르면 표의 그 행이 바뀌어요.
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<mx-grid
style="height: 200px"
[options]="options"
[events]="events"
(gridEvent)="onEvent($event)"
/>
<form style="display: flex; gap: 8px; margin-top: 8px" (submit)="apply($event)">
<input
aria-label="업무 이름"
[disabled]="!row()"
[value]="title()"
(input)="title.set($any($event.target).value)"
/>
<select
aria-label="담당"
[disabled]="!row()"
[value]="owner()"
(change)="owner.set($any($event.target).value)"
>
@for (name of owners; track name) {
<option [value]="name" [selected]="name === owner()">{{ name }}</option>
}
</select>
<button type="submit" [disabled]="!row()">적용</button>
</form>
@if (!row()) {
<p>표에서 행을 하나 골라 주세요.</p>
}
`,
})
export class ExampleGridComponent {
readonly options = options;
readonly owners = OWNERS;
readonly events: GridEventName[] = ['selectionChanged']; // 이 알림만 받아요
readonly row = signal<Row | null>(null);
readonly title = signal('');
readonly owner = signal(OWNERS[0] ?? '');
@ViewChild(GridComponent) private readonly gridRef?: GridComponent<Row>;
onEvent(message: GridEventMessage<Row>): void {
if (!isGridEvent(message, 'selectionChanged')) return;
const key = message.payload.selectedKeys[0];
const next = key === undefined ? null : (this.gridRef?.getApi()?.getRowByKey(key) ?? null);
this.row.set(next);
if (next) {
this.title.set(next.title);
this.owner.set(next.owner);
}
}
apply(event: Event): void {
event.preventDefault();
const row = this.row();
if (!row || this.title().trim() === '') return;
this.gridRef
?.getApi()
?.updateRows([{ ...row, title: this.title().trim(), owner: this.owner() }]);
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string;
export interface Row {
id: number;
title: string;
owner: string;
status: string;
}
export const OWNERS = ['Liam Carter', 'Emma Stone', 'Noah Reed'];
// Map the select values (ASCII ids from meta.json) to the English owner names.
const OWNER_BY_ID: Record<string, string> = {
p1: 'Liam Carter',
p2: 'Emma Stone',
p3: 'Noah Reed',
};
const ID_BY_OWNER = Object.fromEntries(Object.entries(OWNER_BY_ID).map(([id, name]) => [name, id]));
export const options: GridOptions<Row> = {
columns: [
{ id: 'title', title: 'Task', width: 190 },
{ id: 'owner', title: 'Owner', width: 120 },
{ id: 'status', title: 'Status', width: 90 },
],
rows: [
{ id: 1, title: 'Send the quote', owner: 'Liam Carter', status: 'Active' },
{ id: 2, title: 'Review the contract', owner: 'Emma Stone', status: 'Pending' },
{ id: 3, title: 'Ship samples', owner: 'Noah Reed', status: 'Active' },
{ id: 4, title: 'Check inventory', owner: 'Liam Carter', status: 'Done' },
],
rowKey: 'id',
selection: 'single',
};
// Pick a row in the table and its values fill the form; 'Apply' the values edited in the form and that row changes.
// The form values (the draft) are held by the screen, and the table does not change before applying. Below is the JavaScript version.
let say: (message: LogText) => void = () => {};
const draft = { title: '', owner: OWNERS[0] as string };
const put = (id: string, value: string): void => {
const el = document.getElementById(id);
if (el instanceof HTMLInputElement || 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]);
draft.title = row?.title ?? '';
draft.owner = row?.owner ?? OWNERS[0] ?? '';
put('title', draft.title);
put('owner', ID_BY_OWNER[draft.owner] ?? draft.owner);
log(row ? `Filled the form with row '${row.title}'` : 'No row selected');
});
}
export const actions = {
title: (_api: GridApi<Row>, value?: string) => {
draft.title = value ?? '';
},
owner: (_api: GridApi<Row>, value?: string) => {
draft.owner = value === undefined ? draft.owner : (OWNER_BY_ID[value] ?? value);
},
apply: (api: GridApi<Row>) => {
const key = api.getSelectedKeys()[0];
const row = key === undefined ? undefined : api.getRowByKey(key);
if (!row) return say('Select a row in the table first');
if (draft.title.trim() === '') return say('The task name cannot be empty');
api.updateRows([{ ...row, title: draft.title.trim(), owner: draft.owner }]);
say(`Applied to row '${draft.title.trim()}' (owner ${draft.owner})`);
},
};