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

왼쪽·오른쪽 컬럼 붙여 두기Pin columns to the left and right

컬럼이 많아 가로로 스크롤해도 거래처 이름은 왼쪽에, 합계는 오른쪽에 그대로 남아요. 몇 개를 붙일지 직접 바꿔 볼 수 있어요.Scroll sideways through many columns while the customer stays on the left and the total on the right. Change how many are pinned.

입문Beginner
직접 써 보기Live preview왼쪽·오른쪽 컬럼 붙여 두기Pin columns to the left and 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">
    <select aria-label="왼쪽 고정" id="left">
      <option value="0">없음</option>
      <option value="1" selected>1개</option>
      <option value="2">2개</option>
      <option value="3">3개</option>
    </select>
    <select aria-label="오른쪽 고정" id="right">
      <option value="0">없음</option>
      <option value="1" selected>1개</option>
      <option value="2">2개</option>
    </select>
  </div>
  <div id="grid" style="height: 300px"></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('left').addEventListener('change', (event) => actions.left(api, event.target.value));
document.getElementById('right').addEventListener('change', (event) => actions.right(api, event.target.value));
example.js
const customers = [
  '한빛물류',
  '푸른식품',
  '북촌상회',
  '단풍리테일',
  '정상테크',
  '바다수산',
  '새봄약품',
  '은하전자',
];
const managers = ['김서연', '이준호', '박민지', '최도윤'];
const months = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8'];
// 가운데 컬럼이 많아서 가로로 스크롤돼요. 맨 왼쪽(고객)과 맨 오른쪽(합계)은 스크롤해도 그대로 있어요.
// 컬럼에 pinned: 'start'(왼쪽) 또는 'end'(오른쪽)를 주면 돼요.
const rows = customers.map((customer, i) => {
  const sales = months.map((_, k) => (3 + ((i * 5 + k * 7) % 9)) * 1_000_000 + k * 250_000);
  const [m1, m2, m3, m4, m5, m6, m7, m8] = sales;
  return {
    id: i + 1,
    customer,
    manager: managers[i % managers.length],
    m1,
    m2,
    m3,
    m4,
    m5,
    m6,
    m7,
    m8,
    total: sales.reduce((sum, v) => sum + v, 0),
    grade: ['A', 'B', 'A', 'C', 'B', 'A', 'C', 'B'][i],
  };
});
const won = (value) => `${Number(value).toLocaleString('ko-KR')}`;
export const options = {
  columns: [
    { id: 'customer', title: '거래처', width: 100, pinned: 'start' },
    { id: 'manager', title: '담당자', width: 110 },
    { id: 'grade', title: '등급', width: 80, align: 'center' },
    ...months.map((id, k) => ({
      id,
      title: `${k + 1}월`,
      kind: 'number',
      width: 130,
      align: 'end',
      formatValue: won,
    })),
    {
      id: 'total',
      title: '합계',
      kind: 'number',
      width: 110,
      align: 'end',
      pinned: 'end',
      formatValue: won,
    },
  ],
  rows,
  rowKey: 'id',
  // 화면이 480px보다 좁으면 고정이 풀려서 모두 같이 스크롤되고, 넓어지면 다시 붙어요.
  pinMinWidth: 480,
};
// 왼쪽·오른쪽에서 몇 개를 붙일지 기억해 두고, 바뀔 때마다 모든 컬럼에 다시 적용해요.
let left = 1;
let right = 1;
function applyPins(api) {
  const ids = api.getColumns().map((c) => c.id);
  // 먼저 전부 풀고(뒤에서부터 풀어야 컬럼 순서가 그대로예요), 왼쪽은 앞에서부터, 오른쪽은 끝에서부터 다시 붙여요.
  [...ids].reverse().forEach((id) => api.pinColumn(id, null));
  ids.slice(0, left).forEach((id) => api.pinColumn(id, 'start'));
  ids
    .slice(ids.length - right)
    .reverse()
    .forEach((id) => api.pinColumn(id, 'end'));
}
export const actions = {
  left: (api, value) => {
    left = Number(value ?? 0);
    applyPins(api);
  },
  right: (api, value) => {
    right = Number(value ?? 0);
    applyPins(api);
  },
};

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="Pinned left" id="left">
      <option value="0">None</option>
      <option value="1" selected>1 column</option>
      <option value="2">2 columns</option>
      <option value="3">3 columns</option>
    </select>
    <select aria-label="Pinned right" id="right">
      <option value="0">None</option>
      <option value="1" selected>1 column</option>
      <option value="2">2 columns</option>
    </select>
  </div>
  <div id="grid" style="height: 300px"></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('left').addEventListener('change', (event) => actions.left(api, event.target.value));
document.getElementById('right').addEventListener('change', (event) => actions.right(api, event.target.value));
example.js
const customers = [
  'Northwind Logistics',
  'Greenfield Foods',
  'Maple Trading',
  'Autumn Retail',
  'Summit Tech',
  'Ocean Seafood',
  'Springtime Pharma',
  'Galaxy Electronics',
];
const managers = ['Liam Carter', 'Emma Stone', 'Noah Brooks', 'Olivia Reed'];
const months = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8'];
const monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug'];
// With many middle columns, the grid scrolls horizontally. The leftmost (customer) and rightmost (total) columns stay in place while scrolling.
// Give a column pinned: 'start' (left) or 'end' (right).
const rows = customers.map((customer, i) => {
  const sales = months.map((_, k) => (3 + ((i * 5 + k * 7) % 9)) * 1_000_000 + k * 250_000);
  const [m1, m2, m3, m4, m5, m6, m7, m8] = sales;
  return {
    id: i + 1,
    customer,
    manager: managers[i % managers.length],
    m1,
    m2,
    m3,
    m4,
    m5,
    m6,
    m7,
    m8,
    total: sales.reduce((sum, v) => sum + v, 0),
    grade: ['A', 'B', 'A', 'C', 'B', 'A', 'C', 'B'][i],
  };
});
const money = (value) => `${Number(value).toLocaleString('en-US')}`;
export const options = {
  columns: [
    { id: 'customer', title: 'Account', width: 160, pinned: 'start' },
    { id: 'manager', title: 'Manager', width: 120 },
    { id: 'grade', title: 'Grade', width: 80, align: 'center' },
    ...months.map((id, k) => ({
      id,
      title: monthNames[k],
      kind: 'number',
      width: 130,
      align: 'end',
      formatValue: money,
    })),
    {
      id: 'total',
      title: 'Total',
      kind: 'number',
      width: 110,
      align: 'end',
      pinned: 'end',
      formatValue: money,
    },
  ],
  rows,
  rowKey: 'id',
  // On a screen narrower than 480px the pins are released and everything scrolls together; they reattach when it gets wider.
  pinMinWidth: 480,
};
// Remember how many columns to pin on the left and right, and reapply to all columns whenever they change.
let left = 1;
let right = 1;
function applyPins(api) {
  const ids = api.getColumns().map((c) => c.id);
  // Release all first (release from the back so the column order stays the same), then pin the left from the front and the right from the end.
  [...ids].reverse().forEach((id) => api.pinColumn(id, null));
  ids.slice(0, left).forEach((id) => api.pinColumn(id, 'start'));
  ids
    .slice(ids.length - right)
    .reverse()
    .forEach((id) => api.pinColumn(id, 'end'));
}
export const actions = {
  left: (api, value) => {
    left = Number(value ?? 0);
    applyPins(api);
  },
  right: (api, value) => {
    right = Number(value ?? 0);
    applyPins(api);
  },
};