Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/en.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,27 @@ Number of data items per page.

If you want to show all pages, just set it to `null`.

### pageLink <em>string (default ``)</em>
`pageLink` customizes the anchor href attribute in the pagination page buttons, for SEO purposes mostly.

By default, they use `<a>{{INDEX}}</a>`.

By setting a string in `pageLink`, it becomes `<a href="{{pageLink}}">{{INDEX}}</a>`.

If you use the special string `{{index}}` inside your `pageLink`, the link will be customized with the current index.

Example:

pageLink: `/mysite/products/?page={{index}}`

will render:

`<a href="/mysite/products?page=1">1</a>`

`<a href="/mysite/products?page=2">2</a>`

etc.

### callback <em>function(data, pagination)</em>
Used to customize item's innerHTML, will be invoked on each paging.

Expand Down
48 changes: 48 additions & 0 deletions examples/pagination.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
<div id="pagination-demo2"></div>
<div class="data-container"></div>
<div id="pagination-demo3"></div>
<div class="data-container"></div>
<div id="pagination-demo4"></div>
</section>
</div>

Expand Down Expand Up @@ -168,6 +170,52 @@
}
})
})('demo3');

(function(name) {
var container = $('#pagination-' + name);
if (!container.length) return;
var sources = function () {
var result = [];

for (var i = 1; i < 196; i++) {
result.push(i);
}

return result;
}();

var options = {
dataSource: sources,
pageLink: '/mysite/products/?page={{index}}', //to show the page button href attribute
pageNumber: 5, //to show how the next/prev links are calculated
callback: function (response, pagination) {
window.console && console.log(response, pagination);

var dataHtml = '<ul>';

$.each(response, function (index, item) {
dataHtml += '<li>' + item + '</li>';
});

dataHtml += '</ul>';

container.prev().html(dataHtml);
}
};

//$.pagination(container, options);

container.addHook('beforeInit', function () {
window.console && console.log('beforeInit...');
});
container.pagination(options);

container.addHook('beforePageOnClick', function () {
window.console && console.log('beforePageOnClick...');
//return false
});
})('demo4');

})
</script>
</body>
Expand Down
13 changes: 12 additions & 1 deletion src/pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,18 @@

getPageLinkTag: function(index) {
var pageLink = attributes.pageLink;
return pageLink ? `<a href="${pageLink}">${index}</a>` : `<a>${index}</a>`;
let pageIndex = index;
let total = attributes.totalNumber;
let currentPage = attributes.pageNumber;
switch (index){
case attributes.prevText:
pageIndex = (currentPage>1)?currentPage-1:1;
break;
case attributes.nextText:
pageIndex= (currentPage<total)?currentPage+1:total;
break;
}
return pageLink ? `<a href="${pageLink.replace('{{index}}', pageIndex)}">${index}</a>` : `<a>${index}</a>`;
},

// Generate HTML for page numbers
Expand Down