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

컬럼 도구 모음으로 컬럼 숨기고 옮기기Hide and reorder columns with the column toolbar

columnToolbar를 켜면 그리드 위나 아래에 컬럼마다 칩이 하나 생겨요. 칩을 누르면(Space·Enter) 컬럼이 보이거나 숨고, 칩을 끌거나 Alt+←/→를 누르면 컬럼 순서가 바뀌어요. 숨긴 칩은 흐리게 보이고 각 칩은 aria-pressed로 상태를 알려요.Turn on columnToolbar and a strip above or below the grid shows one chip per column. Press a chip (Space or Enter) to hide or show the column, and drag a chip or press Alt+Left/Right to reorder. Hidden chips are dimmed and every chip reports its state with aria-pressed.

입문Beginner
직접 써 보기Live preview컬럼 도구 모음으로 컬럼 숨기고 옮기기Hide and reorder columns with the column toolbar

코드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">
    <select aria-label="칩 위치" id="position">
      <option value="top" selected>위</option>
      <option value="bottom">아래</option>
    </select>
    <select aria-label="칩 글자" id="labels">
      <option value="labels" selected>컬럼 제목 보이기</option>
      <option value="icons">아이콘만</option>
    </select>
    <button type="button" id="showAll">숨긴 컬럼 모두 보이기</button>
  </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('position').addEventListener('change', (event) => actions.position(api, event.target.value));
document.getElementById('labels').addEventListener('change', (event) => actions.labels(api, event.target.value));
document.getElementById('showAll').addEventListener('click', () => actions.showAll(api));
example.js
const names = ['김서연', '이준호', '박민지', '최도윤', '정하은', '강지후', '윤서아', '임태민'];
const teams = ['개발', '디자인', '영업', '인사'];
const cities = ['서울', '부산', '대구', '인천', '광주'];
// columnToolbar: true면 그리드 위에 컬럼마다 칩이 하나 생겨요. 칩을 누르면 컬럼이 보이거나 숨고,
// 칩을 끌거나 Alt+←/→를 누르면 컬럼 순서가 바뀌어요. 칩 모양은 { position, showLabels }로 정해요.
let toolbar = {
  position: 'top',
  showLabels: true,
};
export const options = {
  columns: [
    { id: 'id', title: '번호', width: 70, align: 'end' },
    { id: 'name', title: '이름', width: 120 },
    { id: 'team', title: '팀', width: 100 },
    { id: 'city', title: '지역', width: 100 },
    { id: 'email', title: '이메일', width: 200 },
    { id: 'joined', title: '입사일', width: 120 },
  ],
  rows: Array.from({ length: 12 }, (_, i) => ({
    id: i + 1,
    name: names[i % names.length],
    team: teams[(i * 3) % teams.length],
    city: cities[(i * 2) % cities.length],
    email: `user${i + 1}@example.com`,
    joined: `2023-${String(1 + (i % 12)).padStart(2, '0')}-${String(3 + i).padStart(2, '0')}`,
  })),
  rowKey: 'id',
  columnToolbar: true,
};
export function setup(api, log) {
  toolbar = { position: 'top', showLabels: true };
  log({
    ko: '칩을 눌러 컬럼을 숨기거나 보이고, 칩을 끌어 순서를 바꿔 보세요.',
    en: 'Press a chip to hide or show its column, and drag a chip to reorder.',
  });
  const offs = [
    api.on('columnVisibilityChanged', ({ columnId, hidden }) => log({
      ko: `${columnId} 컬럼을 ${hidden ? '숨겼어요' : '보여요'}`,
      en: `Column ${columnId} ${hidden ? 'hidden' : 'shown'}`,
    })),
    api.on('columnMoved', ({ columnId, order }) => log({
      ko: `${columnId} 이동 → ${order.join(' · ')}`,
      en: `${columnId} moved → ${order.join(' · ')}`,
    })),
  ];
  return () => offs.forEach((off) => off());
}
export const actions = {
  position: (api, value) => {
    toolbar = { ...toolbar, position: value === 'bottom' ? 'bottom' : 'top' };
    api.setOptions({ columnToolbar: toolbar });
  },
  labels: (api, value) => {
    toolbar = { ...toolbar, showLabels: value !== 'icons' };
    api.setOptions({ columnToolbar: toolbar });
  },
  showAll: (api) => api.showAllColumns(),
};

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">
    <select aria-label="Chip position" id="position">
      <option value="top" selected>Top</option>
      <option value="bottom">Bottom</option>
    </select>
    <select aria-label="Chip text" id="labels">
      <option value="labels" selected>Show titles</option>
      <option value="icons">Icons only</option>
    </select>
    <button type="button" id="showAll">Show all columns</button>
  </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('position').addEventListener('change', (event) => actions.position(api, event.target.value));
document.getElementById('labels').addEventListener('change', (event) => actions.labels(api, event.target.value));
document.getElementById('showAll').addEventListener('click', () => actions.showAll(api));
example.js
const names = [
  'Liam Carter',
  'Emma Stone',
  'Noah Hayes',
  'Olivia Reed',
  'Ethan Brooks',
  'Ava Foster',
  'Mason Clark',
  'Mia Turner',
];
const teams = ['Dev', 'Design', 'Sales', 'HR'];
const cities = ['Austin', 'Boston', 'Denver', 'Seattle', 'Portland'];
// With columnToolbar: true a strip above the grid shows one chip per column. Press a chip to hide or show its column,
// and drag a chip or press Alt+Left/Right to reorder. The strip itself is set with { position, showLabels }.
let toolbar = {
  position: 'top',
  showLabels: true,
};
export const options = {
  columns: [
    { id: 'id', title: 'No.', width: 70, align: 'end' },
    { id: 'name', title: 'Name', width: 130 },
    { id: 'team', title: 'Team', width: 100 },
    { id: 'city', title: 'City', width: 100 },
    { id: 'email', title: 'Email', width: 200 },
    { id: 'joined', title: 'Joined', width: 120 },
  ],
  rows: Array.from({ length: 12 }, (_, i) => ({
    id: i + 1,
    name: names[i % names.length],
    team: teams[(i * 3) % teams.length],
    city: cities[(i * 2) % cities.length],
    email: `user${i + 1}@example.com`,
    joined: `2023-${String(1 + (i % 12)).padStart(2, '0')}-${String(3 + i).padStart(2, '0')}`,
  })),
  rowKey: 'id',
  columnToolbar: true,
};
export function setup(api, log) {
  toolbar = { position: 'top', showLabels: true };
  log('Press a chip to hide or show its column, and drag a chip to reorder.');
  const offs = [
    api.on('columnVisibilityChanged', ({ columnId, hidden }) => log(`Column ${columnId} ${hidden ? 'hidden' : 'shown'}`)),
    api.on('columnMoved', ({ columnId, order }) => log(`${columnId} moved → ${order.join(' · ')}`)),
  ];
  return () => offs.forEach((off) => off());
}
export const actions = {
  position: (api, value) => {
    toolbar = { ...toolbar, position: value === 'bottom' ? 'bottom' : 'top' };
    api.setOptions({ columnToolbar: toolbar });
  },
  labels: (api, value) => {
    toolbar = { ...toolbar, showLabels: value !== 'icons' };
    api.setOptions({ columnToolbar: toolbar });
  },
  showAll: (api) => api.showAllColumns(),
};