pagination
Pagination for javascript/nodejs
usage example
var pagination = ;var paginator = pagination;console;
Or customized renderer
This example show how to generate markup for twitter boostrap, see example/twitter.html for template rendering
var pagination = var boostrapPaginator = prelink:'/' current: 3 rowsPerPage: 200 totalResult: 10020 slashSeparator: true { var i len prelink; var html = '<div><ul class="pagination">'; ifresultpageCount < 2 html += '</ul></div>'; return html; prelink = this; ifresultprevious html += '<li class="page-item"><a class="page-link" href="' + prelink + resultprevious + '">' + thisoptions + '</a></li>'; ifresultrangelength for i = 0 len = resultrangelength; i < len; i++ ifresultrangei === resultcurrent html += '<li class="active page-item"><a class="page-link" href="' + prelink + resultrangei + '">' + resultrangei + '</a></li>'; else html += '<li class="page-item"><a class="page-link" href="' + prelink + resultrangei + '">' + resultrangei + '</a></li>'; ifresultnext html += '<li class="page-item"><a class="page-link" href="' + prelink + resultnext + '" class="paginator-next">' + thisoptions + '</a></li>'; html += '</ul></div>'; return html; };console;
OR
var pagination = ;var paginator = prelink:'/' current: 10 rowsPerPage: 200 totalResult: 10020;console;// output (without newlines)
Previous 8 9 10 11 12 Next
OR need data from the calculation
var pagination = ;var paginator = prelink:'/' current: 3 rowsPerPage: 200 totalResult: 10020;console; // output prelink: '/' current: 3 previous: 2 next: 4 first: 1 last: 51 range: 1 2 3 4 5 fromResult: 401 toResult: 600 totalResult: 10020 pageCount: 51
Pagination on client side
You can browse example folder for more. Release folder contains all mimified versions for browser. To customize your need you can use ./bin/build.js -h html += paginator.render();
Classes
SearchPaginator(options)
options
See Options bellow
See also http://developer.yahoo.com/ypatterns/navigation/pagination/search.html
ItemPaginator(options)
options
See Options bellow
See also http://developer.yahoo.com/ypatterns/navigation/pagination/item.html
TemplatePaginator(options)
options
See Options bellow
This class will render the markup as desired. The options must contains property "template" It can be either a template string or a compiled template. The local variables available in the template are
prelink
StringpreparedPreLink
Stringcurrent
Integerprevious
Integernext
Integerfirst
Integerlast
Integerrange
ArrayfromResult
IntegertoResult
IntegertotalResult
IntegerpageCount
Integertranslations
Object with properties NEXT, PREVIOUS, FIRST, LAST, CURRENT_PAGE_REPORT
API
Paginator.getPaginationData()
Return an object contains data for rendering markup. See example above.
Paginator.set(option, value)
Set value to a single for option. See options section bellow
Paginator.preparePreLink(prelink)
prelink
String
Append page param to the link
Paginator.render()
Return the rendered markup
pagination.create(type, options)
type
Stringoptions
Object see Options section bellow
Wrapper for create instance of classes above
pagination.TemplateEngine.parse(str, options)
See pagination.TemplateEngine.compile bellow
pagination.TemplateEngine.compile(str, options)
str
Template stringoptions
object which can contains .open .close .cache and .id
Options
Object to pass to paginator classes (second argument when using create function)
totalResult: {Integer}
Number of total items in result set
prelink: {String}
Link to append the page-param
rowsPerPage: {Integer}
Number of items per page, default to 10
pageLinks: {Integer}
Number of links to create in page range, default to 5. This value will be ignored when using item pagination.
current: {Integer}
Indicate which page is the current one. Page always starts with 1.
translationCache: {Boolean}
To indicate if the result from CURRENT_PAGE_REPORT translation can be cached or not. Default is false. The cache is global and will be the same for all instances which have specified translationCacheKey as bellow.
translationCacheKey: {String}
For supporting multiple versions of translation of CURRENT_PAGE_REPORT. It can use for multilanguages or different formats. Default is "en"
translator: {Function}
For translations of FIRST, NEXT, ... Simple example
var translations = 'PREVIOUS' : 'Voorgaand' 'NEXT' : 'Volgende' 'FIRST' : 'Eerst' 'LAST' : 'Laatste' 'CURRENT_PAGE_REPORT' : 'Resulten {FromResult} - {ToResult} van {TotalResult}'; var item = prelink : '/' pageLinks : 5 current : 5 totalResult : 100 { return translationsstr; };
pageParamName: {String}
The name of the page parameter. Default is "page"
slashSeparator: {Boolean}
Indicate if using slash instead of equal sign, ie /page/2 instead of /?page=2, default is false.
template: {String | Function}
This can be a template string or a compiled template (function). The compiled function will be called with an object as argument.
Acknowledgements
Template engine is taking mostly from https://github.com/vanng822/ejs which is a fork of https://github.com/visionmedia/ejs