Home > Web Front-end > JS Tutorial > body text

JavaScript interaction based on HTML template and JSON data (mobile terminal)_javascript skills

WBOY
Release: 2016-05-16 15:06:26
Original
1443 people have browsed it

写本文之前,我正在做一个基于Tab页的订单中心:

每点击一个TAB标签,会请求对应状态的订单列表。之前的项目,我会在js里使用 + 连接符连接多个html内容:

var html = '';
html += '<div class="empty-list">' + 
'<div class="icon icon-box"></div>'+
'<div>还没有订单</div>'+
'</div>';
Copy after login

Html内容少点还好,但是当内容多起来的时候,再使用这种方式,以后维护起来不方便,也影响美观,可阅读性差。

突然想起来PHP模板的相关知识,那么应该也有类似JavaScript模板一说。由于为了快速使用在项目上,就去网上找了找。还真有:

基于HTML模板和JSON数据的JavaScript交互

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

看了下文章,使用起来还是蛮简单的:

1、准备好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="<&#63;php echo site_url('order/detail/'. '$id$')&#63;>" 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> 
Copy after login

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

2、模板方法很简单,直接写一个基于字符串原型的扩展方法,确保全局可用:

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

主要使用到了正则知识。

3、准备json数据:

{
"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": "北京 北京海淀区",
"flag": "5",
"create_time": "20160329115544",
"update_time": "20160330120001",
"flag_name": "订单已取消"
}],
"locate": ""
} 
Copy after login

4、使用ajax显示数据

$.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);
}
},
}); 
Copy after login

这里的部分方法没有给出,大家知道流程、原理即可。通过点击TAB标签,就可以显示数据了:

以上内容是小编给大家介绍的基于HTML模板和JSON数据的JavaScript交互(移动端),希望对大家有所帮助!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template