JavaScript interaction (mobile end) _javascript techniques based on HTML templates and JSON data

Source: Internet
Author: User
Tags prepare

Before I wrote this article, I was doing a tab-based order center:

Each click of a tab tab will request a list of orders for the corresponding status. Before the project, I will use the + connector in JS to connect multiple HTML content:

var html = ';
html = ' <div class= ' empty-list ' > ' + 
' <div class= ' icon icon-box ' ></div> ' +
' <div> There is no order </div> ' +
' </div> ';

HTML content less good, but when the content is more, then use this way, later maintenance inconvenient, but also affect the beautiful, poor reading.

Suddenly think of the knowledge of PHP templates, then there should be a similar JavaScript template said. Because in order to quickly use in the project, they went online to find. And there's really:

JavaScript interaction based on HTML templates and JSON data

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

Read the article, the use of it is quite simple:

1. Prepare HTML templates

<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 ">  </div> < Div class= "Item-intro" > <div class= "sub-l" > <p class= "T-order-num" > Order number: $order _num$</p> <p class= "T-name" > Recipient: $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> not yet ordered </div> </div> <!--/no Order--> </textarea>  

Where the variable part is all expressed in variate variate.

2, the template method is very simple, directly write a string based on the prototype extension method to ensure global availability:

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

The main use of regular knowledge.

3. Prepare JSON data:

{
"ECD": 0,
"MSG": "Success",
"result": [{
"id": "]",
"Order_num": "test-001",
"title": " Test ",
" thumb ":" Http:\/\/40da1265-40f6-d622-8ba5-04ba0af72573.jpg ",
" item_id ":" "," Price
":" 0.11 ",
" Cus_name ":" Test ",
" Cus_tel ":" 10086 ",
" cus_address ":" Beijing Haidian, Beijing ",
" flag ":" 5 ",
" Create_time ":" 20160329115544 ","
update_time ":" 20160330120001 ",
" Flag_name ":" Order Canceled "
}],
" Locate ":" "

4. Display data using Ajax

$.progress_show (' Trying to load in ');
$.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);
}
},

Some of the methods here are not given, we know the process, the principle can be. By clicking the Tab tab, you can display the data:

The above is a small series to introduce the HTML template and JSON data based on JavaScript interaction (mobile end), I hope to help!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.