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

컬럼 하나만 끼워 넣기Insert a single column

addColumn은 컬럼 정의 전체를 바꾸지 않고 하나만 더해요. 위치(index)와 헤더 그룹(groupId)을 고르고, fill을 켜면 defaultValue를 기존 행에 채워요. 지금까지 더하고 뺀 컬럼은 getAddedColumns/getRemovedColumns로 알 수 있어요.addColumn adds one column without replacing the whole list. Pick the position (index) and header group (groupId); fill writes defaultValue into existing rows. getAddedColumns and getRemovedColumns tell what changed so far.

활용Intermediate
직접 써 보기Live preview컬럼 하나만 끼워 넣기Insert a single 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="first">앞에 끼우기</button>
    <button type="button" id="group">그룹에 넣기</button>
    <button type="button" id="fill">보너스 채우기</button>
    <button type="button" id="remove">마지막 빼기</button>
    <button type="button" id="changes">변경 내역</button>
  </div>
  <div id="grid" style="height: 240px"></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('first').addEventListener('click', () => actions.first(api));
document.getElementById('group').addEventListener('click', () => actions.group(api));
document.getElementById('fill').addEventListener('click', () => actions.fill(api));
document.getElementById('remove').addEventListener('click', () => actions.remove(api));
document.getElementById('changes').addEventListener('click', () => actions.changes(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',
  headerGroups: [{ id: 'work', title: '근무', children: ['team', 'city'] }],
};
export function setup(_api, log) {
  say = log;
  return () => { };
}
// 조사는 마지막 글자 소리에 맞춘다(숫자는 읽는 소리: 1 일, 3 삼, 6 육, 7 칠, 8 팔은 받침이 있다).
const eulReul = (word) => (/[13678]$/.test(word) ? '을' : '를');
let seq = 0;
export const actions = {
  first: (api) => {
    const id = `memo${++seq}`;
    api.addColumn({ id, title: `메모 ${seq}`, width: 90 }, { index: 0 });
    say({ ko: `'${id}'${eulReul(id)} 맨 앞에 끼웠어요`, en: `Inserted '${id}' first` });
  },
  group: (api) => {
    const id = `grp${++seq}`;
    api.addColumn({ id, title: `근무 ${seq}`, width: 90 }, { groupId: 'work' });
    say({
      ko: `'${id}'${eulReul(id)} '근무' 그룹에 넣었어요`,
      en: `Added '${id}' to the Work group`,
    });
  },
  fill: (api) => {
    const id = `bonus${++seq}`;
    api.addColumn({
      id,
      title: '보너스',
      kind: 'number',
      width: 100,
      defaultValue: (row) => row.salary / 10,
    }, { fill: true });
    say({
      ko: `'${id}'${eulReul(id)} 더하고 기존 행에 보너스를 채웠어요`,
      en: `Added '${id}' and filled existing rows`,
    });
  },
  remove: (api) => {
    const last = api.getColumns().at(-1);
    if (last === undefined || api.getColumns().length <= 2)
      return say({ ko: '더 빼지 않아요', en: 'Keeping at least 2 columns' });
    api.removeColumn(last.id);
    say({ ko: `'${last.id}'${eulReul(last.id)} 뺐어요`, en: `Removed '${last.id}'` });
  },
  changes: (api) => {
    say({
      ko: `더한 컬럼: ${api.getAddedColumns().join(', ') || '없음'} / 뺀 컬럼: ${api.getRemovedColumns().join(', ') || '없음'}`,
      en: `Added: ${api.getAddedColumns().join(', ') || 'none'} / Removed: ${api.getRemovedColumns().join(', ') || 'none'}`,
    });
  },
};

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="first">Insert first</button>
    <button type="button" id="group">Add to group</button>
    <button type="button" id="fill">Add with fill</button>
    <button type="button" id="remove">Remove last</button>
    <button type="button" id="changes">Changes</button>
  </div>
  <div id="grid" style="height: 240px"></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('first').addEventListener('click', () => actions.first(api));
document.getElementById('group').addEventListener('click', () => actions.group(api));
document.getElementById('fill').addEventListener('click', () => actions.fill(api));
document.getElementById('remove').addEventListener('click', () => actions.remove(api));
document.getElementById('changes').addEventListener('click', () => actions.changes(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: 'Seattle', salary: 62000 },
  { id: 1002, name: 'Emma Stone', team: 'Design', city: 'Boston', salary: 48000 },
  { id: 1003, name: 'Noah Hayes', team: 'Sales', city: 'Denver', salary: 71000 },
  { id: 1004, name: 'Olivia Reed', team: 'HR', city: 'Seattle', salary: 39000 },
];
let say = () => { };
export const options = {
  columns: COLUMNS,
  rows: ROWS,
  rowKey: 'id',
  headerGroups: [{ id: 'work', title: 'Work', children: ['team', 'city'] }],
};
export function setup(_api, log) {
  say = log;
  return () => { };
}
let seq = 0;
export const actions = {
  first: (api) => {
    const id = `memo${++seq}`;
    api.addColumn({ id, title: `Memo ${seq}`, width: 90 }, { index: 0 });
    say(`Inserted '${id}' first`);
  },
  group: (api) => {
    const id = `grp${++seq}`;
    api.addColumn({ id, title: `Work ${seq}`, width: 90 }, { groupId: 'work' });
    say(`Added '${id}' to the Work group`);
  },
  fill: (api) => {
    const id = `bonus${++seq}`;
    api.addColumn({
      id,
      title: 'Bonus',
      kind: 'number',
      width: 100,
      defaultValue: (row) => row.salary / 10,
    }, { fill: true });
    say(`Added '${id}' and filled existing rows`);
  },
  remove: (api) => {
    const last = api.getColumns().at(-1);
    if (last === undefined || api.getColumns().length <= 2)
      return say('Keeping at least 2 columns');
    api.removeColumn(last.id);
    say(`Removed '${last.id}'`);
  },
  changes: (api) => {
    say(`Added: ${api.getAddedColumns().join(', ') || 'none'} / Removed: ${api.getRemovedColumns().join(', ') || 'none'}`);
  },
};