尝试从Javascript中的数组变量创建表 [英] Trying to create a table from array variables in Javascript

查看:77
本文介绍了尝试从Javascript中的数组变量创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试获取数组值以形成一个表,但我快到了,我一直遇到一个问题.我的表有7列和6行.我需要为每个变量都具有一个数组,但是当我实际创建数组的表3时,必须将它们放在一起,然后将三个数组中的每个数组的每个值放到该列中.到目前为止,这是我完成的代码:

I have been trying to get array values to form a table and I am almost there, I just keep running into one issue. My table has 7 columns and 6 rows. I was required to have an array for each of the variables but when I actually create the table 3 of the arrays have to be put together and then each value for each of the three arrays put in the column down. This is what I have accomplished so far for code:

<br />
// constants<br />
var colCount = 7;<br />
var rowCount = 7;<br />
<br />
// input data<br />
var id = new Array("1022", "1112", "1230", "554", "1355", "1600");<br />
var title = new Array("Prof.", "Prof.", "Prof.", "Prof.", "Asst. Prof.", "Asst. Prof.");<br />
var name = new Array("Catherine Adler", "Michael Li", "Maria Sanchez", "Robert Hope", "Wayne Li", "Kate Howard");<br />
var position = new Array("Department Chair", "Asst. Chair", "TA Supervisor");<br />
var er = new Array(2, 3, 1, 2, 1, 3);<br />
var yoe = new Array(18, 12, 10, 23, 8, 5);<br />
var cs = new Array(85000, 70000, 62000, 55000, 50000, 45000);<br />
<br />
// make a special column.<br />
var complicatedName;<br />
for (index = 0; index < rowCount; index++)<br />
{<br />
   complicatedName[index] = title[index]+'' ''+name[index]+'' ''+position[index];<br />
}<br />
colCount = colCount - 2<br />
<br />
// create the column array.<br />
var collist = [id,complicatedName,er,yoe,cs];<br />
<br />
// make the table.<br />
function tableCreate() {<br />
 for (rownum = 1; rownum <= rowCount; rownum++) {<br />
     document.write("<table><tbody><tr>");<br />
     for (colnum = 1; colnum <= colCount; colnum++) {  <br />
         document.write("<td>" + (collist[(colnum-1)])[(rownum-1)] + "</td>");<br />
     }<br />
     document.write("</tr></tbody></table>");<br />
 }<br />
}<br />


我发现出于某些奇怪的原因,complexName和collist变得不确定,并且不知道为什么.索引也似乎未定义.
任何见解都会有所帮助,如果需要进一步说明,请提出疑问.


I find that the complicatedName and collist come out to undefined for some strange reason and have no ideas as to why. Index also appears to be undefined.
Any insights would be helpful and if extra clarification is needed please ask.

推荐答案

y,您的排名数组仅包含3个元素...
在第3次迭代中寻找位置[3]并抛出空异常不会失败...

也设置
y your position array got only 3 elements in it...
won''t it fail in 3rd iteration looking for position[3] and throw null exception...

Also set
<br />
 var complicatedName as Array;<br />


我看不到您访问complexName中的数组元素,仅访问整个数组?
I don''t see you access the array elements in complicatedName, only accessing the array as a whole ?


更改该行您在其中定义complicatedName

Change that line where you are defining complicatedName

var complicatedName = new Array();


这篇关于尝试从Javascript中的数组变量创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