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

스크롤해도 붙어 있는 그룹 머리글Group headers that stay pinned while scrolling

groupRows.sticky: true면 지금 보는 그룹의 머리글 행이 컬럼 헤더 아래에 붙어 있어요. 그룹이 중첩되면 바깥 그룹부터 쌓이고(최대 3단계), 붙은 행을 누르면 그 그룹이 접혀요.With groupRows.sticky: true the header row of the group you are inside stays under the column header. Nested groups stack outermost first (up to 3 levels) and a click on a pinned row collapses that group.

입문Beginner
직접 써 보기Live preview스크롤해도 붙어 있는 그룹 머리글Group headers that stay pinned while scrolling

코드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="sticky">
      <option value="on" selected>켜기</option>
      <option value="off">끄기</option>
    </select>
    <select aria-label="그룹 단계" id="levels">
      <option value="two" selected>팀 > 도시</option>
      <option value="one">팀만</option>
    </select>
    <button type="button" id="jump">중간으로 이동</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, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('sticky').addEventListener('change', (event) => actions.sticky(api, event.target.value));
document.getElementById('levels').addEventListener('change', (event) => actions.levels(api, event.target.value));
document.getElementById('jump').addEventListener('click', () => actions.jump(api));
example.js
const teams = ['개발', '디자인', '기획', '영업'];
const cities = ['서울', '부산'];
const names = [
  '김서연',
  '이준호',
  '박민지',
  '최도윤',
  '정하은',
  '강지후',
  '윤서아',
  '임태민',
  '한소희',
  '오세훈',
];
const rows = Array.from({ length: 96 }, (_, i) => ({
  id: i + 1,
  name: `${names[i % names.length]} ${Math.floor(i / names.length) + 1}`,
  team: teams[Math.floor(i / 24)],
  city: cities[Math.floor(i / 12) % 2],
  salary: 42 + ((i * 7) % 31),
}));
// groupRows.sticky: true면 스크롤해도 지금 보는 그룹의 머리글 행이 헤더 아래에 붙어 있어요(중첩은 마지막 3단계까지).
export const options = {
  columns: [
    { id: 'name', title: '이름', width: 150 },
    { id: 'team', title: '팀', width: 110 },
    { id: 'city', title: '도시', width: 110 },
    {
      id: 'salary',
      title: '연봉 ($1,000)',
      kind: 'number',
      width: 140,
      align: 'end',
      aggregate: 'sum',
    },
  ],
  rows,
  rowKey: 'id',
  grouping: { by: ['team', 'city'], expandDepth: 'all' },
  groupRows: { sticky: true },
};
export function setup(_api, log) {
  log({
    ko: '아래로 스크롤하면 지금 보는 팀과 도시의 머리글 행이 헤더 아래에 붙어 있어요. 붙은 행을 누르면 그 그룹이 접혀요.',
    en: 'Scroll down: the header rows of the team and city you are in stay under the column header. Click a pinned row to collapse its group.',
  });
  return () => undefined;
}
export const actions = {
  sticky: (api, value) => api.setOptions({ groupRows: { sticky: value !== 'off' } }),
  levels: (api, value) => api.setOptions({
    grouping: { by: value === 'one' ? ['team'] : ['team', 'city'], expandDepth: 'all' },
  }),
  jump: (api) => api.scrollToIndex(60),
};

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="Pinning" id="sticky">
      <option value="on" selected>On</option>
      <option value="off">Off</option>
    </select>
    <select aria-label="Group levels" id="levels">
      <option value="two" selected>Team > City</option>
      <option value="one">Team only</option>
    </select>
    <button type="button" id="jump">Jump to the middle</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, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('sticky').addEventListener('change', (event) => actions.sticky(api, event.target.value));
document.getElementById('levels').addEventListener('change', (event) => actions.levels(api, event.target.value));
document.getElementById('jump').addEventListener('click', () => actions.jump(api));
example.js
const teams = ['Dev', 'Design', 'Planning', 'Sales'];
const cities = ['New York', 'Boston'];
const names = [
  'Liam Carter',
  'Emma Stone',
  'Noah Kim',
  'Olivia Reed',
  'Ava Brooks',
  'Mason Lee',
  'Sophia Hall',
  'Lucas Wood',
  'Mia Grant',
  'Ethan Cole',
];
const rows = Array.from({ length: 96 }, (_, i) => ({
  id: i + 1,
  name: `${names[i % names.length]} ${Math.floor(i / names.length) + 1}`,
  team: teams[Math.floor(i / 24)],
  city: cities[Math.floor(i / 12) % 2],
  salary: 42 + ((i * 7) % 31),
}));
// groupRows.sticky: true keeps the header row of the group you are inside under the column header while scrolling (up to the last 3 nested levels).
export const options = {
  columns: [
    { id: 'name', title: 'Name', width: 150 },
    { id: 'team', title: 'Team', width: 110 },
    { id: 'city', title: 'City', width: 110 },
    {
      id: 'salary',
      title: 'Salary ($1,000)',
      kind: 'number',
      width: 140,
      align: 'end',
      aggregate: 'sum',
    },
  ],
  rows,
  rowKey: 'id',
  grouping: { by: ['team', 'city'], expandDepth: 'all' },
  groupRows: { sticky: true },
};
export function setup(_api, log) {
  log('Scroll down: the header rows of the team and city you are in stay under the column header. Click a pinned row to collapse its group.');
  return () => undefined;
}
export const actions = {
  sticky: (api, value) => api.setOptions({ groupRows: { sticky: value !== 'off' } }),
  levels: (api, value) => api.setOptions({
    grouping: { by: value === 'one' ? ['team'] : ['team', 'city'], expandDepth: 'all' },
  }),
  jump: (api) => api.scrollToIndex(60),
};