The Pagination component provides flexible pagination capabilities with customizable controls, dynamic page sizes, and scroll-to-top functionality.
- Installation
- Basic Usage
- Configuration
- UI Components
- API Reference
- Events
- Examples
- TypeScript
- Best Practices
import { Pagination } from 'advanced-filter-system';
// As part of AFS
const afs = createAFS({
pagination: {
enabled: true,
itemsPerPage: 10
}
});
// Access pagination
const pagination = afs.pagination;
<!-- Items Container -->
<div id="items-container">
<div class="filter-item">Item 1</div>
<div class="filter-item">Item 2</div>
<!-- More items... -->
</div>
<!-- Pagination Container -->
<div class="pagination-container"></div>
// Initialize with pagination configuration
const afs = createAFS({
containerSelector: '#items-container',
itemSelector: '.filter-item',
pagination: {
enabled: true,
itemsPerPage: 10,
maxButtons: 7,
container: '.pagination-container',
showPrevNext: true,
scrollToTop: true,
scrollOffset: 50
}
});
{
enabled: boolean; // Enable pagination
itemsPerPage: number; // Items per page
maxButtons: number; // Maximum page buttons shown
showPrevNext: boolean; // Show previous/next buttons
showFirstLast: boolean; // Show first/last page buttons
scrollToTop: boolean; // Scroll to top on page change
scrollOffset: number; // Offset for scroll to top
containerClass: string; // Pagination container class
pageButtonClass: string; // Page button class
activePageClass: string; // Active page button class
template: {
prev: string; // Previous button text/HTML
next: string; // Next button text/HTML
first: string; // First page button text/HTML
last: string; // Last page button text/HTML
ellipsis: string; // Ellipsis text/HTML
}
}
<div class="afs-pagination">
<button class="afs-page-button afs-pagination-prev">‹</button>
<button class="afs-page-button" data-page="1">1</button>
<span class="afs-pagination-ellipsis">...</span>
<button class="afs-page-button" data-page="10">10</button>
<button class="afs-page-button afs-pagination-next">›</button>
</div>
.afs-pagination {
display: flex;
justify-content: center;
gap: 8px;
margin-top: 20px;
}
.afs-page-button {
padding: 8px 12px;
border: 1px solid #e5e7eb;
border-radius: 4px;
cursor: pointer;
}
.afs-page-button.active {
background-color: #3b82f6;
color: white;
}
.afs-page-button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
Navigate to specific page.
afs.pagination.goToPage(2);
Set number of items per page.
afs.pagination.setItemsPerPage(20);
Get current pagination information.
const info = afs.pagination.getPageInfo();
Update pagination state and UI.
afs.pagination.update();
Refresh pagination with current state.
afs.pagination.refresh();
interface PageInfo {
currentPage: number;
itemsPerPage: number;
totalPages: number;
totalItems: number;
startIndex: number;
endIndex: number;
hasNextPage: boolean;
hasPrevPage: boolean;
}
// Page changed
afs.on('pagination', (data) => {
console.log('Current page:', data.currentPage);
console.log('Total pages:', data.totalPages);
});
// Items per page changed
afs.on('itemsPerPageChanged', (data) => {
console.log('Items per page:', data.itemsPerPage);
});
// Pagination updated
afs.on('paginationUpdated', (data) => {
console.log('Pagination state:', data);
});
// Simple pagination setup
const afs = createAFS({
pagination: {
enabled: true,
itemsPerPage: 10
}
});
// Custom pagination template
const afs = createAFS({
pagination: {
template: {
prev: '← Previous',
next: 'Next →',
first: '|←',
last: '→|',
ellipsis: '...'
}
}
});
// Add page size selector
const pageSizeSelector = document.createElement('select');
[10, 20, 50, 100].forEach(size => {
const option = document.createElement('option');
option.value = size;
option.textContent = `${size} per page`;
pageSizeSelector.appendChild(option);
});
pageSizeSelector.addEventListener('change', (e) => {
afs.pagination.setItemsPerPage(Number(e.target.value));
});
// Implement infinite scroll
window.addEventListener('scroll', () => {
const { scrollTop, scrollHeight, clientHeight } = document.documentElement;
if (scrollTop + clientHeight >= scrollHeight - 100) {
const info = afs.pagination.getPageInfo();
if (info.hasNextPage) {
afs.pagination.goToPage(info.currentPage + 1);
}
}
});
interface PaginationOptions {
enabled: boolean;
itemsPerPage: number;
maxButtons?: number;
showPrevNext?: boolean;
showFirstLast?: boolean;
scrollToTop?: boolean;
scrollOffset?: number;
containerClass?: string;
pageButtonClass?: string;
activePageClass?: string;
template?: PaginationTemplate;
}
interface PaginationTemplate {
prev?: string;
next?: string;
first?: string;
last?: string;
ellipsis?: string;
}
interface PageInfo {
currentPage: number;
itemsPerPage: number;
totalPages: number;
totalItems: number;
startIndex: number;
endIndex: number;
hasNextPage: boolean;
hasPrevPage: boolean;
}
interface PaginationEvent {
currentPage: number;
totalPages: number;
itemsPerPage: number;
}
-
Performance Optimization
// Cache page calculations const pageCache = new Map(); function getPageItems(page) { const cacheKey = `page_${page}`; if (!pageCache.has(cacheKey)) { pageCache.set(cacheKey, calculatePageItems(page)); } return pageCache.get(cacheKey); }
-
Memory Management
// Clear cache on filter/sort afs.on('filter', () => pageCache.clear()); afs.on('sort', () => pageCache.clear());
-
Error Handling
try { afs.pagination.goToPage(page); } catch (error) { console.error('Pagination error:', error); afs.pagination.goToPage(1); // Fallback to first page }
-
Accessibility
// Add ARIA attributes const paginationContainer = document.querySelector('.afs-pagination'); paginationContainer.setAttribute('role', 'navigation'); paginationContainer.setAttribute('aria-label', 'Pagination'); // Announce page changes afs.on('pagination', (data) => { announcePageChange(data.currentPage, data.totalPages); });
-
URL Integration
// Update URL with page number afs.on('pagination', (data) => { const url = new URL(window.location); url.searchParams.set('page', data.currentPage.toString()); window.history.pushState({}, '', url); });
-
Responsive Design
// Adjust buttons based on screen size function updatePaginationLayout() { const width = window.innerWidth; afs.pagination.updateConfig({ maxButtons: width < 768 ? 3 : 7 }); } window.addEventListener('resize', updatePaginationLayout);