Pagination
Add pagination with customizable templates and page navigation controls.
ZenGrid provides flexible pagination with customizable templates, multiple page sizes, and comprehensive navigation controls.
Configuration
Enable and configure pagination:
const grid = new ZenGrid(container, { pagination: { enabled: true, pageSize: 50, template: 'material', // 'simple' | 'material' | 'bootstrap' | 'compact' | 'full' position: 'bottom', // 'top' | 'bottom' | 'both' pageSizes: [25, 50, 100, 200] }});Pagination is disabled by default. Set pagination.enabled: true to activate it.
Pagination Templates
ZenGrid includes five built-in pagination templates:
Simple
Basic previous/next navigation with page numbers.
pagination: { enabled: true, template: 'simple'}Material
Material Design-inspired pagination controls.
pagination: { enabled: true, template: 'material'}Bootstrap
Bootstrap-styled pagination controls.
pagination: { enabled: true, template: 'bootstrap'}Compact
Minimal pagination with just essential controls.
pagination: { enabled: true, template: 'compact'}Full
Complete pagination with all navigation options and page size selector.
pagination: { enabled: true, template: 'full', pageSizes: [25, 50, 100, 200]}Use the full template when you want to give users maximum control over pagination, including page size selection.
Navigation Methods
Control pagination programmatically:
// Go to a specific page (1-based)grid.goToPage(5);
// Navigate sequentiallygrid.nextPage();grid.previousPage();
// Jump to boundariesgrid.firstPage();grid.lastPage();
// Change page sizegrid.setPageSize(100);Pagination State
Query the current pagination state:
// Get current page number (1-based)const currentPage = grid.getCurrentPage();
// Get current page sizeconst pageSize = grid.getPageSize();
// Get total number of pagesconst totalPages = grid.getTotalPages();
// Get complete pagination stateconst state = grid.getPaginationState();console.log(state);// {// currentPage: 3,// pageSize: 50,// totalRows: 1247,// totalPages: 25,// startRow: 100, // 0-based index// endRow: 149 // 0-based index// }Page Position
Control where pagination controls appear:
// Show at bottom (default)pagination: { enabled: true, position: 'bottom'}
// Show at toppagination: { enabled: true, position: 'top'}
// Show at both top and bottompagination: { enabled: true, position: 'both'}Use position: 'both' for grids with many rows to avoid forcing users to scroll to change pages.
Page Size Options
Provide multiple page size options:
const grid = new ZenGrid(container, { pagination: { enabled: true, pageSize: 50, // Initial page size pageSizes: [25, 50, 100, 200, 500], template: 'full' // Required for page size selector }});Events
Listen to pagination changes:
grid.on('page:change', (event) => { console.log('Page changed:', { previousPage: event.previousPage, currentPage: event.currentPage, pageSize: event.pageSize });});
// Using onPageChange callbackconst grid = new ZenGrid(container, { pagination: { enabled: true, onPageChange: (state) => { console.log('New page:', state.currentPage); console.log('Page range:', state.startRow, '-', state.endRow); } }});Example: Server-Side Pagination
For large datasets, combine pagination with server-side data loading:
const grid = new ZenGrid(container, { pagination: { enabled: true, pageSize: 50, template: 'full', onPageChange: async (state) => { // Fetch data for the current page const response = await fetch( `/api/data?page=${state.currentPage}&size=${state.pageSize}` );
const result = await response.json();
// Update grid with new page data grid.setData(result.rows);
// Update total row count if needed if (result.totalRows !== grid.getTotalRows()) { grid.setTotalRows(result.totalRows); } } }});
// Initial loadgrid.goToPage(1);When implementing server-side pagination, ensure you call setTotalRows() with the total number of rows in the dataset so the grid can calculate the correct number of pages.
Example: Combined with Filtering
Pagination works seamlessly with filtering:
const grid = new ZenGrid(container, { pagination: { enabled: true, pageSize: 50 }});
// Apply a filtergrid.setFilter('status', 'equals', 'active');
// Pagination automatically adjusts to filtered resultsconsole.log(grid.getTotalPages()); // Pages for filtered data
// Clear filtergrid.clearFilters();
// Pagination returns to showing all pagesconsole.log(grid.getTotalPages()); // Pages for all dataKeyboard Navigation
Pagination supports keyboard shortcuts:
- Ctrl + Home: Go to first page
- Ctrl + End: Go to last page
- Page Up: Go to previous page
- Page Down: Go to next page
// These keyboard shortcuts work automatically// when pagination is enabledKeyboard shortcuts are only active when the grid has focus. Click on the grid to enable keyboard navigation.
Custom Styling
Pagination controls use standard CSS classes for easy customization:
.zengrid-pagination { padding: 12px; border-top: 1px solid #ddd;}
.zengrid-pagination-button { padding: 6px 12px; margin: 0 2px;}
.zengrid-pagination-button.active { background: #007bff; color: white;}
.zengrid-pagination-info { margin: 0 16px; font-size: 14px;}