Inserting individual values from an array into a table cell
I have created a table using a for loop in my code and have various variables in multiple arrays. I want to be able to put the values from the array individually into each table cell going down in a column. For code I have this:
function tableCreate() {
for (rownum = 1; rownum <= 7; rownum++) {
document.write("<tr>");
for (colnum = 1; colnum <= 7; colnum++) {
if (rownum == 1 + colnum == 1) {
document.write("<td>" + id[0] + "</td>");
}
}
document.write("</tr>");
}
}
var id = new Array("1022", "1112", "1230", "554", "1355", "1600");
var title = new Array("Prof.", "Prof.", "Prof.", "Prof.", "Asst. Prof.", "Asst. Prof.");
var name = new Array("Catherine Adler", "Michael Li", "Maria Sanchez", "Robert Hope", "Wayne Li", "Kate Howard");
var position = new Array("Department Chair", "Asst. Chair", "TA Supervisor");
var er = new Array(2, 3, 1, 2, 1, 3);
var yoe = new Arr开发者_运维问答ay(18, 12, 10, 23, 8, 5);
var cs = new Array(85000, 70000, 62000, 55000, 50000, 45000);
The above code is all part of an external JavaScript document. I tryed using if statements to produce what I wanted but that failed. Now I'm lost and some help would be appreciated. To reitterate, I want to get say this array: var id = new Array("1022", "1112", "1230", "554", "1355", "1600");
into a table column and have each variable in that array take a cell in that column.
Not so clear but I think what you want is this:
function tableCreate() {
for (rownum = 1; rownum <= 7; rownum++) {
document.write("<tr>");
for (colnum = 1; colnum <= 7; colnum++) {
if (colnum == 1) {
document.write("<td>" + id[(colnum-1)] + "</td>");
}
}
document.write("</tr>");
}
}
I expect this is really what you want:
// constants
var colCount=7;
var rowCount=7;
// input data
var id = new Array("1022", "1112", "1230", "554", "1355", "1600");
var title = new Array("Prof.", "Prof.", "Prof.", "Prof.", "Asst. Prof.", "Asst. Prof.");
var name = new Array("Catherine Adler", "Michael Li", "Maria Sanchez", "Robert Hope", "Wayne Li", "Kate Howard");
var position = new Array("Department Chair", "Asst. Chair", "TA Supervisor");
var er = new Array(2, 3, 1, 2, 1, 3);
var yoe = new Array(18, 12, 10, 23, 8, 5);
var cs = new Array(85000, 70000, 62000, 55000, 50000, 45000);
// make a special column.
var complicatedName;
for(index = 0; index < rowCount;index++)
{
complicatedName[index] = title[index]+' '+name[index]+' - '+position[index];
}
colCount = colCount - 2
// create the column array.
var collist = [id,complicatedName,er,yoe,cs];
// make the table.
function tableCreate() {
for (rownum = 1; rownum <= rowCount; rownum++) {
document.write("<tr>");
for (colnum = 1; colnum <= colCount; colnum++) {
document.write("<td>" + (collist[(colnum-1)])[(rownum-1)] + "</td>");
}
document.write("</tr>");
}
}
There is a syntax error in your code.
Change this:
if (rownum == 1 + colnum == 1) {
...to this:
if (rownum == 1 && colnum == 1) {
精彩评论