Easily rig a custom DOM for ajax pagination requests.
⚠️ This project is very much a work in progress and should not be used in production.
1. Include the library via jsDelivr CDN
<script src="https://cdn.jsdelivr.net/gh/thelevicole/[email protected]/dist/ajax-pager.js"></script>
2. Attach to an element
var $gallery = $( '#gallery' ).ajaxPager( {
url: 'https://example.com/api/gallery',
totalPages: 10,
data: { /* ..additional data to send with request.. */ }
} );
3. Rig the DOM
<div id="gallery"></div>
<button id="load">Load more</button>
$( '#load' ).on( 'click', function() {
$gallery.loadMore();
} );
$gallery.on( 'ap.request.done', function( event, response ) {
$gallery.append( response.html );
} );
Name | Description | Example |
---|---|---|
Events | ||
'ap.request.start' |
Fired before a request is made. Useful for showing a loading icon. |
|
'ap.request.done' |
Fired right after a request has successully completed. |
|
'ap.request.catch' |
Fired if a request failed. |
|
'ap.request.finally' |
Fired after every request, successfull or not. Useful for hiding a loading icon. |
|
Methods | ||
loadMore() |
Initiate a request. |
|
hasMore() |
Returns boolean value. |
|
currentPage() |
Returns integer value of the current page number. |
|
resetPage() |
Reset the internal page counter to 0 |
|
setPage( page ) |
Override the internall page counter. |
|
updateOptions( options ) |
Update options that were set when originally initiating the library. |
|
HTML
<div id="users"></div>
<button id="loadmore" type="button">Load more</button>
JavaScript
var $button = $( '#loadmore' );
var $pager = $( '#users' ).ajaxPager( {
url: 'https://reqres.in/api/users',
totalPages: 4
} );
$button.on( 'click', function() {
$pager.loadMore();
} );
$pager.on( 'ap.request.done', function( event, response ) {
if ( response.data.length ) {
for ( var i = 0; i < response.data.length; i++ ) {
var user = response.data[ i ];
$pager.append( '<div><h3>' + user.first_name + '' + user.last_name + '</h3></div>' );
}
}
} );
Playground