본문으로 건너뛰기Skip to content
예제 첫화면Examples home

표에서 고른 행을 폼에서 고쳐 적용하기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.

활용Intermediate
직접 써 보기Live preview표에서 고른 행을 폼에서 고쳐 적용하기Edit the selected row in a form and apply 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)

빌드 도구(Vite 등)를 쓰는 프로젝트용 모듈 파일이에요. 설치 없이 바로 돌려 보려면 HTML 탭이나 직접 해보기를 쓰세요.

버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.

index.html
<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>
main.js
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));
example.js
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})`,
    });
  },
};

Module files for a project with a bundler such as Vite. To run it with no setup, use the HTML tab or Try it.

When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.

index.html
<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>
main.js
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));
example.js
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})`);
  },
};