The Pagination connector provides the logic to build a custom widget that lets users choose the current page of the results.
When using pagination with Algolia, you should be aware that the engine won’t provide pages beyond the 1000th hit by default. You can find more information in the Algolia documentation.
Usage
import { connectPagination } from 'instantsearch.js/es/connectors';
const customPagination = connectPagination(
(renderOptions, isFirstRender) => {
const { pages, currentRefinement, nbPages, isFirstPage, isLastPage, refine, widgetParams } = renderOptions;
const { container } = widgetParams;
container.innerHTML = `
<div>
<button ${isFirstPage ? 'disabled' : ''} data-page="${currentRefinement - 1}">
Previous
</button>
${pages.map(page => `
<button
data-page="${page}"
style="${page === currentRefinement ? 'font-weight: bold' : ''}"
>
${page + 1}
</button>
`).join('')}
<button ${isLastPage ? 'disabled' : ''} data-page="${currentRefinement + 1}">
Next
</button>
</div>
`;
container.querySelectorAll('button').forEach(button => {
button.addEventListener('click', (e) => {
const page = parseInt(e.target.dataset.page);
if (!isNaN(page)) {
refine(page);
}
});
});
}
);
search.addWidgets([
customPagination({
container: document.querySelector('#pagination'),
}),
]);
Connector Options
The total number of pages to browse.
The padding of pages to show around the current page.
Render Options
The number of the page currently displayed.
The number of pages for the result set.
The number of hits computed for the last query (can be approximated).
The actual pages relevant to the current situation and padding.
true if the current page is also the first page.
true if the current page is also the last page.
true if this search returned more than one page.
Sets the current page and triggers a search.
Creates URLs for the next state. The number is the page to generate the URL for.
The options passed to the connector.