基于HTML模板和JSON數(shù)據(jù)的JavaScript交互(移動端)
寫本文之前,我正在做一個基于Tab頁的訂單中心:
每點擊一個TAB標(biāo)簽,會請求對應(yīng)狀態(tài)的訂單列表。之前的項目,我會在js里使用 + 連接符連接多個html內(nèi)容:
var html = ''; html += '<div class="empty-list">' + '<div class="icon icon-box"></div>'+ '<div>還沒有訂單</div>'+ '</div>';
Html內(nèi)容少點還好,但是當(dāng)內(nèi)容多起來的時候,再使用這種方式,以后維護(hù)起來不方便,也影響美觀,可閱讀性差。
突然想起來PHP模板的相關(guān)知識,那么應(yīng)該也有類似JavaScript模板一說。由于為了快速使用在項目上,就去網(wǎng)上找了找。還真有:
基于HTML模板和JSON數(shù)據(jù)的JavaScript交互
http://www.zhangxinxu.com/wordpress/2012/09/javascript-html-json-template/
看了下文章,使用起來還是蠻簡單的:
1、準(zhǔn)備好html模板
<textarea class="js-order-tmp" style="display:none;"> <!--goods-block--> <div class="block m-s-order"> <div class="block-item block-news"> <a href="<?php echo site_url('order/detail/'. '$id$')?>" class="dis-block external"> <div class="item-img"> <img src="$thumb$"/> </div> <div class="item-intro"> <div class="sub-l"> <p class="t-order-num">訂單號:$order_num$</p> <p class="t-name">收件人:$cus_name$</p> <p class="t-date">$create_time$</p> </div> <div class="sub-r"> <p class="t-expand"><span class="icon icon-right"></span></p> <p class="t-flag">$flag_name$</p> </div> </div> <div class="clear"></div> </a> </div> </div> <!--/goods-block--> </textarea> <textarea class="js-no-order-tmp" style="display:none;"> <!-- no order --> <div class="empty-list"> <div class="icon icon-box"></div> <div>還沒有訂單</div> </div> <!-- /no order --> </textarea>
其中變量部分全部用 variate variate 表示。
2、模板方法很簡單,直接寫一個基于字符串原型的擴展方法,確保全局可用:
String.prototype.temp = function(obj) { return this.replace(/\$\w+\$/gi, function(matchs) { var returns = obj[matchs.replace(/\$/g, "")]; return (returns + "") == "undefined"? "": returns; }); };
主要使用到了正則知識。
3、準(zhǔn)備json數(shù)據(jù):
{ "ecd": 0, "msg": "成功", "result": [{ "id": "32", "order_num": "test-001", "title": "test", "thumb": "http:\/\/40DA1265-40F6-D622-8BA5-04BA0AF72573.jpg", "item_id": "21", "price": "0.11", "cus_name": "test", "cus_tel": "10086", "cus_address": "北京 北京海淀區(qū)", "flag": "5", "create_time": "20160329115544", "update_time": "20160330120001", "flag_name": "訂單已取消" }], "locate": "" }
4、使用ajax顯示數(shù)據(jù)
$.progress_show('正在努力加載中'); $.ajax({ url: site_url + 'api/order/getAll/' + status, type: 'get', dataType: 'json', error: doAjax.error, success: function (response) { $.progress_hide(); if (response.ecd == '0') { var htmlList = '', htmlTemp = $("textarea.js-order-tmp").val(); if(typeof response.result === 'undefined'){ htmlList = $("textarea.js-no-order-tmp").val(); }else{ $.each(response.result, function(i,el) { htmlList += htmlTemp.temp(el); }); } $('.js-status-' + status).empty().append(htmlList); return true; } else { return $.alert(response.msg); } }, });
這里的部分方法沒有給出,大家知道流程、原理即可。通過點擊TAB標(biāo)簽,就可以顯示數(shù)據(jù)了:
以上內(nèi)容是小編給大家介紹的基于HTML模板和JSON數(shù)據(jù)的JavaScript交互(移動端),希望對大家有所幫助!
- ASP+模板生成Word、Excel、html的代碼
- php Smarty模板生成html文檔的方法
- JavaScript和ActionScript的交互實現(xiàn)代碼
- PHP JSON格式數(shù)據(jù)交互實例代碼詳解
- thinkPHP的Html模板標(biāo)簽使用方法
- 解析Android中webview和js之間的交互
- PHP與javascript實現(xiàn)變量交互的示例代碼
- 多種方式實現(xiàn)JS調(diào)用后臺方法進(jìn)行數(shù)據(jù)交互
- extjs中form與grid交互數(shù)據(jù)(record)的方法
- C#.NET采用HTML模板發(fā)送電子郵件完整實例
相關(guān)文章
千分位數(shù)字格式化(用逗號隔開 代碼已做了修改 支持0-9位逗號隔開)的JS代碼
這篇文章主要介紹了千分位數(shù)字格式化的JS代碼,有需要的朋友可以參考一下2013-12-12微信公眾號網(wǎng)頁授權(quán)登錄的超簡單實現(xiàn)步驟
微信開放平臺為第三方移動程序提供分享、傳播的接口,使用戶可將第三方程序的內(nèi)容發(fā)布給好友或分享至朋友圈,下面這篇文章主要給大家介紹了關(guān)于微信公眾號網(wǎng)頁授權(quán)登錄的超簡單實現(xiàn)方法,需要的朋友可以參考下2022-06-06使用documentElement正確取得當(dāng)前可見區(qū)域的大小
如何取得當(dāng)前瀏覽器里面可見區(qū)域的大???其他方法都不適用,只有documentElement才可以,需要的朋友可以參考下2014-07-07用javascript調(diào)出windows色版的代碼
用javascript調(diào)出windows色版的代碼...2007-07-07KnockoutJS 3.X API 第四章之事件event綁定
event綁定即為事件綁定,即當(dāng)觸發(fā)相關(guān)DOM事件的時候回調(diào)函數(shù),這篇文章主要介紹了KnockoutJS 3.X API 第四章之事件event綁定的相關(guān)知識,感興趣的朋友一起看看吧2016-10-10TypeScript中定義變量方式以及數(shù)據(jù)類型詳解
TypeScript支持 JavaScript的所有語法和語義,同時通過作為ECMAScript的超集來提供一些額外的功能,如類型檢測和更豐富的語法,這篇文章主要給大家介紹了關(guān)于TypeScript中定義變量方式以及數(shù)據(jù)類型詳解的相關(guān)資料,需要的朋友可以參考下2022-08-08