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

위쪽 줄과 왼쪽 컬럼을 같이 붙이기Pin rows and columns together

합계 줄은 위에, 구분·품목 컬럼은 왼쪽에 붙여요. 가로세로로 어느 쪽으로 스크롤해도 기준이 되는 줄과 컬럼이 항상 눈앞에 남아요.Totals pinned on top, group and item columns pinned on the left. Scroll either way and your reference rows and columns stay in view.

활용Intermediate
직접 써 보기Live preview위쪽 줄과 왼쪽 컬럼을 같이 붙이기Pin rows and columns together

코드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="rowCount">
      <option value="3">3줄</option>
      <option value="1">1줄</option>
      <option value="0">고정 안 함</option>
    </select>
    <select aria-label="고정 컬럼 수" id="colCount">
      <option value="2">2개</option>
      <option value="1">1개</option>
      <option value="0">고정 안 함</option>
    </select>
  </div>
  <div id="grid" style="height: 360px"></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('rowCount').addEventListener('change', (event) => actions.rowCount(api, event.target.value));
document.getElementById('colCount').addEventListener('change', (event) => actions.colCount(api, event.target.value));
example.js
// 위쪽 줄은 pinnedRows로, 왼쪽 컬럼은 컬럼의 pinned로 붙여요. 둘을 같이 쓰면
// 가로로도 세로로도 스크롤해도 "합계 줄"과 "구분·품목" 컬럼이 항상 보여요.
const years = ['y2020', 'y2021', 'y2022', 'y2023', 'y2024', 'y2025'];
const groups = ['곡류', '채소류', '과일류'];
const items = Array.from({ length: 36 }, (_, i) => {
  const base = 40 + ((i * 37) % 260);
  return {
    id: i + 4,
    group: groups[Math.floor(i / 12)],
    item: `품목 ${String(i + 1).padStart(2, '0')}`,
    y2020: base,
    y2021: base + 3,
    y2022: base + 5,
    y2023: base + 6,
    y2024: base + 9,
    y2025: base + 12,
  };
});
// 맨 위 세 줄: 전체 합계, 평균, 가장 큰 값. 아래 품목에서 계산해요.
const pick = (fn) => Object.fromEntries(years.map((y) => [y, fn(items.map((row) => row[y]))]));
const total = (values) => values.reduce((a, b) => a + b, 0);
const rows = [
  { id: 1, group: '요약', item: '전체 합계', ...pick(total) },
  { id: 2, group: '요약', item: '평균', ...pick((v) => Math.round(total(v) / v.length)) },
  { id: 3, group: '요약', item: '최대', ...pick((v) => Math.max(...v)) },
  ...items,
];
export const options = {
  columns: [
    { id: 'group', title: '구분', width: 110, pinned: 'start', merge: true },
    { id: 'item', title: '품목', width: 140, pinned: 'start' },
    ...years.map((id) => ({
      id,
      title: id.slice(1),
      kind: 'number',
      width: 150,
      align: 'end',
    })),
  ],
  headerGroups: [
    { id: 'early', title: '2020~2022', children: ['y2020', 'y2021', 'y2022'] },
    { id: 'late', title: '2023~2025', children: ['y2023', 'y2024', 'y2025'] },
  ],
  rows,
  rowKey: 'id',
  // 화면이 480px보다 좁으면 고정이 풀려서 모두 같이 스크롤되고, 넓어지면 다시 붙어요.
  pinMinWidth: 480,
  pinnedRows: 3,
};
export const actions = {
  rowCount: (api, value) => api.setOptions({ pinnedRows: Number(value ?? 0) }),
  colCount: (api, value) => {
    const count = Number(value ?? 0);
    const ids = api.getColumns().map((c) => c.id);
    // 뒤에서부터 풀어야 컬럼 순서가 그대로예요.
    [...ids].reverse().forEach((id) => api.pinColumn(id, null));
    ids.slice(0, count).forEach((id) => api.pinColumn(id, 'start'));
  },
};

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 rows" id="rowCount">
      <option value="3">3 rows</option>
      <option value="1">1 row</option>
      <option value="0">None</option>
    </select>
    <select aria-label="Pinned columns" id="colCount">
      <option value="2">2 columns</option>
      <option value="1">1 column</option>
      <option value="0">None</option>
    </select>
  </div>
  <div id="grid" style="height: 360px"></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('rowCount').addEventListener('change', (event) => actions.rowCount(api, event.target.value));
document.getElementById('colCount').addEventListener('change', (event) => actions.colCount(api, event.target.value));
example.js
// The top rows are pinned with pinnedRows and the left columns with the column's pinned. Used together,
// the "total rows" and the "group / item" columns stay visible whether you scroll horizontally or vertically.
const years = ['y2020', 'y2021', 'y2022', 'y2023', 'y2024', 'y2025'];
const groups = ['Grains', 'Vegetables', 'Fruits'];
const items = Array.from({ length: 36 }, (_, i) => {
  const base = 40 + ((i * 37) % 260);
  return {
    id: i + 4,
    group: groups[Math.floor(i / 12)],
    item: `Item ${String(i + 1).padStart(2, '0')}`,
    y2020: base,
    y2021: base + 3,
    y2022: base + 5,
    y2023: base + 6,
    y2024: base + 9,
    y2025: base + 12,
  };
});
// Top three rows: grand total, average, largest value. Computed from the items below.
const pick = (fn) => Object.fromEntries(years.map((y) => [y, fn(items.map((row) => row[y]))]));
const total = (values) => values.reduce((a, b) => a + b, 0);
const rows = [
  { id: 1, group: 'Summary', item: 'Grand total', ...pick(total) },
  { id: 2, group: 'Summary', item: 'Average', ...pick((v) => Math.round(total(v) / v.length)) },
  { id: 3, group: 'Summary', item: 'Maximum', ...pick((v) => Math.max(...v)) },
  ...items,
];
export const options = {
  columns: [
    { id: 'group', title: 'Group', width: 110, pinned: 'start', merge: true },
    { id: 'item', title: 'Item', width: 140, pinned: 'start' },
    ...years.map((id) => ({
      id,
      title: id.slice(1),
      kind: 'number',
      width: 150,
      align: 'end',
    })),
  ],
  headerGroups: [
    { id: 'early', title: '2020-2022', children: ['y2020', 'y2021', 'y2022'] },
    { id: 'late', title: '2023-2025', children: ['y2023', 'y2024', 'y2025'] },
  ],
  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,
  pinnedRows: 3,
};
export const actions = {
  rowCount: (api, value) => api.setOptions({ pinnedRows: Number(value ?? 0) }),
  colCount: (api, value) => {
    const count = Number(value ?? 0);
    const ids = api.getColumns().map((c) => c.id);
    // Release from the back so the column order stays the same.
    [...ids].reverse().forEach((id) => api.pinColumn(id, null));
    ids.slice(0, count).forEach((id) => api.pinColumn(id, 'start'));
  },
};