78
rated 0 times
[
81]
[
3]
/ answers: 1 / hits: 23461
/ 8 Years ago, thu, july 7, 2016, 12:00:00
I used a form and some JavaScript to take data from an uploaded CSV file and created an HTML table based on that data. In addition, I added an extra column for user input. My issues is that I cannot figure out a manageable way to take the user input at each input tag and replace the input tag with the text they entered.
Code
This is the code behind the table. When I implement it, there will not be a finite number of rows though. Instead the amount of rows will be based on the data from the CSV file.
var table = document.createElement('table');
table.classList.add('table');
var thead = document.createElement('thead');
var headRow = document.createElement('tr');
var columnNames = [Col1, Col2, Col3, Col4];
for (var i = 0; i < 4; i++) {
var th = document.createElement('th');
th.appendChild(document.createTextNode(columnNames[i]));
headRow.appendChild(th);
}
thead.appendChild(headRow);
var tbody = document.createElement('tbody');
for (var i = 0; i < 10; i++) {
var tr = document.createElement('tr');
for (var j = 0; j < 4; j++) {
var td = document.createElement('td');
if (j == 3) {
td.classList.add(input);
var input = document.createElement('input');
input.type = text;
td.appendChild(input);
tr.appendChild(td);
continue;
}
td.appendChild(document.createTextNode(x));
tr.appendChild(td);
}
tbody.appendChild(tr);
}
table.appendChild(thead);
table.appendChild(tbody);
document.body.appendChild(table);
td { width: 25vw; }
<link href=https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css rel=stylesheet/>
Table Example
More From » html