-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap-advanced-pagination.js
75 lines (71 loc) · 2.96 KB
/
bootstrap-advanced-pagination.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* Advanced Bootstrap Pagination with Bootstrap
*
* @author Lorenzo Vainigli
* @version 1.0
* @license MIT
*/
class BootstrapAdvancedPagination {
setBootstrapVersion(version) {
this.version = version
}
/**
* Renders the pagination widget
*
* @param selector the css selector of the element in which the widget must be displayed
* @param count the total number of pages in the widget
* @param page the index of the current page
* @param range how many page indexes to show next to the index of the current page
*/
printWidget(selector, count, page, range) {
switch (this.version) {
case "4.0.0":
this.advanced_pagination_bootstrap_4_0_0(selector, count, page, range)
break
default:
console.error("Bootstrap Advanced Pagination: unsupported Bootstrap version")
break
}
}
/**
* Renders the pagination widget for Bootstrap 4.0.0.
* Don't call this method, use BootstrapAdvancedPagination.printWidget() instead.
*
* @param selector the css selector of the element in which the widget must be displayed
* @param count the total number of pages in the widget
* @param page the index of the current page
* @param range how many page indexes to show next to the index of the current page
*/
advanced_pagination_bootstrap_4_0_0(selector, count, page, range) {
let html = '<nav><ul class="pagination">';
if (page > 1) {
html += '<li class="page-item"><a class="page-link" href="./' + (page - 1) + '">Previous</a></li>';
}
for (let i = 1; i <= count; i++) {
if (i == 1 && page - range > 1) {
html += '<li class="page-item"><a class="page-link" href="./1">1</a></li>';
if (page - range > 2) {
html += '<li class="page-item disabled"><a class="page-link" href="#">...</a></li>';
}
}
if (i >= page - range && i <= page + range) {
if (i != page) {
html += '<li class="page-item"><a class="page-link" href="./' + i + '">' + i + '</a></li>';
} else {
html += '<li class="page-item active"><a class="page-link" href=""./' + i + '"">' + i + '</a></li>';
}
}
if (i == count && page + range < count) {
if (page + range < count - 1) {
html += '<li class="page-item disabled"><a class="page-link" href="#">...</a></li>';
}
html += '<li class="page-item"><a class="page-link" href="./' + count + '">' + count + '</a></li>';
}
}
if (page < count) {
html += '<li class="page-item"><a class="page-link" href="./' + (page + 1) + '">Next</a></li>';
}
html += '</ul></nav>';
$(selector).html(html);
}
}