source: src/main/resources/static/js/pagination.js@ 0c37625

Last change on this file since 0c37625 was 276a8b6, checked in by KostaFortumanov <kfortumanov@…>, 3 years ago

post pagination

  • Property mode set to 100644
File size: 3.4 KB
Line 
1let url = window.location.href.toString().split(window.location.host)[1].split("?")[0];
2
3let pages = parseInt(document.getElementById("helper").getAttribute("data-pages"));
4let currentPage = parseInt(findGetParameter("page"));
5let sort = findGetParameter("sort");
6let order = findGetParameter("order");
7
8let sortByInput = document.getElementById("sortBy");
9sortByInput.value = sort;
10
11let orderInput = document.getElementById("order");
12orderInput.value = order;
13
14sortByInput.addEventListener("change", function () {
15 window.location.href = url + "?page=" + currentPage + "&sort=" + sortByInput.value + "&order=" + orderInput.value;
16})
17
18orderInput.addEventListener("change", function() {
19 window.location.href = url + "?page=" + currentPage + "&sort=" + sortByInput.value + "&order=" + orderInput.value;
20})
21
22document.getElementById('pagination').innerHTML = createPagination(pages, currentPage);
23
24function createPagination(pages, page) {
25 let str = '<ul class="pagination justify-content-center">';
26 let active;
27 let pagesBefore = page - 1;
28 let pagesAfter = page + 1;
29
30 if (page > 1) {
31 str += '<li class="page-item"><a class="page-link" href="'+url+'?page='+(page-1)+'&sort='+sort+'&order='+order+'">Previous</a></li>';
32 }
33
34 if (pages < 6) {
35 for (let p = 1; p <= pages; p++) {
36 active = page == p ? "active" : "";
37 str += '<li class="page-item '+active+'"><a class="page-link" href="'+url+'?page='+p+'&sort='+sort+'&order='+order+'">'+ p +'</a></li>';
38 }
39 }
40
41 else {
42 if (page > 2) {
43 str += '<li class="page-item"><a class="page-link" href="'+url+'?page=1&sort='+sort+'&order='+order+'">1</a></li>';
44 if (page > 3) {
45 str += '<li class="page-item"><a class="page-link" href="'+url+'?page='+(page-2)+'&sort='+sort+'&order='+order+'">...</a></li>';
46 }
47 }
48
49 if (page === 1) {
50 pagesAfter += 2;
51 } else if (page === 2) {
52 pagesAfter += 1;
53 }
54
55 if (page === pages) {
56 pagesBefore -= 2;
57 } else if (page === pages-1) {
58 pagesBefore -= 1;
59 }
60
61 for (let p = pagesBefore; p <= pagesAfter; p++) {
62 if (p === 0) {
63 p += 1;
64 }
65 if (p > pages) {
66 continue
67 }
68 active = page == p ? "active" : "";
69 str += '<li class="page-item '+active+'"><a class="page-link" href="'+url+'?page='+p+'&sort='+sort+'&order='+order+'">'+ p +'</a></li>';
70 }
71
72 if (page < pages-1) {
73 if (page < pages-2) {
74 str += '<li class="page-item"><a class="page-link" href="'+url+'?page='+(page+2)+'&sort='+sort+'&order='+order+'">...</a></li>';
75 }
76 str += '<li class="page-item"><a class="page-link" href="'+url+'?page='+pages+'&sort='+sort+'&order='+order+'">'+pages+'</a></li>';
77 }
78 }
79
80 if (page < pages) {
81 str += '<li class="page-item"><a class="page-link" href="'+url+'?page='+(page+1)+'&sort='+sort+'&order='+order+'">Next</a></li>';
82 }
83 str += '</ul>';
84
85 return str;
86}
87
88function findGetParameter(parameterName) {
89 let result = null,
90 tmp = [];
91 location.search
92 .substr(1)
93 .split("&")
94 .forEach(function (item) {
95 tmp = item.split("=");
96 if (tmp[0] === parameterName) result = tmp[1];
97 });
98 return result;
99}
Note: See TracBrowser for help on using the repository browser.