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

표와 바깥 검색 상자·입력 폼 연결하기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).

활용Intermediate
직접 써 보기Live preview표와 바깥 검색 상자·입력 폼 연결하기Link the grid to a search box and form fields

코드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="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>
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('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));
example.js
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 });
  },
};

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="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>
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('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));
example.js
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 });
  },
};