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

고정된 머리글과 줄 번호Fixed header and row numbers

줄을 스크롤해도 머리글은 맨 위에 그대로 있고, 줄 번호 컬럼은 화면에 보이는 순서를 세요. 그래서 정렬하거나 필터를 걸면 줄 번호가 다시 1번부터 시작해요. 줄 번호는 설정으로 켜고 끌 수 있어요.The header stays on top while the rows scroll, and the row number column counts the lines as shown, so sorting or filtering restarts at 1. Row numbers can be turned on and off with a setting.

입문Beginner
직접 써 보기Live preview고정된 머리글과 줄 번호Fixed header and row numbers

코드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="numbers">
      <option value="on" selected>켬 (기본)</option>
      <option value="off">끔</option>
    </select>
    <button type="button" id="middle">중간으로 가기</button>
    <button type="button" id="sortSalary">월급 큰 순</button>
    <button type="button" id="onlyDesign">디자인팀만 보기</button>
    <button type="button" id="reset">정렬·필터 풀기</button>
  </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('numbers').addEventListener('change', (event) => actions.numbers(api, event.target.value));
document.getElementById('middle').addEventListener('click', () => actions.middle(api));
document.getElementById('sortSalary').addEventListener('click', () => actions.sortSalary(api));
document.getElementById('onlyDesign').addEventListener('click', () => actions.onlyDesign(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));
example.js
const names = ['김민준', '이서연', '박지호', '최유나', '정도윤', '한소희'];
const depts = ['개발팀', '디자인팀', '영업팀', '인사팀'];
// 줄이 스크롤돼도 머리글은 맨 위에 그대로 있어요. 줄 번호 컬럼은 '화면에 보이는 순서'를 세기 때문에
// 정렬하거나 필터를 걸면 다시 1번부터 시작해요.
export const options = {
  columns: [
    { id: 'name', title: '이름', width: 150 },
    { id: 'dept', title: '부서', width: 130 },
    {
      id: 'salary',
      title: '월급(만원)',
      kind: 'number',
      width: 130,
      align: 'end',
      formatValue: (value) => Number(value).toLocaleString('ko-KR'),
    },
  ],
  rows: Array.from({ length: 200 }, (_, i) => ({
    id: i + 1,
    name: `${names[i % 6]} ${Math.floor(i / 6) + 1}`,
    dept: depts[i % 4],
    salary: 300 + ((i * 79) % 500),
  })),
  rowKey: 'id',
  showRowNumbers: true,
  headerHeight: 40,
};
export const actions = {
  numbers: (api, value) => api.setOptions({ showRowNumbers: value !== 'off' }),
  sortSalary: (api) => {
    api.setSortModel([{ columnId: 'salary', direction: 'desc' }]);
    api.scrollToIndex(0); // 정렬하면 맨 위로 올라가서 1번부터 보여 줘요
  },
  onlyDesign: (api) => api.setFilterModel({
    match: 'all',
    items: [{ columnId: 'dept', operator: 'equals', value: '디자인팀' }],
  }),
  reset: (api) => {
    api.setSortModel([]);
    api.setFilterModel(null);
    api.scrollToIndex(0);
  },
  middle: (api) => api.scrollToIndex(Math.min(100, api.getVisibleRowCount() - 1)),
};

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="Row numbers" id="numbers">
      <option value="on" selected>On (default)</option>
      <option value="off">Off</option>
    </select>
    <button type="button" id="middle">Go to middle</button>
    <button type="button" id="sortSalary">Salary high to low</button>
    <button type="button" id="onlyDesign">Design team only</button>
    <button type="button" id="reset">Clear sort and filter</button>
  </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('numbers').addEventListener('change', (event) => actions.numbers(api, event.target.value));
document.getElementById('middle').addEventListener('click', () => actions.middle(api));
document.getElementById('sortSalary').addEventListener('click', () => actions.sortSalary(api));
document.getElementById('onlyDesign').addEventListener('click', () => actions.onlyDesign(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));
example.js
const names = [
  'Liam Carter',
  'Emma Stone',
  'Noah Reed',
  'Olivia Hayes',
  'Ethan Brooks',
  'Ava Morgan',
];
const depts = ['Dev', 'Design', 'Sales', 'HR'];
// The header stays at the top while rows scroll. The row-number column counts the order as shown on screen,
// so it starts again from 1 after sorting or filtering.
export const options = {
  columns: [
    { id: 'name', title: 'Name', width: 170 },
    { id: 'dept', title: 'Dept', width: 130 },
    {
      id: 'salary',
      title: 'Monthly pay ($)',
      kind: 'number',
      width: 150,
      align: 'end',
      formatValue: (value) => Number(value).toLocaleString('en-US'),
    },
  ],
  rows: Array.from({ length: 200 }, (_, i) => ({
    id: i + 1,
    name: `${names[i % 6]} ${Math.floor(i / 6) + 1}`,
    dept: depts[i % 4],
    salary: 300 + ((i * 79) % 500),
  })),
  rowKey: 'id',
  showRowNumbers: true,
  headerHeight: 40,
};
export const actions = {
  numbers: (api, value) => api.setOptions({ showRowNumbers: value !== 'off' }),
  sortSalary: (api) => {
    api.setSortModel([{ columnId: 'salary', direction: 'desc' }]);
    api.scrollToIndex(0); // sorting jumps to the top so numbering shows from 1
  },
  onlyDesign: (api) => api.setFilterModel({
    match: 'all',
    items: [{ columnId: 'dept', operator: 'equals', value: 'Design' }],
  }),
  reset: (api) => {
    api.setSortModel([]);
    api.setFilterModel(null);
    api.scrollToIndex(0);
  },
  middle: (api) => api.scrollToIndex(Math.min(100, api.getVisibleRowCount() - 1)),
};