...

View Full Version : Problem populating table dynamically



Anarchist
11-03-2004, 09:37 PM
Hi,
I am having a problem getting the data cells to show up in my table, I have gotten some of the table working, but I am having problems with the loops that run through the array [sales].

I would like data to run horizontally alongside the table header cells which are months but have had no luck getting this to work.

The array has 72 items

Could someone help please?



<html>
<head>
<title>Javascript Table</title>
<script type="text/javascript" src="business.js">

</script>
<script type="text/javascript" src="tabledata.js">
</script>
</head>
<body>
<script>
document.write("<table border='1'>");

document.write("<caption>");
document.write(caption);
document.write("</caption>");

document.write("<tr>");
document.write("<th colspan='2' rowspan='2'>");
document.write(year);
document.write("</th>");

document.write("<th colspan='6'>");
document.write(coltop);
document.write("</th> ");

document.write("<th colspan='2' rowspan='2'>");
document.write(lastcoltitle);
document.write("</th>");
document.write("</tr>");

document.write("<tr>");
for (i=0;i<branch.length;++i) {
document.write("<th>");
document.write(branch[i]);
document.write("</th>");
};
document.write("</tr>");

document.write("<tr>");
document.write("<th rowspan='13'>");
document.write(rowtop);
document.write("</th> ");
document.write("</tr>");

document.write("<tr>");
document.write("<th>");
document.write(month[0]);
document.write("</th>");
document.write("</tr>");

document.write("<tr>");
document.write("<th>");
document.write(month[1]);
document.write("</th>");
document.write("</tr>");

document.write("<tr>");
document.write("<th>");
document.write(month[2]);
document.write("</th>");
document.write("</tr>");

document.write("<tr>");
document.write("<th>");
document.write(month[3]);
document.write("</th>");
document.write("</tr>");

document.write("<tr>");
document.write("<th>");
document.write(month[4]);
document.write("</th>");
document.write("</tr>");

document.write("<tr>");
document.write("<th>");
document.write(month[5]);
document.write("</th>");
document.write("</tr>");

document.write("<tr>");
document.write("<th>");
document.write(month[6]);
document.write("</th>");
document.write("</tr>");

document.write("<tr>");
document.write("<th>");
document.write(month[7]);
document.write("</th>");
document.write("</tr>");

document.write("<tr>");
document.write("<th>");
document.write(month[8]);
document.write("</th>");
document.write("</tr>");

document.write("<tr>");
document.write("<th>");
document.write(month[9]);
document.write("</th>");
document.write("</tr>");

document.write("<tr>");
document.write("<th>");
document.write(month[10]);
document.write("</th>");
document.write("</tr>");

document.write("<tr>");
document.write("<th>");
document.write(month[11]);
document.write("</th>");
document.write("</tr>");

document.write("<th colspan='2' rowspan='2'>");
document.write(lastrowtitle);
document.write("</th>");
document.write("</tr>");

document.write("<tr>");
for (i=64;i<sales.length;++i) {
document.write("<td>");
document.write(sales[i]);
document.write("</td>");
};
document.write("</tr>");

document.write("</table>");

</script>
</body>
</html>

Roy Sinclair
11-03-2004, 10:20 PM
It's doing what you told it to do (typical program :) ). If you want to break out the information by month then you need to change your loop to output the month (which I see you already have in another array) and add <tr> and </tr> between the entries for each month instead of just trying to output the whole table in one row.

Anarchist
11-03-2004, 10:31 PM
I can't really grasp what you are saying mate, one part of the table works, but when I try to get the same thing with the rest of the table it goes chaotic:

this part works.




document.write("<tr>");
for (i=64;i<sales.length;++i) {
document.write("<td>");
document.write(sales);
document.write("</td>");
};
document.write("</tr>");


when I try to place this portion of code elsewhere the whole gets messy. It's in the <tr> tags you mention.

Cheers Charlie

Roy Sinclair
11-04-2004, 04:41 PM
This is a class assignment isn't it?

Think about it a moment. You have three arrays, one array corresponds to the x axis of your table (the branches array with 6 entries), one array corresponds to the y axis of your table (the month array with 12 entries) and the third array corresponds to both (the sales array with 6 time 12 or 72 entries).

The table you output apparently needs to show the "branches" heading at the top and the months along the side. It would be assumed that the first six entries in the "sales" array are the sales for each of those six branches for the month of January and the next six entries in the array are the sales for those same six branches for the month of February....

For each month's data in the sales array you need to output an opening <tr> tag, then the table cell with the month's name followed by the table cells showing the sales for each of the six branches for that month followed by a total for all branches for that month and then the the closing </tr> tag.

Once all the months have been display you need to display an accumulation total for the years sales for each branch, you can either add an additional array and add the sales for each branch to the right entry in the array

Your original code simply displayed the branches heading line (which is right) but then ran down the months and displayed each month in a new row but didn't display the sales data or total for that month. Not intil you got to the end do you display the sales data and then you do it all at once where the totals should be instead.

Anarchist
11-04-2004, 10:54 PM
This is a class assignment isn't it?


Sorry for the late reply, I was at school..HeHe..wish I was young enough for it to be a class project.

Believe it or not, I have 5 children, the eldest of which is 20yrs old. You can guarantee at least one person will ask though.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum