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

컬럼이 바뀔 때 누가 바꿨는지 알기Know who changed a column

컬럼의 폭·순서·고정·숨김이 바뀔 때마다 기록 창에 알려 줘요. 마우스, 키보드, 헤더 메뉴, 코드 중 어디서 바꿨는지도 함께 나와요. 사용자가 직접 한 선택만 저장하고 싶을 때, 코드가 한 변경과 구분하는 데 써요.Width, order, pin and visibility changes raise events that say whether they came from pointer, keyboard, menu or code (source). Use it to keep only the user's own choices as preferences.

활용Intermediate
직접 써 보기Live preview컬럼이 바뀔 때 누가 바꿨는지 알기Know who changed a column

코드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">
    <button type="button" id="resizeByCode">코드로 이름 폭 넓히기</button>
    <button type="button" id="moveByCode">코드로 금액을 맨 앞으로</button>
    <button type="button" id="hideByCode">코드로 팀 숨기기</button>
    <button type="button" id="pinByCode">코드로 이름 왼쪽에 고정</button>
    <button type="button" id="reset">처음 상태로</button>
  </div>
  <div id="grid" style="height: 250px"></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('resizeByCode').addEventListener('click', () => actions.resizeByCode(api));
document.getElementById('moveByCode').addEventListener('click', () => actions.moveByCode(api));
document.getElementById('hideByCode').addEventListener('click', () => actions.hideByCode(api));
document.getElementById('pinByCode').addEventListener('click', () => actions.pinByCode(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));
example.js
// columnMenu를 켜면 헤더 메뉴(⋮)로도 숨기기·고정을 할 수 있어서, 'menu'가 어떻게 오는지도 볼 수 있어요.
export const options = {
  columnMenu: true,
  columns: [
    { id: 'id', title: '번호', width: 80, align: 'end' },
    { id: 'name', title: '이름', width: 150 },
    { id: 'team', title: '팀', width: 130 },
    { id: 'amount', title: '금액(원)', kind: 'number', width: 130, align: 'end' },
  ],
  rows: [
    { id: 1, name: '김민준', team: '개발팀', amount: 4200000 },
    { id: 2, name: '이서연', team: '디자인팀', amount: 1850000 },
  ],
  rowKey: 'id',
};
// 컬럼 이벤트에는 변화가 어디서 왔는지(source)가 들어 있어요. 사용자의 선택만 저장하고 싶을 때 코드가 한 변경과 구분해요.
const FROM = {
  pointer: ['마우스로 직접', 'by mouse'],
  keyboard: ['키보드로', 'by keyboard'],
  menu: ['헤더 메뉴로', 'by header menu'],
  api: ['코드로', 'by code'],
};
const from = (source, lang) => FROM[source]?.[lang] ?? source;
const TITLE = { id: '번호', name: '이름', team: '팀', amount: '금액' };
const name = (id) => TITLE[id] ?? id;
const NAME_EN = { id: 'No', name: 'Name', team: 'Team', amount: 'Amount' };
const nameEn = (id) => NAME_EN[id] ?? id;
export function setup(api, log) {
  let timer;
  let by = 'api';
  const offs = [
    api.on('columnResized', (e) => log({
      ko: `${name(e.columnId)} 폭 ${Math.round(e.previousWidth)} → ${Math.round(e.width)}px (${from(e.source, 0)})`,
      en: `${nameEn(e.columnId)} width ${Math.round(e.previousWidth)} → ${Math.round(e.width)}px (${from(e.source, 1)})`,
    })),
    // 컬럼 하나를 옮기면 사이에 낀 컬럼들의 이동 이벤트도 연달아 와서, 잠깐 모았다가 새 순서를 한 줄로 알려 줘요.
    api.on('columnMoved', (e) => {
      by = e.source;
      clearTimeout(timer);
      timer = setTimeout(() => log({
        ko: `새 순서: ${api.getColumnOrder().map(name).join(' · ')} (${from(by, 0)})`,
        en: `New order: ${api.getColumnOrder().map(nameEn).join(' · ')} (${from(by, 1)})`,
      }), 80);
    }),
    api.on('columnPinned', (e) => log({
      ko: `${name(e.columnId)} 고정: ${e.pinned === 'start' ? '왼쪽' : e.pinned === 'end' ? '오른쪽' : '해제'} (${from(e.source, 0)})`,
      en: `${nameEn(e.columnId)} pinned: ${e.pinned === 'start' ? 'left' : e.pinned === 'end' ? 'right' : 'off'} (${from(e.source, 1)})`,
    })),
    api.on('columnVisibilityChanged', (e) => log({
      ko: `${name(e.columnId)} ${e.hidden ? '숨김' : '표시'} (${from(e.source, 0)})`,
      en: `${nameEn(e.columnId)} ${e.hidden ? 'hidden' : 'shown'} (${from(e.source, 1)})`,
    })),
  ];
  return () => {
    clearTimeout(timer);
    offs.forEach((off) => off());
  };
}
export const actions = {
  resizeByCode: (api) => api.resizeColumn('name', 220),
  moveByCode: (api) => api.moveColumn('amount', 0),
  pinByCode: (api) => api.pinColumn('name', 'start'),
  hideByCode: (api) => api.setColumnVisible('team', false),
  reset: (api) => api.resetColumnState(),
};

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">
    <button type="button" id="resizeByCode">Widen Name by code</button>
    <button type="button" id="moveByCode">Move Amount first by code</button>
    <button type="button" id="hideByCode">Hide Team by code</button>
    <button type="button" id="pinByCode">Pin Name to the left by code</button>
    <button type="button" id="reset">Reset</button>
  </div>
  <div id="grid" style="height: 250px"></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('resizeByCode').addEventListener('click', () => actions.resizeByCode(api));
