make table function for new table
This commit is contained in:
parent
365d3b4921
commit
56834b8498
|
@ -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;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue