feat: implementa paginação e contagem correta na lista de militantes
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user