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

컬럼 설정 일부만 바꾸기Patch part of a column

updateColumn은 컬럼 정의의 일부만 얕게 합쳐 바꿔요. id와 key는 바꿀 수 없고, 사용자가 끈 폭·순서·고정은 그대로 남아요. columnsChanged의 updated에 바뀐 id가 실려요.updateColumn shallow-merges a patch into one column definition. id and key cannot change, and the width, order and pin the user chose stay. columnsChanged carries the changed id in updated.

활용Intermediate
직접 써 보기Live preview컬럼 설정 일부만 바꾸기Patch part of 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="title">이름 제목 바꾸기</button>
    <button type="button" id="wide">연봉 폭 넓히기</button>
    <button type="button" id="align">팀 가운데 정렬</button>
    <button type="button" id="bad">금지된 변경</button>
  </div>
  <div id="grid" style="height: 220px"></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('click', () => actions.title(api));
document.getElementById('wide').addEventListener('click', () => actions.wide(api));
document.getElementById('align').addEventListener('click', () => actions.align(api));
document.getElementById('bad').addEventListener('click', () => actions.bad(api));
example.js
const COLUMNS = [
  { id: 'id', title: '사번', width: 80, align: 'end' },
  { id: 'name', title: '이름', width: 110 },
  { id: 'team', title: '팀', width: 110 },
  { id: 'city', title: '근무지', width: 100 },
  { id: 'salary', title: '연봉(만원)', kind: 'number', width: 120, align: 'end' },
];
const ROWS = [
  { id: 1001, name: '김민준', team: '개발팀', city: '서울', salary: 6200 },
  { id: 1002, name: '이서연', team: '디자인팀', city: '부산', salary: 4800 },
  { id: 1003, name: '박지호', team: '영업팀', city: '대구', salary: 7100 },
  { id: 1004, name: '최유나', team: '인사팀', city: '서울', salary: 3900 },
];
let say = () => { };
export const options = { columns: COLUMNS, rows: ROWS, rowKey: 'id' };
export function setup(api, log) {
  say = log;
  const off = api.on('columnsChanged', (e) => say({
    ko: `columnsChanged: ${e.reason ?? 'set'} ${(e.updated ?? []).join(',')}`,
    en: `columnsChanged: ${e.reason ?? 'set'} ${(e.updated ?? []).join(',')}`,
  }));
  return () => off();
}
let wide = false;
export const actions = {
  title: (api) => api.updateColumn('name', { title: `이름 ${Math.floor(Math.random() * 90 + 10)}` }),
  wide: (api) => {
    wide = !wide;
    api.updateColumn('salary', { width: wide ? 200 : 120 });
  },
  align: (api) => api.updateColumn('team', { align: 'center' }),
  bad: (api) => {
    try {
      api.updateColumn('name', { id: 'other' });
    }
    catch (error) {
      say({
        ko: `막혔어요: ${error.message}`,
        en: `Refused: ${error.message}`,
      });
    }
  },
};

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="title">Rename Name</button>
    <button type="button" id="wide">Toggle Salary width</button>
    <button type="button" id="align">Center Team</button>
    <button type="button" id="bad">Forbidden change</button>
  </div>
  <div id="grid" style="height: 220px"></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('click', () => actions.title(api));
document.getElementById('wide').addEventListener('click', () => actions.wide(api));
document.getElementById('align').addEventListener('click', () => actions.align(api));
document.getElementById('bad').addEventListener('click', () => actions.bad(api));
example.js
const COLUMNS = [
  { id: 'id', title: 'Emp. ID', width: 90, align: 'end' },
  { id: 'name', title: 'Name', width: 110 },
  { id: 'team', title: 'Team', width: 110 },
  { id: 'city', title: 'Office', width: 100 },
  { id: 'salary', title: 'Salary ($)', kind: 'number', width: 120, align: 'end' },
];
const ROWS = [
  { id: 1001, name: 'Liam Carter', team: 'Dev', city: 'Seoul', salary: 62000 },
  { id: 1002, name: 'Emma Stone', team: 'Design', city: 'Busan', salary: 48000 },
  { id: 1003, name: 'Noah Parker', team: 'Sales', city: 'Daegu', salary: 71000 },
  { id: 1004, name: 'Olivia Reed', team: 'HR', city: 'Seoul', salary: 39000 },
];
let say = () => { };
export const options = { columns: COLUMNS, rows: ROWS, rowKey: 'id' };
export function setup(api, log) {
  say = log;
  const off = api.on('columnsChanged', (e) => say(`columnsChanged: ${e.reason ?? 'set'} ${(e.updated ?? []).join(',')}`));
  return () => off();
}
let wide = false;
export const actions = {
  title: (api) => api.updateColumn('name', { title: `Name ${Math.floor(Math.random() * 90 + 10)}` }),
  wide: (api) => {
    wide = !wide;
    api.updateColumn('salary', { width: wide ? 200 : 120 });
  },
  align: (api) => api.updateColumn('team', { align: 'center' }),
  bad: (api) => {
    try {
      api.updateColumn('name', { id: 'other' });
    }
    catch (error) {
      say(`Refused: ${error.message}`);
    }
  },
};