首页 > 编程语言 > 详细

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

时间:2017-01-21 20:19:31      阅读:268      评论:0      收藏:0      [点我收藏+]

为了将后台的数据处理放到前台,我们可能出现以下的代码

var html = ‘‘;
html += ‘
‘ + ‘
‘+ ‘
还没有订单
‘+‘
‘;

在实际的应用中后台传过来的数据更长,拼接的html会更长,这样维护起来就变得非常的困难

html




html处理

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

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": ""
}

js

$.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);
returntrue;
} else {
return $.alert(response.msg);
}
},
});

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

原文:http://www.cnblogs.com/heyinwangchuan/p/6337561.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!