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

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

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

}

},

});

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值