Skip to content

Instantly share code, notes, and snippets.

@waitingallday
Created May 29, 2018 05:54
Show Gist options
  • Save waitingallday/e9ab057269adc78593e4be4b8e6a256c to your computer and use it in GitHub Desktop.
Save waitingallday/e9ab057269adc78593e4be4b8e6a256c to your computer and use it in GitHub Desktop.
Figure out the pagination buttons
Handlebars.registerHelper('pagebuttons', function(totalMatching, currStart, query) {
var q = query,
out = "",
pages = (parseInt(totalMatching / 10)) + 1,
currPage = currStart <= 1 ? 1 : (currStart + 9) / 10,
startPage = (currPage - 2) >= 1 ? (currPage === pages ? currPage - 2 : currPage - 1) : 1,
endPage = (currPage + 2) <= pages ? (currPage === 1 ? currPage + 2 : currPage + 1) : pages,
i;
for (i = startPage; i <= endPage; i++) {
out = out + '<a ' + 'class="search__pagination-link';
if (i === currPage) out = out + ' search__pagination-link--current';
out = out + '" href="?query=' + q + '&start-rank=' + parseInt(((i-1) * 10) + 1) + '">' + i + "</a>\n";
}
return out;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment