diff --git a/py_app/app/static/script.js b/py_app/app/static/script.js index acbe491..81b787a 100644 --- a/py_app/app/static/script.js +++ b/py_app/app/static/script.js @@ -66,6 +66,8 @@ document.addEventListener('DOMContentLoaded', () => { // Function to populate the table with data function populateTable(data) { + if (!reportTable) return; // Guard clause if reportTable doesn't exist + const tableHead = reportTable.querySelector('thead tr'); const tableBody = reportTable.querySelector('tbody'); @@ -137,6 +139,8 @@ document.addEventListener('DOMContentLoaded', () => { // Function to export table data as CSV function exportTableToCSV(filename) { + if (!reportTable) return; // Guard clause if reportTable doesn't exist + let csv = []; const rows = reportTable.querySelectorAll('tr'); @@ -165,16 +169,17 @@ document.addEventListener('DOMContentLoaded', () => { document.body.removeChild(downloadLink); } - // Handle report button clicks - reportButtons.forEach((button) => { - button.addEventListener('click', () => { - // Skip buttons that have their own handlers - if (button.id === 'select-day-report' || button.id === 'date-range-report' || button.id === 'select-day-defects-report' || button.id === 'date-range-defects-report') { - return; - } - - const reportNumber = button.dataset.report; - const reportLabel = button.textContent.trim(); + // Handle report button clicks (only if report elements exist) + if (reportButtons && reportButtons.length > 0) { + reportButtons.forEach((button) => { + button.addEventListener('click', () => { + // Skip buttons that have their own handlers + if (button.id === 'select-day-report' || button.id === 'date-range-report' || button.id === 'select-day-defects-report' || button.id === 'date-range-defects-report') { + return; + } + + const reportNumber = button.dataset.report; + const reportLabel = button.textContent.trim(); // Check if reportNumber exists if (!reportNumber) { @@ -209,13 +214,14 @@ document.addEventListener('DOMContentLoaded', () => { }) .catch((error) => { console.error('Error fetching report data:', error); - reportTitle.textContent = 'Error loading data.'; + if (reportTitle) reportTitle.textContent = 'Error loading data.'; }); }); - }); + }); + } // Bind the export functionality to the CSV button - if (exportCsvButton) { + if (exportCsvButton && reportTable && reportTitle) { exportCsvButton.addEventListener('click', () => { const rows = reportTable.querySelectorAll('tr'); if (rows.length === 0) {