-3

使用 Javascript、Html 和 BFO(Big Faceless Org)PDF 生成器,我需要为销售订单中的每一页创建一个具有固定行数的表。基本上,该表应该由固定的 10 行组成,但是填充有信息的行可能只有 1 或 2 行。其他行将为空。

任何人都可以帮忙吗?

目前我只有:

for(I=0;i<=salesitem.length;i++){
    document.write('<tr><td>salesitem[I]</td></tr>');
}

这会为每个销售项目生成一个 td 行。但是,我想在表中固定 10 行。

4

2 回答 2

1

There are several issues with the tiny piece of code you've supplied...

  • javascript is case sensitive, therefore I and i are different variables
  • looping from 0 to i<=salesitems.length will end in an error, because if length is 10 and you use salesitem[10] it will fail (arrays are 0-based, and therefore an array with length of 10 has items 0 to 9)
  • I believe you think that salesitem[I] will process like PHP, it won't. PHP allows you to use echo "print this $varName", javascript simply doesn't allow that.

Try something along these lines...

for (i = 0; i < salesitem.length; i++) {
  document.write('<tr><td>' + salesitem[i] + '</td></tr>');
}
for (i = salesitem.length; i < 10; i++) {
  document.write('<tr><td></td></tr>');
}

The first loop will display everything in your array (including any entries over 10)

The second loop will display lines to complete the table (if there are less than 10 entries)

于 2013-09-30T11:48:04.303 回答
0

尝试这个。

for (var i=0;i<10;i++) { 
    document.write("<tr>");

    //This will depend entirely upon what conditions have to be met
    //in order for you to display the content of each row
    if(variable == 'condition') {
         document.write("<td>content</td>");
    }

    document.write("</tr>");
}
于 2013-09-30T10:33:55.267 回答