1,JTable.js
var JTable = Class.create();
JTable.prototype=

{

/** *//**
* @param titles is a array([])
* @param datas is a double array([][])
* @param tablePropertyStr is the table'property.
*/
initialize:function(titles, datas, tablePropertyStr)//titles is a array([]), datas is a double array([][])

{
this.titles = titles;
this.datas = datas;
if (tablePropertyStr)

{
this.tablePropertyStr = tablePropertyStr;
}
else

{
this.tablePropertyStr = "";
}
},

/** *//**
* create a table and displays it if needed.
* @param id the place's id which contains the table
*/
createTable:function(id, isDisplayTable)

{
var tableStr = "<table " + this.tablePropertyStr + ">";
var theadStr = "<thead>";
if (this.titles == null || this.titles.length == 0)

{
theadStr = "";
}
else

{
for (var i = 0; i < this.titles.length; i++)

{
theadStr += "<td>" + this.titles[i] + "</td>";
}
theadStr += "</thead>"
}
var tbodyStr = "<tbody>";
if (this.datas == null || this.datas.length == 0)

{
tbodyStr = "";
}
else

{
for(var j = 0; j < this.datas.length; j++)

{
tbodyStr += "<tr>";
for(var n = 0; n < this.datas[j].length; n++)

{
tbodyStr += "<td>" + this.datas[j][n] + "</td>";
}
tbodyStr += "</tr>";
}
}
tbodyStr += "</tbody>";
tableStr += theadStr + tbodyStr + "</table>";
//alert(tableStr); for test
if (isDisplayTable)

{
$(id).innerHTML = tableStr;
}
else

{
return tableStr;
}
},
displayTable:function(id)

{
this.createTable(id, true);
}
};
2,使用test.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">



<html>

<head>
<script src="prototype.js" language="JavaScript"></script>
<script src="JTable.js" language="JavaScript"></script>
</head>

<body>
<div id="value_list">
</div>

<script language="JavaScript">

function _show()


{
j = 0;
var titles = new Array();
titles[j++] = "name";
titles[j++] = "address";
titles[j++] = "telephone";
titles[j++] = "身份证";
var datas = new Array(5);
for (var i = 0; i < 5; i++)

{
datas[i] = new Array(4);
var n = 0;
datas[i][n++] = "name"+i;
datas[i][n++] = "address"+i
datas[i][n++] = "telephone"+i;
datas[i][n++] = "身份证"+i;
}
var tableCreater = new JTable(titles, datas, 'width="80%" border="1" cellpadding="1" cellspacing="1" bordercolorlight="#C2E0E9" bordercolordark="#FFFFFF"');
tableCreater.displayTable("value_list");
}
_show();

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

3,要注意的是引入prototype.js
posted on 2007-07-25 14:45
Java_Tan 阅读(213)
评论(0) 编辑 收藏