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

위쪽 줄 붙여 두기Pin rows to the top

합계·평균 같은 요약 줄을 맨 위에 붙여 두면, 아래로 스크롤해도 헤더 바로 밑에 그대로 보여요. 정렬할 때 이 줄을 같이 정렬할지도 고를 수 있어요.Pin summary rows such as total and average under the header so they stay visible while scrolling. Choose whether sorting moves them too.

입문Beginner
직접 써 보기Live preview위쪽 줄 붙여 두기Pin rows to the top

코드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="count">
      <option value="3">3줄</option>
      <option value="2">2줄</option>
      <option value="1">1줄</option>
      <option value="0">고정 안 함</option>
    </select>
    <select aria-label="정렬할 때 고정 줄" id="sortMode">
      <option value="stay">그대로 두기</option>
      <option value="with">같이 정렬</option>
    </select>
    <button type="button" id="sort">판매량 큰 순 정렬</button>
    <button type="button" id="clearSort">정렬 해제</button>
  </div>
  <div id="grid" style="height: 340px"></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('count').addEventListener('change', (event) => actions.count(api, event.target.value));
document.getElementById('sortMode').addEventListener('change', (event) => actions.sortMode(api, event.target.value));
document.getElementById('sort').addEventListener('click', () => actions.sort(api));
document.getElementById('clearSort').addEventListener('click', () => actions.clearSort(api));
example.js
// 아래 40개 품목의 합계·평균·최대를 계산해서 맨 위 세 줄로 놓아요.
// pinnedRows: 3 이면 그 세 줄은 스크롤해도 헤더 아래에 붙어 있어요.
const regions = ['서울점', '부산점', '대구점'];
const items = Array.from({ length: 40 }, (_, i) => ({
  id: i + 4,
  group: regions[i % regions.length],
  item: `상품 ${String(i + 1).padStart(2, '0')}`,
  amount: 100 + ((i * 53) % 400),
}));
const sum = items.reduce((total, row) => total + row.amount, 0);
const rows = [
  { id: 1, group: '요약', item: '전체 합계', amount: sum },
  { id: 2, group: '요약', item: '평균', amount: Math.round(sum / items.length) },
  { id: 3, group: '요약', item: '최대', amount: Math.max(...items.map((row) => row.amount)) },
  ...items,
];
// 고정 줄 수와 "정렬에 같이 따를지"를 기억해 두었다가 함께 넘겨요.
let count = 3;
let sortToo = false;
export const options = {
  columns: [
    { id: 'group', title: '구분', width: 110 },
    { id: 'item', title: '항목', width: 160 },
    { id: 'amount', title: '판매량', kind: 'number', width: 120, align: 'end' },
  ],
  rows,
  rowKey: 'id',
  pinnedRows: { count, sort: sortToo },
};
const apply = (api) => api.setOptions({ pinnedRows: { count, sort: sortToo } });
export const actions = {
  count: (api, value) => {
    count = Number(value ?? 0);
    apply(api);
  },
  // 기본은 "그대로". 정렬해도 고정 줄은 위에 원래 순서로 남고, 나머지 줄만 정렬돼요.
  sortMode: (api, value) => {
    sortToo = value === 'with';
    apply(api);
  },
  sort: (api) => api.setSortModel([{ columnId: 'amount', direction: 'desc' }]),
  clearSort: (api) => api.setSortModel([]),
};

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="count">
      <option value="3">3 rows</option>
      <option value="2">2 rows</option>
      <option value="1">1 row</option>
      <option value="0">None</option>
    </select>
    <select aria-label="Pinned rows when sorting" id="sortMode">
      <option value="stay">Stay put</option>
      <option value="with">Sort too</option>
    </select>
    <button type="button" id="sort">Sort by quantity</button>
    <button type="button" id="clearSort">Clear sort</button>
  </div>
  <div id="grid" style="height: 340px"></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('count').addEventListener('change', (event) => actions.count(api, event.target.value));
document.getElementById('sortMode').addEventListener('change', (event) => actions.sortMode(api, event.target.value));
document.getElementById('sort').addEventListener('click', () => actions.sort(api));
document.getElementById('clearSort').addEventListener('click', () => actions.clearSort(api));
example.js
// Compute the total, average and maximum of the 40 items below and place them as the top three rows.
// With pinnedRows: 3, those three rows stay attached under the header while scrolling.
const regions = ['Seoul Store', 'Busan Store', 'Daegu Store'];
const items = Array.from({ length: 40 }, (_, i) => ({
  id: i + 4,
  group: regions[i % regions.length],
  item: `Product ${String(i + 1).padStart(2, '0')}`,
  amount: 100 + ((i * 53) % 400),
}));
const sum = items.reduce((total, row) => total + row.amount, 0);
const rows = [
  { id: 1, group: 'Summary', item: 'Grand total', amount: sum },
  { id: 2, group: 'Summary', item: 'Average', amount: Math.round(sum / items.length) },
  { id: 3, group: 'Summary', item: 'Maximum', amount: Math.max(...items.map((row) => row.amount)) },
  ...items,
];
// Remember the number of pinned rows and whether they "follow the sort", and pass them together.
let count = 3;
let sortToo = false;
export const options = {
  columns: [
    { id: 'group', title: 'Group', width: 110 },
    { id: 'item', title: 'Item', width: 160 },
    { id: 'amount', title: 'Units sold', kind: 'number', width: 120, align: 'end' },
  ],
  rows,
  rowKey: 'id',
  pinnedRows: { count, sort: sortToo },
};
const apply = (api) => api.setOptions({ pinnedRows: { count, sort: sortToo } });
export const actions = {
  count: (api, value) => {
    count = Number(value ?? 0);
    apply(api);
  },
  // The default is "stay put". Even when sorting, the pinned rows stay on top in their original order and only the other rows are sorted.
  sortMode: (api, value) => {
    sortToo = value === 'with';
    apply(api);
  },
  sort: (api) => api.setSortModel([{ columnId: 'amount', direction: 'desc' }]),
  clearSort: (api) => api.setSortModel([]),
};