一个模板控件规定了它的模板语法和js api,这是一个repeater控件的js实现:
<!doctype html>
<html>
<head>
<meta charset="gb2312" />
<title>javascript repeater控件</title>
</head>
<body>
<div id="crossrate">
<!-- placeholder {-->
<table width="815" class="table-data">
<tr>
<th>代码</th>
<th>名称</th>
<th>最新价</th>
<th>涨跌额</th>
<th>涨跌幅</th>
<th>开盘</th>
<th>最高</th>
<th>最低</th>
<th>昨收</th>
</tr>
</table>
<!-- placeholder }-->
<script type="text/template" id="crossrateheader">
<table width="815" class="table-data">
<tr>
<th>代码</th>
<th>名称</th>
<th>最新价</th>
<th>涨跌额</th>
<th>涨跌幅</th>
<th>开盘</th>
<th>最高</th>
<th>最低</th>
<th>昨收</th>
</tr>
</script>
<script type="text/template" id="crossrateitem">
<tr>
<td>{$datarow[1]}</td>
<td>{$datarow[2]}</td>
<td>{$datarow[5]}</td>
<td>{$datarow[17]}</td>
<td>{$datarow[18]}</td>
<td>{$datarow[4]}</td>
<td>{$datarow[6]}</td>
<td>{$datarow[7]}</td>
<td>{$datarow[3]}</td>
</tr>
</script>
<script type="text/template" id="crossratefooter">
</table>
</script>
</div>
<script>
//view
(function(ns){
function init(){
var container = document.getelementbyid(crossrate);
container.setattribute(data-headertemplate, document.getelementbyid(crossrateheader).text);
container.setattribute(data-itemtemplate, document.getelementbyid(crossrateitem).text);
container.setattribute(data-footertemplate, document.getelementbyid(crossratefooter).text);
}
function render(dt){
var container = document.getelementbyid(crossrate),
headerhtml = container.getattribute(data-headertemplate),
rowhtml = container.getattribute(data-itemtemplate),
footerhtml = container.getattribute(data-footertemplate);
var repater = new repater(headerhtml,rowhtml,footerhtml);
var datarow = [];
for(var i=0,n=dt.length; i
repater.set(datarow,datarow);
repater.parse();
}
container.innerhtml = repater.tohtml();
}
ns.crossrate = {
init: init,
render: render,
fill: function(data){
render(data);
}
};
ns.crossrate.init();
}(window));
//异步获取数据渲染数据data
var datas = [usdeur0,usdeur,美元欧元,0.7731,0.7732,0.7723,0.7734,0.7717,0,22913,0.0000,0,0,0.7731,0.0000,0,0,-0.0008,-0.10%,0.0000,1,3848,0,-1,1,0.0000,0.0000,2012-05-10 13:49:53,3,usdhkd0,usdhkd,美元港币,7.7625,7.7633,7.7621,7.7634,7.7617,0,14208,0.0000,0,0,7.7625,0.0000,0,0,-0.0004,-0.01%,0.0000,2,2062,0,-1,0,0.0000,0.0000,2012-05-10 13:49:49,3,usdjpy0,usdjpy,美元日元,79.71,79.73,79.62,79.77,79.57,0,25489,0.00,0,0,79.71,0.00,0,0,-0.09,-0.11%,0.00,1,4307,0,-1,-1,0.00,0.00,2012-05-10 13:50:13,3,usdchf0,usdchf,美元瑞郎,0.9285,0.9287,0.9276,0.9289,0.9266,0,29637,0.0000,0,0,0.9285,0.0000,0,0,-0.0009,-0.10%,0.0000,1,4960,0,-1,1,0.0000,0.0000,2012-05-10 13:50:02,3,gbpusd0,gbpusd,英镑美元,1.6134,1.6136,1.6138,1.6144,1.6121,0,20808,0.0000,0,0,1.6134,0.0000,0,0,0.0004,0.02%,0.0000,2,5381,0,-1,0,0.0000,0.0000,2012-05-10 13:50:04,3];
//填充数据到view
crossrate.fill(datas);
//repater模板控件 www.2cto.com
function repater(headerhtml,rowhtml,footerhtml) {
var _this = this;
var n = 0;
_this.cache = [];
_this.dic = {};
_this.header = headerhtml;
_this.row = rowhtml;
_this.footer = '</table>';
if (headerhtml) _this.header = headerhtml;
if (rowhtml) _this.row = rowhtml;
if (footerhtml) _this.footer = footerhtml;
_this.set = function(tag, val) {
this.dic[tag] = val;
};
_this.parse = function(dic) {
var row = this.row,
dic = dic || this.dic,
re = /\{\$(\w+)(?:\[(\d+)\])?(?:\|(\w+))?\}/g,
html;
html = row.replace(re, function(a, b, c, d) {
var val;
if (typeof dic[b] == undefined){
return b;
}
if (dic[b].constructor == array) {
val = dic[b][c];
} else {
val = dic[b];
}
if (repater[d] || window[d]) {//修饰符
val = (repater[d] || window[d])(val);
}
return val;
});
_this.cache[n++] = html;
return html;
};
_this.tohtml = function(args) {
var cache = _this.cache,
result;
_this.cache = [];
n = 0;
result = _this.header + cache.join() + _this.footer;
for (i in args) {
if (args.hasownproperty(i)) {
result = result.replace({$+i+}, args[i]);
}
}
return result;
};
}
</script>
</body>
</html>