欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于HTML模板和JSON數(shù)據(jù)的JavaScript交互(移動(dòng)端)

 更新時(shí)間:2016年04月06日 14:10:02   作者:飛鴻影~  
這篇文章主要介紹了基于HTML模板和JSON數(shù)據(jù)的JavaScript交互(移動(dòng)端)的相關(guān)資料,需要的朋友可以參考下

寫本文之前,我正在做一個(gè)基于Tab頁(yè)的訂單中心:

每點(diǎn)擊一個(gè)TAB標(biāo)簽,會(huì)請(qǐng)求對(duì)應(yīng)狀態(tài)的訂單列表。之前的項(xiàng)目,我會(huì)在js里使用 + 連接符連接多個(gè)html內(nèi)容:

var html = '';
html += '<div class="empty-list">' + 
'<div class="icon icon-box"></div>'+
'<div>還沒(méi)有訂單</div>'+
'</div>';

Html內(nèi)容少點(diǎn)還好,但是當(dāng)內(nèi)容多起來(lái)的時(shí)候,再使用這種方式,以后維護(hù)起來(lái)不方便,也影響美觀,可閱讀性差。

突然想起來(lái)PHP模板的相關(guān)知識(shí),那么應(yīng)該也有類似JavaScript模板一說(shuō)。由于為了快速使用在項(xiàng)目上,就去網(wǎng)上找了找。還真有:

基于HTML模板和JSON數(shù)據(jù)的JavaScript交互

http://www.zhangxinxu.com/wordpress/2012/09/javascript-html-json-template/

看了下文章,使用起來(lái)還是蠻簡(jiǎn)單的:

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">訂單號(hào):$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>還沒(méi)有訂單</div>
</div>
<!-- /no order -->
</textarea> 

其中變量部分全部用 variate variate 表示。

2、模板方法很簡(jiǎn)單,直接寫一個(gè)基于字符串原型的擴(kuò)展方法,確保全局可用:

String.prototype.temp = function(obj) {
return this.replace(/\$\w+\$/gi, function(matchs) {
var returns = obj[matchs.replace(/\$/g, "")]; 
return (returns + "") == "undefined"? "": returns;
});
}; 

主要使用到了正則知識(shí)。

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);
}
},
}); 

這里的部分方法沒(méi)有給出,大家知道流程、原理即可。通過(guò)點(diǎn)擊TAB標(biāo)簽,就可以顯示數(shù)據(jù)了:

以上內(nèi)容是小編給大家介紹的基于HTML模板和JSON數(shù)據(jù)的JavaScript交互(移動(dòng)端),希望對(duì)大家有所幫助!

相關(guān)文章

最新評(píng)論