feat: implementa paginação e contagem correta na lista de militantes

This commit is contained in:
andersonid
2025-04-04 13:31:52 -03:00
parent 75ba696355
commit a302a259a6
2 changed files with 154 additions and 21 deletions

View File

@@ -1,15 +1,115 @@
console.log('Carregando script militantes.js...');
// Variáveis globais para controle dos filtros
// Variáveis globais para controle dos filtros e paginação
let filtroAtual = 'todos';
let filtroResponsabilidade = null;
let filtroCelula = null;
let currentPage = 1;
let rowsPerPage = 20;
let totalRows = 0;
// Função para calcular o total de páginas
function calculateTotalPages() {
const allRows = document.querySelectorAll('#militantesTable tbody tr');
const visibleRows = Array.from(allRows).filter(row =>
!row.hasAttribute('data-filtered-out')
);
totalRows = visibleRows.length;
return Math.ceil(totalRows / rowsPerPage);
}
// Função para atualizar o texto de contagem
function updateCountText() {
const allRows = document.querySelectorAll('#militantesTable tbody tr');
const visibleRows = Array.from(allRows).filter(row =>
!row.hasAttribute('data-filtered-out')
);
totalRows = visibleRows.length;
const startIndex = (currentPage - 1) * rowsPerPage + 1;
const endIndex = Math.min(currentPage * rowsPerPage, totalRows);
// Atualizar texto de contagem
document.getElementById('countMilitantes').textContent =
`${startIndex}-${endIndex} de ${totalRows}`;
}
// Função para atualizar a paginação
function updatePagination() {
const totalPages = calculateTotalPages();
const paginationUl = document.querySelector('.pagination');
const prevPage = document.getElementById('prevPage');
const nextPage = document.getElementById('nextPage');
// Limpar páginas existentes (exceto prev e next)
const pageItems = paginationUl.querySelectorAll('li:not(#prevPage):not(#nextPage)');
pageItems.forEach(item => item.remove());
// Adicionar novas páginas
for (let i = 1; i <= totalPages; i++) {
const li = document.createElement('li');
li.className = `page-item${i === currentPage ? ' active' : ''}`;
li.innerHTML = `<a class="page-link" href="#">${i}</a>`;
li.addEventListener('click', (e) => {
e.preventDefault();
currentPage = i;
updateVisibleRows();
updatePagination();
});
paginationUl.insertBefore(li, nextPage);
}
// Atualizar estado dos botões prev/next
prevPage.classList.toggle('disabled', currentPage === 1);
nextPage.classList.toggle('disabled', currentPage === totalPages || totalPages === 0);
// Adicionar eventos aos botões prev/next
prevPage.onclick = (e) => {
e.preventDefault();
if (currentPage > 1) {
currentPage--;
updateVisibleRows();
updatePagination();
}
};
nextPage.onclick = (e) => {
e.preventDefault();
if (currentPage < totalPages) {
currentPage++;
updateVisibleRows();
updatePagination();
}
};
// Atualizar texto de contagem
updateCountText();
}
// Função para atualizar as linhas visíveis
function updateVisibleRows() {
const allRows = document.querySelectorAll('#militantesTable tbody tr');
const visibleRows = Array.from(allRows).filter(row =>
!row.hasAttribute('data-filtered-out')
);
const startIndex = (currentPage - 1) * rowsPerPage;
const endIndex = startIndex + rowsPerPage;
visibleRows.forEach((row, index) => {
if (index >= startIndex && index < endIndex) {
row.style.display = '';
} else {
row.style.display = 'none';
}
});
updateCountText();
}
// Função para filtrar militantes
function filtrarMilitantes() {
const searchTerm = document.getElementById('searchInput').value.toLowerCase();
const rows = document.querySelectorAll('#militantesTable tbody tr');
let count = 0;
rows.forEach(row => {
let shouldShow = true;
@@ -39,19 +139,36 @@ function filtrarMilitantes() {
}
}
// Aplicar visibilidade
row.style.display = shouldShow ? '' : 'none';
if (shouldShow) count++;
// Marcar linha como filtrada ou não
if (shouldShow) {
row.removeAttribute('data-filtered-out');
} else {
row.setAttribute('data-filtered-out', '');
row.style.display = 'none';
}
});
// Atualizar contador
document.getElementById('countMilitantes').textContent = count;
// Resetar para a primeira página e atualizar paginação
currentPage = 1;
updateVisibleRows();
updatePagination();
}
// Configurar eventos quando o DOM estiver carregado
document.addEventListener('DOMContentLoaded', function() {
console.log('DOM carregado, configurando eventos...');
// Configurar seletor de linhas por página
const rowsPerPageSelect = document.getElementById('rowsPerPage');
if (rowsPerPageSelect) {
rowsPerPageSelect.addEventListener('change', function() {
rowsPerPage = parseInt(this.value);
currentPage = 1;
updateVisibleRows();
updatePagination();
});
}
// Configurar pesquisa
const searchInput = document.getElementById('searchInput');
if (searchInput) {
@@ -595,6 +712,10 @@ document.addEventListener('DOMContentLoaded', function() {
}
});
});
// Inicializar paginação
updateVisibleRows();
updatePagination();
});
// Função para mostrar alertas

View File

@@ -122,10 +122,21 @@
</table>
</div>
<div class="pagination-container">
<div class="pagination-container d-flex justify-content-between align-items-center">
<div class="text-muted">
Mostrando <span id="countMilitantes">{{ militantes|length }}</span> militantes
</div>
<div class="d-flex align-items-center gap-3">
<div class="d-flex align-items-center">
<span class="me-2">Mostrar</span>
<select class="form-select form-select-sm me-2" id="rowsPerPage" style="width: auto;">
<option value="10">10</option>
<option value="20" selected>20</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
<span>linhas</span>
</div>
<nav aria-label="Navegação de páginas">
<ul class="pagination mb-0">
<li class="page-item disabled" id="prevPage">
@@ -142,6 +153,7 @@
</div>
</div>
</div>
</div>
<!-- Modais -->
{% include 'modals/militante_novo.html' %}