Fix: Add comprehensive null checks to prevent script.js errors
- Add guard clauses for reportTable, reportButtons, exportCsvButton, exportPdfButton - Prevents JavaScript errors when report elements don't exist on page - Ensures theme toggle and other functionality work on all pages - Wraps report-specific code in null checks to avoid breaking other features
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user