Skip to content

Instantly share code, notes, and snippets.

@fatihbahceci
Created October 2, 2018 10:46
Show Gist options
  • Save fatihbahceci/adeadaf1057752377767679e03b2d970 to your computer and use it in GitHub Desktop.
Save fatihbahceci/adeadaf1057752377767679e03b2d970 to your computer and use it in GitHub Desktop.
Javacript Bootstrap Pagination
function GeneratePagination(total, perPage, current, visible, prefix) {
var r = "";
total = parseInt(total || 0);
perPage = parseInt(perPage || 0);
current = parseInt(current || 1);
visible = parseInt(visible || 0);
prefix = prefix || '';
if (total <= 0) {
return r;
} else {
var pCount = 1;
if (perPage > 0) pCount = Math.ceil(total / perPage);
if (pCount > 1) {
var start = 1;
var end = pCount;
if (visible > 0 && pCount > visible) {
start = Math.max(1, current - Math.round(visible / 2));
end = Math.min(pCount, start + visible - 1);
}
r = '<nav><ul class="pagination">';
if (current > 1) {
var hhh = (prefix + (current - 1));
debugger;
r +=
'<li class="page-item">' +
' <a class="page-link" href="' + hhh + '" aria-label="Önceki">' +
' <span aria-hidden="true">&laquo;</span>' +
' <span class="sr-only">Önceki</span>' +
' </a>' +
'</li>';
}
for (var i = start; i <= end; i++) {
var link = prefix + i;
r +=
'<li class="page-item ' + (i == current ? 'active' : '') + '"><a class="page-link" href="' + link + '">' + i + '</a></li>';
}
if (current < pCount) {
var hhh = (prefix + (current + 1));
debugger;
r +=
'<li class="page-item">' +
' <a class="page-link" href="' + hhh + '" aria-label="Sonraki">' +
' <span aria-hidden="true">&raquo;</span>' +
' <span class="sr-only">Sonraki</span>' +
' </a>' +
'</li>';
}
r += '</ul></nav>';
}
return r;
/*
<nav aria-label="Page navigation example">
<ul class="pagination">
<li class="page-item"><a class="page-link" href="#">Previous</a></li>
<li class="page-item"><a class="page-link" href="#">1</a></li>
<li class="page-item"><a class="page-link" href="#">2</a></li>
<li class="page-item"><a class="page-link" href="#">3</a></li>
<li class="page-item"><a class="page-link" href="#">Next</a></li>
</ul>
</nav>
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment