.net JS類比Repeater控制項的實現代碼

來源:互聯網
上載者:User

功能強大的模板引擎大都需要對模板進行文法解析,會有效能問題。通過把一個大的模板引擎根據不同呈現需求分隔成多個互相獨立模板控制項,可以降低處理複雜度提供處理效能,可以根據需求靈活組合這些模板控制項得到一個可以定製的模板功能庫。

<!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<n; i++){ dataRow = dt[i].split(","); 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模板控制項 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>

相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。