Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function paginate(id, qnt_total, pg_atual) {
let qnt_paginas = Math.ceil(qnt_total / config[id].querydefault.limit);
if(pg_atual > qnt_paginas){
pg_atual = qnt_paginas
}
var lateral = 2
var btns = ''
var pagina = `
<nav aria-label="Buscar..." class="text-center">
<ul class="pagination justify-content-center">
first
buttons
last
</ul>
</nav>
`
if (qnt_paginas > 1) {
for (var page = pg_atual - lateral; page <= pg_atual + lateral; page++) {
if (page > 0 && page <= qnt_paginas && qnt_paginas > 1) {
if (page == pg_atual) {
btns += '<li class="page-item active" onclick="createTableAndFillFields('+id+',{page:' + page + '})"><a class="page-link">' + page + '</a></li>'
} else {
btns += '<li class="page-item" onclick="createTableAndFillFields('+id+',{page:' + page + '})"><a class="page-link">' + page + '</a></li>'
}
if (pg_atual != qnt_paginas) {
pagina = pagina.replace('last', '<li class="page-item" onclick="createTableAndFillFields('+id+',{page:' + qnt_paginas + '})"><a class="page-link">Ultimo</a></li>')
} else {
pagina = pagina.replace('last', '')
}
if (pg_atual != 1) {
pagina = pagina.replace('first', '<li class="page-item" onclick="createTableAndFillFields('+id+',{page:' + 1 + '})"><a class="page-link">Primeiro</a></li>')
} else {
pagina = pagina.replace('first', '')
}
}
}
}
pagina = pagina.replace('first', '')
pagina = pagina.replace('last', '')
$('#pagination' + id).html('<div id="pagination_info' + id + '"><div class="row"><div class="text-left col-md-6">Quantidade de registros: ' + qnt_total.toString() + '</div><div class="text-right col-md-6"> Página ' + (pg_atual) + ' de ' + qnt_paginas + '</div></div><ul class="pagination justify-content-center pb-2" id="paginacao' + id + '"></ul></div>')
$('#pagination_info' + id).append(pagina.replace('buttons', btns))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment