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

컬럼을 다른 그룹으로 옮기기Move a column to another group

moveColumnToGroup은 컬럼을 헤더 그룹 사이로 옮기거나(null이면 그룹에서 빼요) getParentGroup으로 지금 속한 그룹을 물어봐요. 그룹의 마지막 컬럼이 빠지면 빈 그룹은 사라져요.moveColumnToGroup moves a column between header groups (null takes it out) and getParentGroup tells which group it is in. A group that loses its last column disappears.

활용Intermediate
직접 써 보기Live preview컬럼을 다른 그룹으로 옮기기Move a column to another group

코드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="toB">연봉을 사람 그룹으로</button>
    <button type="button" id="out">연봉을 그룹에서 빼기</button>
    <button type="button" id="where">소속 보기</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('toB').addEventListener('click', () => actions.toB(api));
document.getElementById('out').addEventListener('click', () => actions.out(api));
document.getElementById('where').addEventListener('click', () => actions.where(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: 'person', title: '사람', children: ['name', 'team'] },
    { id: 'hr', title: '인사', children: ['city', 'salary'] },
  ],
};
export function setup(_api, log) {
  say = log;
  return () => { };
}
export const actions = {
  toB: (api) => {
    api.moveColumnToGroup('salary', 'person');
    // 그룹만 바꾸면 연봉은 제자리에 남아 '사람' 그룹이 둘로 갈라져요. 팀 바로 뒤로 옮겨 한 덩어리로 만들어요.
    api.moveColumn('salary', api.getColumnOrder().indexOf('team') + 1);
    say({ ko: "연봉을 '사람' 그룹으로 옮겼어요", en: "Moved Salary to the 'person' group" });
  },
  out: (api) => {
    api.moveColumnToGroup('salary', null);
    say({ ko: '연봉을 그룹에서 뺐어요', en: 'Took Salary out of its group' });
  },
  where: (api) => {
    const group = api.getParentGroup('salary');
    say(group === null
      ? { ko: '연봉은 어느 그룹에도 없어요', en: 'Salary is in no group' }
      : { ko: `연봉의 그룹: ${group.title}`, en: `Salary group: ${group.title}` });
  },
};

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="toB">Salary to People group</button>
    <button type="button" id="out">Salary out of groups</button>
    <button type="button" id="where">Where is Salary</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('toB').addEventListener('click', () => actions.toB(api));
document.getElementById('out').addEventListener('click', () => actions.out(api));
document.getElementById('where').addEventListener('click', () => actions.where(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: 'person', title: 'People', children: ['name', 'team'] },
    { id: 'hr', title: 'HR', children: ['city', 'salary'] },
  ],
};
export function setup(_api, log) {
  say = log;
  return () => { };
}
export const actions = {
  toB: (api) => {
    api.moveColumnToGroup('salary', 'person');
    // Changing only the group leaves Salary where it was and splits the 'People' group in two. Move it right after Team to make one block.
    api.moveColumn('salary', api.getColumnOrder().indexOf('team') + 1);
    say("Moved Salary to the 'person' group");
  },
  out: (api) => {
    api.moveColumnToGroup('salary', null);
    say('Took Salary out of its group');
  },
  where: (api) => {
    const group = api.getParentGroup('salary');
    say(group === null ? 'Salary is in no group' : `Salary group: ${group.title}`);
  },
};