手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >用JS写的一个TableView控件代码
用JS写的一个TableView控件代码
摘要:请看看编码是否规范,使用是否方便HTML:代码复制代码代码如下:编号姓名{value}{value}编号名称{value}{value}Ja...

请看看编码是否规范,使用是否方便

HTML:

代码

复制代码 代码如下:

<table id="customTableView">

<thead>

<tr>

<td>编号</td>

<td>姓名</td>

</tr>

</thead>

<tbody><>

<tr name=""><>

<td bind="0"><span>{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>

<td bind="0"><span>{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>

输出结果为:

用JS写的一个TableView控件代码1

【用JS写的一个TableView控件代码】相关文章:

用JavaScript实现对话框的教程

实现DIV圆角的JavaScript代码

js实现顶部可折叠的菜单工具栏效果实例

网页常用特效代码整理

免费空间广告万能消除代码

网页里控制图片大小的相关代码

Javascript随机显示图片的源代码

AngularJS身份验证的方法

些很实用且必用的小脚本代码

在页面中输出当前客户端时间javascript实例代码

精品推荐
分类导航