如何使用javascript动态打印表格

How to use javascript to print table dynamically?

本文关键字:打印 表格 动态 javascript 何使用      更新时间:2023-09-26

我想动态打印一个表,但是我的程序显示得很混乱。我希望每行包含两个元素。我想这样显示这个表。在15秒内,它获得一个名称并显示它。在接下来的15秒里,它得到另一个名字并显示出来。通过这种方式重复,显示所有数据。下面是我的主代码:

  var count = 0;
  function AddTd(id, showname)
  {
    console.log(id);
    if(id == 0)   // if this is in the first column
    {
      var str = '<tr><td width="10px">'+showname+'</td>';
      $("#datashow").append(str);
      count = 1;  //  next element should in second column
      return;
    }
    if(id == 1)   // if this is in the second column
    {
      var str = '<td width="10px">'+showname+'</td></tr>';
      $("#datashow").append(str);
      count = 0;  //  next element should in first column
      return;
    }
  }
  setInterval('change()',50); // In function change, print the table dynamically

这将创建一个包含尽可能多元素的表,并且每行2列。

http://jsfiddle.net/XfKME/3/

 var names = ["name1", "name2", "name3", "name4"];
 var tbody = "<table>";
 $.each(names, function (i, item) {
     if (i % 2 == 0)
         tbody += "<tr>";
     tbody += "<td>";
     tbody += item;
     tbody += "</td>"
     if (i % 2 == 1)
         tbody += "</tr><br />";
 });
 tbody += "</table>";