moneymagnet-tracker / script.js
devlucem's picture
"FinanceFlow," is a personal finance tracker with several key features we've progressively added. I will consolidate all the functionality into a final set of files for you.
e54110d verified
Raw History Blame Contribute Delete
2.77 kB
document.addEventListener('DOMContentLoaded', function() {
// Initialize tooltips
const tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl);
});
// Sort table functionality
const tableHeaders = document.querySelectorAll('th[data-sort]');
tableHeaders.forEach(header => {
header.addEventListener('click', () => {
const table = header.closest('table');
const columnIndex = header.cellIndex;
const rows = Array.from(table.querySelectorAll('tbody tr'));
const isAscending = header.getAttribute('data-sort-direction') === 'asc';
rows.sort((a, b) => {
const aValue = a.cells[columnIndex].textContent.trim();
const bValue = b.cells[columnIndex].textContent.trim();
// Handle numeric values
if (!isNaN(aValue) && !isNaN(bValue)) {
return isAscending ? aValue - bValue : bValue - aValue;
}
// Handle date values
if (Date.parse(aValue) && Date.parse(bValue)) {
return isAscending ?
new Date(aValue) - new Date(bValue) :
new Date(bValue) - new Date(aValue);
}
// Default to string comparison
return isAscending ?
aValue.localeCompare(bValue) :
bValue.localeCompare(aValue);
});
// Update sort direction
table.querySelectorAll('th').forEach(th => {
th.removeAttribute('data-sort-direction');
th.classList.remove('text-primary-500');
});
header.setAttribute('data-sort-direction', isAscending ? 'desc' : 'asc');
header.classList.add('text-primary-500');
// Rebuild table
const tbody = table.querySelector('tbody');
tbody.innerHTML = '';
rows.forEach(row => tbody.appendChild(row));
});
});
// Filter functionality
const searchInput = document.querySelector('input[placeholder="Search transactions..."]');
searchInput.addEventListener('input', function() {
const searchTerm = this.value.toLowerCase();
const rows = document.querySelectorAll('tbody tr');
rows.forEach(row => {
const text = row.textContent.toLowerCase();
row.style.display = text.includes(searchTerm) ? '' : 'none';
});
});
});