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

페이지당 행 수를 사용자가 고르게 하기Let users pick how many rows per page

5, 10, 15건 중에서 한 페이지에 몇 건을 볼지 고를 수 있는 칸을 페이지 막대에 달아요. 개수를 바꾸면 전체 페이지 수가 바로 다시 계산돼요.Add a rows-per-page chooser (5, 10, 15) to the pager. Changing it recalculates the page count immediately.

입문Beginner
직접 써 보기Live preview페이지당 행 수를 사용자가 고르게 하기Let users pick how many rows per page

코드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="size">
      <option value="5">5건</option>
      <option value="10" selected>10건 (기본)</option>
      <option value="15">15건</option>
    </select>
  </div>
  <div id="grid" style="height: 600px"></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('size').addEventListener('change', (event) => actions.size(api, event.target.value));
example.js
const names = ['김민준', '이서연', '박지호', '최유나', '정도윤', '한소희', '오세훈', '윤지아'];
const teams = ['개발팀', '디자인팀', '영업팀', '인사팀'];
// pageSizes에 후보를 적으면 페이지 막대에 "페이지당 행 수" 선택 칸이 생겨요.
export const options = {
  columns: [
    { id: 'employee', title: '이름', width: 120 },
    { id: 'team', title: '팀', width: 120 },
    {
      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,
    employee: `${names[i % 8]} ${i + 1}`,
    team: teams[(i * 3) % 4],
    salary: 3800 + ((i * 537) % 4200),
  })),
  rowKey: 'id',
  pagination: { mode: 'client', pageSize: 10, pageSizes: [5, 10, 15] },
};
export function setup(api, log) {
  let lastSize = api.getPageSize();
  const off = api.on('pageChanged', ({ pageSize, pageCount, reason }) => {
    if (reason === 'size' || pageSize !== lastSize) {
      log({
        ko: `페이지당 ${lastSize}건 → ${pageSize}건 (전체 ${pageCount}페이지)`,
        en: `Rows per page ${lastSize} → ${pageSize} (${pageCount} pages in all)`,
      });
    }
    lastSize = pageSize;
    // 쪽 막대에서 크기를 바꿔도 위의 '코드로 바꾸기' 선택 목록이 같은 값을 보여 주게 맞춰요.
    const select = document.getElementById('size');
    if (select instanceof HTMLSelectElement)
      select.value = String(pageSize);
  });
  return () => off();
}
export const actions = {
  // 코드로도 바꿀 수 있어요.
  size: (api, value) => api.setPageSize(Number(value ?? 10)),
};

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="Set from code" id="size">
      <option value="5">5 rows</option>
      <option value="10" selected>10 rows (default)</option>
      <option value="15">15 rows</option>
    </select>
  </div>
  <div id="grid" style="height: 600px"></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('size').addEventListener('change', (event) => actions.size(api, event.target.value));
example.js
const names = [
  'Liam Carter',
  'Emma Stone',
  'Noah Kim',
  'Olivia Park',
  'Ethan Brown',
  'Mia Johnson',
  'Jack Miller',
  'Zoe Davis',
];
const teams = ['Dev', 'Design', 'Sales', 'HR'];
// List candidates in pageSizes and the pager gets a "rows per page" select.
export const options = {
  columns: [
    { id: 'employee', title: 'Name', width: 170 },
    { id: 'team', title: 'Team', width: 120 },
    {
      id: 'salary',
      title: 'Salary ($)',
      kind: 'number',
      width: 130,
      align: 'end',
      formatValue: (value) => Number(value).toLocaleString('en-US'),
    },
  ],
  rows: Array.from({ length: 200 }, (_, i) => ({
    id: i + 1,
    employee: `${names[i % 8]} ${i + 1}`,
    team: teams[(i * 3) % 4],
    salary: 38000 + ((i * 537) % 4200) * 10,
  })),
  rowKey: 'id',
  pagination: { mode: 'client', pageSize: 10, pageSizes: [5, 10, 15] },
};
export function setup(api, log) {
  let lastSize = api.getPageSize();
  const off = api.on('pageChanged', ({ pageSize, pageCount, reason }) => {
    if (reason === 'size' || pageSize !== lastSize) {
      log(`Rows per page ${lastSize} → ${pageSize} (${pageCount} pages in all)`);
    }
    lastSize = pageSize;
    // Even when the size is changed in the pager, keep the "change from code" select above showing the same value.
    const select = document.getElementById('size');
    if (select instanceof HTMLSelectElement)
      select.value = String(pageSize);
  });
  return () => off();
}
export const actions = {
  // You can also change it from code.
  size: (api, value) => api.setPageSize(Number(value ?? 10)),
};