日期:2014-05-18  浏览次数:20998 次

js动态生成table怎么让table的数据是从数据库中绑定过来
这是参考(因为表生成出来了)有好的方法可采取
<script type="text/javascript">
var oTable = document.createElement("table");
oTable.border = 1;
oTable.style.width = "300px";
oTable.style.height = "300px";
oTable.style.borderCollapse = "collapse";

var oTr1 = oTable.insertRow();
var oTd1 = oTr1.insertCell();
oTd1.rowSpan = 3;
var oTd2 = oTr1.insertCell();
var oTd3 = oTr1.insertCell();

var oTr2 = oTable.insertRow();
var oTd22 = oTr2.insertCell();
var oTd23 = oTr2.insertCell();

var oTr3 = oTable.insertRow();
var oTd32 = oTr3.insertCell();
var oTd33 = oTr3.insertCell();

var oTr4 = oTable.insertRow();
var oTd41 = oTr4.insertCell();
oTd41.colSpan = 2;
var oTd43 = oTr4.insertCell();

document.body.appendChild(oTable);
</script>
谢谢了

------解决方案--------------------
先从数据库读数据
然后遍历单元格自己填每个td的innerText
------解决方案--------------------
后台生成,前台显示
------解决方案--------------------
通过ajax取数据再遍历数据,生成单元格
或在cs里生成动态table
HtmlTable table1 = new HtmlTable();
  HtmlTableRow row;
  HtmlTableCell cell;
  table1.Border =1;
  table1.CellPadding =3;
  table1.CellSpacing =3;
  for(int i=1; i<=5; i++)
  {
  row = new HtmlTableRow();
}

}
------解决方案--------------------
我是在.ashx(IHttpHandler)中循环读数据,然后用context.Response.Write输出的,最后 放到 div的
innerHTML中:

//javascript(用XMLHttpRequest取出ashx返回的网页代码)

 var _console = document.getElementById('Div_Sendto');
if (_console != null) _console.innerHTML = data;

//ashx
for (int i=0 ;i<temp_Article_list.Count;i++)
{
Article_sub sub=temp_Article_list[i];
context.Response.Write("<table id='tab_" + sub.Sort.ToString() + "' border='1' cellpadding='3' cellspacing='3' bordercolor=#008000 width='" + width.ToString() + "px'> ");
context.Response.Write(" <tr bgcolor='lightyellow'> ");
context.Response.Write(" <td width='222px' >发言时间:" + sub.UpLoadDate + "</td> ");
context.Response.Write(" <td >给 “" + sub.ToAuthor + "” 的留言</td> ");
context.Response.Write(" </tr> ");
context.Response.Write(" <tr bgcolor='lightcyan'> ");
context.Response.Write(" <td width='222px'> <img id='AuthorSub_" + sub.Sort.ToString() + "' src='images/zuozhe.GIF' " +
" onmouseover=\"AboutYonghu('replaceconsole','AuthorSub_" + sub.Sort.ToString() + "','getProfile.aspx?width=300&','" + sub.Author + "')\" " +
" onmouseout=\"HideAboutZuoZhe('replaceconsole')\" alt='' />");
context.Response.Write(sub.Sort.ToString() + "楼:" + sub.Author);

if (context.User.Identity.IsAuthenticated) //没有登录不可以留言
{
context.Response.Write("<br/><a id='fy" + sub.Sort.ToString() + "' href='javascript:sendto(\"" + sub.Author + "\") '> 对“" + sub.Author + "”说</a> <br/>");
if (context.User.Identity.Name == sub.Article_Author || context.User.IsInRole("Admin"))
{
context.Response.Write("<a id='sc" + sub.Sort.ToString() + "' href='javascript:delsub(\"" + sub.Sort + "\") '> 删除</a><br/>");