[don't copy paste]
var pageNo = 1;
var numPages;
var currentPage = 0;
numPages = response.pagination.pageCount;
var paginationToShow = '';
paginationToShow += "<ul class='pagination pagination-sm'>";
if(response.pagination.pageNo > 1){
paginationToShow += "<li id='NA'></li>";
paginationToShow += "<li id='first'><a href='#'>First</a></li>";
paginationToShow += "<li id='previous'><a href='#'>Prev</a></li>";
}else{
paginationToShow += "<li id='NA'></li>";
paginationToShow += "<li id='first'></li>";
paginationToShow += "<li id='previous'></li>";
}
// paginationToShow += "<li><a href='#' rel='1'>1<span class='sr-only'>(current)</span></a></li>";
var pageCount = numPages;
var pageNumber = response.pagination.pageNo;
var paginationValues = [];
var activePosition = 0;
var i = 0;
if (pageNumber > 4) {
var leftCount = 8 - ((pageCount - pageNumber) >= 4 ? 4
: (pageCount - pageNumber));
leftCount = leftCount < pageNumber ? leftCount
: (pageNumber - 1);
while (i <= leftCount) {
paginationValues[i] = pageNumber + i - leftCount;
i++;
}
} else {
while (i < pageNumber) {
paginationValues[i] = i + 1;
i++;
}
}
activePosition = i+3;
if (pageNumber < pageCount) {
var value = pageNumber + 1;
var rightCount = 9 - paginationValues.length;
var j = 0;
while ((value <= pageCount) && (j < rightCount)) {
paginationValues[i++] = value++;
j++;
}
}
console.log(paginationValues);
console.log(activePosition);
for (var i = 0; i < paginationValues.length; i++) {
paginationToShow += "<li><a href='#' rel='" + paginationValues[i] + "'>" + paginationValues[i]
+ "</a></li>";
}
if(response.pagination.pageNo < numPages){
paginationToShow += "<li id='next'><a href='#'>Next</a></li>";
paginationToShow += "<li id='last'><a href='#'>Last</a></li>";
paginationToShow += "<li id='NA'></li>";
}else{
paginationToShow += "<li id='next'></li>";
paginationToShow += "<li id='last'></li>";
paginationToShow += "<li id='NA'></li>";
}
paginationToShow += "</ul>";
$('#page_navigation').html(paginationToShow);
$(
"#page_navigation li:nth-child("
+ (activePosition) + ")").addClass(
'active');
flag = true;
currentPage = response.pagination.pageNo;