document.getElementById('moveByCode').addEventListener('click', () => actions.moveByCode(api));
document.getElementById('hideByCode').addEventListener('click', () => actions.hideByCode(api));
document.getElementById('pinByCode').addEventListener('click', () => actions.pinByCode(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));
example.js
// With columnMenu on, the header menu (⋮) can also hide and pin, so you can see how 'menu' arrives too.
export const options = {
  columnMenu: true,
  columns: [
    { id: 'id', title: 'No', width: 80, align: 'end' },
    { id: 'name', title: 'Name', width: 150 },
    { id: 'team', title: 'Team', width: 130 },
    { id: 'amount', title: 'Amount ($)', kind: 'number', width: 130, align: 'end' },
  ],
  rows: [
    { id: 1, name: 'Liam Carter', team: 'Dev', amount: 4200 },
    { id: 2, name: 'Emma Stone', team: 'Design', amount: 1850 },
  ],
  rowKey: 'id',
};
// Column events carry where the change came from (source). Code can tell the user's own choices from changes it made itself, for example to save only the user's choices.
const FROM = {
  pointer: 'by mouse',
  keyboard: 'by keyboard',
  menu: 'by header menu',
  api: 'by code',
};
const from = (source) => FROM[source] ?? source;
const NAMES = { id: 'No', name: 'Name', team: 'Team', amount: 'Amount' };
const name = (id) => NAMES[id] ?? id;
export function setup(api, log) {
  let timer;
  let by = 'api';
  const offs = [
    api.on('columnResized', (e) => log(`${name(e.columnId)} width ${Math.round(e.previousWidth)} → ${Math.round(e.width)}px (${from(e.source)})`)),
    // Moving one column also sends move events for the columns in between, so collect them briefly and report the new order in one line.
    api.on('columnMoved', (e) => {
      by = e.source;
      clearTimeout(timer);
      timer = setTimeout(() => log(`New order: ${api.getColumnOrder().map(name).join(' · ')} (${from(by)})`), 80);
    }),
    api.on('columnPinned', (e) => log(`${name(e.columnId)} pinned: ${e.pinned === 'start' ? 'left' : e.pinned === 'end' ? 'right' : 'off'} (${from(e.source)})`)),
    api.on('columnVisibilityChanged', (e) => log(`${name(e.columnId)} ${e.hidden ? 'hidden' : 'shown'} (${from(e.source)})`)),
  ];
  return () => {
    clearTimeout(timer);
    offs.forEach((off) => off());
  };
}
export const actions = {
  resizeByCode: (api) => api.resizeColumn('name', 220),
  moveByCode: (api) => api.moveColumn('amount', 0),
  pinByCode: (api) => api.pinColumn('name', 'start'),
  hideByCode: (api) => api.setColumnVisible('team', false),
  reset: (api) => api.resetColumnState(),
};