The mobile side JavaScript interaction based on HTML templates and JSON data

Source: Internet
Author: User
Tags php template

Before writing this article, I was doing an order center based on the tab page:

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

var html = '+ = ' <div class= "empty-list" > ' +     ' <div class= "icon Icon-box" ></div > ' +    ' <div> no orders </div> ' + ' </div> ';

HTML content is a little bit better, but when the content of the time, and then use this way, later maintenance is not convenient, but also affect the beautiful, readable poor.

Suddenly think of the PHP template related knowledge, then there should be a similar JavaScript template to say. In order to quickly use on the project, I went to search the Internet. There are really:

JavaScript interaction based on HTML templates and JSON data
http://www.zhangxinxu.com/wordpress/2012/09/javascript-html-json-template/

Read the next article, it is quite simple to use:

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=" s Ub-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&gt    ; </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> No orders </div> </div> <!--/no Order--></textarea>

Where the variable part is all represented by two dollar sign $ .

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

function (obj) {    returnthisfunction(matchs) {        var returns = Obj[matchs.replace (/\$/g, "")];                 return (returns + "") = = "undefined"? "" : Returns;    });};

The main use of the regular knowledge.

3. Prepare JSON data:

{    "ECD": 0,    "MSG": "Success",    "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": "Haidian District, Beijing, China",        "Flag": "5",        "Create_time": "20160329115544",        "Update_time": "20160330120001",        "Flag_name": "Order Canceled"    }],    "Locate": ""}

4. Using AJAX to request and display data

$.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 ') {            varHtmllist = ', Htmltemp = $ ("textarea.js-order-tmp"). Val (); if(typeofResponse.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, you know the process, the principle can be. By clicking the Tab tab, you can display the data:

Finally thank Bo Master Zhang Xin Xu to provide ideas and articles! Details can be read:

JavaScript interaction based on HTML templates and JSON data «Zhang Xin Xu-Xin space-Xin Life
http://www.zhangxinxu.com/wordpress/2012/09/javascript-html-json-template/

The mobile side JavaScript interaction based on HTML templates and JSON data

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.