欢迎来到代码驿站!

JavaScript代码

当前位置:首页 > 网页前端 > JavaScript代码

用JS写的一个TableView控件代码

时间:2021-02-10 14:50:31|栏目:JavaScript代码|点击:
请看看编码是否规范,使用是否方便
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>

输出结果为:
 

上一篇:JavaScript代理模式原理与用法实例详解

栏    目:JavaScript代码

下一篇:使用JavaScript计算前一天和后一天的思路详解

本文标题:用JS写的一个TableView控件代码

本文地址:http://www.codeinn.net/misctech/60436.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有