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

컬럼을 왼쪽·오른쪽에 고정하기Pin columns to the left or the right

번호와 고객사는 왼쪽에, 합계는 오른쪽에 고정하고 가운데 월별 컬럼만 가로로 스크롤해요. 코드로 컬럼을 더 고정하거나 풀 수도 있어요.Pin No and Customer to the left and H1 total to the right while the monthly columns in the middle scroll. Pin or unpin more columns from code.

입문Beginner
직접 써 보기Live preview컬럼을 왼쪽·오른쪽에 고정하기Pin columns to the left or the right

코드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="pinJan">1월 왼쪽에 고정</button>
    <button type="button" id="pinJun">6월 오른쪽에 고정</button>
    <button type="button" id="unpinAll">고정 모두 풀기</button>
  </div>
  <div id="grid" style="height: 320px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options } from './example';
const api = createGrid(document.getElementById('grid'), options);
document.getElementById('pinJan').addEventListener('click', () => actions.pinJan(api));
document.getElementById('pinJun').addEventListener('click', () => actions.pinJun(api));
document.getElementById('unpinAll').addEventListener('click', () => actions.unpinAll(api));
example.js
const customers = ['한빛물류', '푸른하늘식품', '북극성무역', '다온상사', '새벽농원'];
// pinned: 'start'는 왼쪽, 'end'는 오른쪽 가장자리에 붙고, 가운데 컬럼만 가로로 스크롤돼요.
export const options = {
  columns: [
    { id: 'id', title: '번호', width: 60, pinned: 'start', align: 'end' },
    { id: 'customer', title: '고객사', width: 160, pinned: 'start' },
    ...['jan', 'feb', 'mar', 'apr', 'may', 'jun'].map((id, i) => ({
      id,
      title: `${i + 1}월`,
      kind: 'number',
      width: 150,
      align: 'end',
      formatValue: (value) => Number(value).toLocaleString('ko-KR'),
    })),
    {
      id: 'total',
      title: '상반기 합계',
      kind: 'number',
      width: 150,
      pinned: 'end',
      align: 'end',
      formatValue: (value) => Number(value).toLocaleString('ko-KR'),
    },
  ],
  rows: customers.map((customer, i) => {
    const sales = (k) => (i + 2) * k * 1000;
    return {
      id: i + 1,
      customer,
      jan: sales(1),
      feb: sales(2),
      mar: sales(3),
      apr: sales(4),
      may: sales(5),
      jun: sales(6),
      total: sales(21),
    };
  }),
  rowKey: 'id',
};
export const actions = {
  pinJan: (api) => api.pinColumn('jan', 'start'),
  pinJun: (api) => api.pinColumn('jun', 'end'),
  // 고정을 풀면 컬럼은 가운데 영역의 가장자리(왼쪽 고정은 왼쪽 끝, 오른쪽 고정은 오른쪽 끝)로 가요.
  // 그래서 왼쪽 고정은 뒤에서부터, 오른쪽 고정은 앞에서부터 풀어야 처음 순서가 그대로 돌아와요.
  unpinAll: (api) => {
    const state = api.getColumnState();
    const start = state.filter((c) => c.pinned === 'start').reverse();
    const end = state.filter((c) => c.pinned === 'end');
    for (const c of [...start, ...end])
      api.pinColumn(c.id, null);
  },
};

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="pinJan">Pin Jan left</button>
    <button type="button" id="pinJun">Pin Jun right</button>
    <button type="button" id="unpinAll">Unpin all</button>
  </div>
  <div id="grid" style="height: 320px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options } from './example';
const api = createGrid(document.getElementById('grid'), options);
document.getElementById('pinJan').addEventListener('click', () => actions.pinJan(api));
document.getElementById('pinJun').addEventListener('click', () => actions.pinJun(api));
document.getElementById('unpinAll').addEventListener('click', () => actions.unpinAll(api));
example.js
const customers = [
  'Northwind Freight',
  'Bluesky Foods',
  'Polaris Trading',
  'Harbor & Co',
  'Sunrise Farms',
];
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'];
// pinned: 'start' sticks to the left edge and 'end' to the right edge, and only the middle columns scroll sideways.
export const options = {
  columns: [
    { id: 'id', title: 'No', width: 60, pinned: 'start', align: 'end' },
    { id: 'customer', title: 'Customer', width: 160, pinned: 'start' },
    ...['jan', 'feb', 'mar', 'apr', 'may', 'jun'].map((id, i) => ({
      id,
      title: MONTHS[i],
      kind: 'number',
      width: 150,
      align: 'end',
      formatValue: (value) => Number(value).toLocaleString('en-US'),
    })),
    {
      id: 'total',
      title: 'H1 total',
      kind: 'number',
      width: 150,
      pinned: 'end',
      align: 'end',
      formatValue: (value) => Number(value).toLocaleString('en-US'),
    },
  ],
  rows: customers.map((customer, i) => {
    const sales = (k) => (i + 2) * k * 1000;
    return {
      id: i + 1,
      customer,
      jan: sales(1),
      feb: sales(2),
      mar: sales(3),
      apr: sales(4),
      may: sales(5),
      jun: sales(6),
      total: sales(21),
    };
  }),
  rowKey: 'id',
};
export const actions = {
  pinJan: (api) => api.pinColumn('jan', 'start'),
  pinJun: (api) => api.pinColumn('jun', 'end'),
  // Unpinning sends a column to the edge of the middle area (a left pin to its left end, a right pin to its right end).
  // So unpin left pins from the back and right pins from the front to get the original order back.
  unpinAll: (api) => {
    const state = api.getColumnState();
    const start = state.filter((c) => c.pinned === 'start').reverse();
    const end = state.filter((c) => c.pinned === 'end');
    for (const c of [...start, ...end])
      api.pinColumn(c.id, null);
  },
};