当前位置: 首页 > 图文教程 > 网络编程 > Javascript > 用JS写的一个TableView控件代码

Javascript
用jquery实现学校的校历(asp.net+jquery ui 1.72)
使用JQUERY Tabs插件宿主IFRAMES
jquery 简短右键菜单 多浏览器兼容
JQery 渐变图片导航效果代码 漂亮
HTML node相关的一些资料整理
JavaScript与DropDownList 区别分析
20个非常棒的Jquery实用工具 国外文章
Firefox+FireBug使JQuery的学习更加轻松愉快
firefox firebug中文入门教程 脚本之家新年特别版
firefox插件Firebug的使用教程
javascript addLoadEvent函数说明
javascript getElementsByClassName 和js取地址栏参数
让firefox支持IE的一些方法的javascript扩展函数代码
javascript 多种搜索引擎集成的页面实现代码
Javascript 模拟点击事件(点击链接与html点击) 兼容IE/Firefox
javascript 模拟点击广告
javascript 精确获取样式属性(上)
javascript 精确获取样式属性(下)
javascript 精确获取页面元素的位置
javascript 可控式透明特效实现代码

Javascript 中的 用JS写的一个TableView控件代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-02-27   浏览: 94 ::
收藏到网摘: n/a

JS写的一个TableView控件代码,方便输出表格。 请看看编码是否规范,使用是否方便
HTML:
代码
复制代码 代码如下:

<table id="customTableView">
<thead>
<tr>
<td>编号</td>
<td>姓名</td>
</tr>
</thead>
<tbody><!--template-tbody-->
<tr name="" style=" display:none"><!--template-tr-->
<td bind="0"><span class="red">{value}</span></td>
<td bind="1"><strong>{value}</strong></td>
</tr>
</tbody>
</table>
<hr />
<table id="productTableView">
<thead>
<tr>
<td>编号</td>
<td>名称</td>
</tr>
</thead>
<tbody>
<tr style=" display:none">
<td bind="0"><span class="red">{value}</span></td>
<td bind="1"><strong>{value}</strong></td>
</tr>
</tbody>
</table>

Javascript:
代码
复制代码 代码如下:

<script type="text/javascript">
//class TableView {
//构造函数
function TableView(ID){
var htmlTable = document.getElementById(ID);
this.container = htmlTable.getElementsByTagName("tbody")[0];
this.template = this.container.getElementsByTagName("tr")[0];
}
//成员方法
TableView.prototype.bind = function(dataSource) {
var template = this.template;
var container = this.container;
for(var k=0; k<dataSource.length; k++) {
var newRow = template.cloneNode(true);
newRow.removeAttribute("id");
newRow.removeAttribute("style");
for(var i=0;i<2;i++) {
var dataItem = newRow.cells[i];
dataItem.innerHTML = dataItem.innerHTML.replace("{value}", dataSource[k][dataItem.getAttribute("bind")]);
}
container.appendChild(newRow);
}
}
//}
//测试-1
var productDataSource = [["001","产品名称1"],["002","产品名称2"]];
var productTableView = new TableView("productTableView");
productTableView.bind(productDataSource);
//测试-2
var customDataSource = [["001","客户姓名1"],["002","客户姓名2"]];
var customTableView = new TableView("customTableView");
customTableView.bind(customDataSource);
</script>

输出结果为: