From 56834b8498b2eb00ef01192af515b566fe307d0d Mon Sep 17 00:00:00 2001 From: Ahmed Date: Thu, 12 Sep 2024 15:06:18 +0300 Subject: [PATCH] make table function for new table --- static/js/maketable.js | 79 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 static/js/maketable.js diff --git a/static/js/maketable.js b/static/js/maketable.js new file mode 100644 index 0000000..921dc97 --- /dev/null +++ b/static/js/maketable.js @@ -0,0 +1,79 @@ +// Function to create HTML table with dynamic values +function createTable(values) { + // Create table element + var table = document.createElement('table'); + table.setAttribute('id', 'excelTable'); + table.setAttribute('border', '1'); + + // Create table body + var tbody = document.createElement('tbody'); + + // Create one row for the table + var row = document.createElement('tr'); + + // Create cells for each dynamic value + for (var i = 0; i < values.length; i++) { + var cell = document.createElement('td'); + cell.appendChild(document.createTextNode(values[i])); + row.appendChild(cell); + } + + // Append the row to the table body + tbody.appendChild(row); + + // Append table body to table + table.appendChild(tbody); + + return table.outerHTML; // Return the HTML string representation of the table +} + +function copyTableToClipboard() { + // Select the table element + var table = document.querySelector('#excelTable'); + + // Create a range for the table + var range = document.createRange(); + range.selectNode(table); + + // Select the table range + window.getSelection().removeAllRanges(); + window.getSelection().addRange(range); + + // Copy to clipboard + document.execCommand('copy'); + window.getSelection().removeAllRanges(); + + // Alert user + alert('Table copied to clipboard!'); +} + + +function prepareTable(containerId, array, color = 'white', repeated = false) { + var containerDiv = document.getElementById(containerId); + if (!repeated) { + containerDiv.innerHTML = createTable(array); + console.log('Table is not repeated'); + } else { + // Remove any existing table before appending a new one if needed + containerDiv.innerHTML += createTable(array); + console.log('Table is appended'); + } + + if (color !== 'white') { + // Get all rows in the table + var rows = document.querySelectorAll(`#${containerId} tr`); + + // Loop through each row + rows.forEach(function (row) { + // Get all cells in the row + var cells = row.querySelectorAll('td'); + + // Change the background color of the second cell (if it exists) + if (cells.length > 1) { + cells[1].style.backgroundColor = color; + } + }); + } +} + + \ No newline at end of file