HTML:
<div id="tables"></div>
JavaScript:
var tables = [
// Table #1
[
[111, 112, 113], // Row #1
[121, 122, 123] // Row #2
],
// Table #2
[
[211, 212, 213, 214], // Row #1
[221, 222, 223, 224], // Row #2
[231, 232, 233, 234] // Row #3
],
// Table #3 (empty!)
[]
],
nr, nc, i, j, k,
name, value, textBox, cell, row, table,
$tables = $("#tables");
for (i = 0; i < tables.length; ++i) {
nr = tables[i].length;
nc = nr > 0 ? tables[i][0].length : 0;
table = "<table>";
for (j = 0; j < nr; ++j) {
row = "<tr>";
for (k = 0; k < nc; ++k) {
name = "textBox" + (i + 1) + (j + 1) + (k + 1);
value = tables[i][j][k];
textBox = "<input type='text' name='" + name + "' value='" + value + "'>";
cell = "<td>" + textBox + "</td>";
row += cell;
}
row += "</tr>\n";
table += row;
}
table += "</table>\n";
$tables.append(table);
}
See the jsfiddle. The 3rd, empty table will be inserted as an empty table
tag, but this is not necessary.
I would like to mention that since you need to connect these tables to a database, an elegant solution would be using a client-side MVC framework, like Backbone.js. Here you find a good intro book. The View could use a code similar to the one above to render
the tables. The tables
array could be stored in a Collection.