zoukankan      html  css  js  c++  java
  • 基于HTML模板和JSON数据的JavaScript交互

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

    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);
            }
        },
    });
    
  • 相关阅读:
    SharePoint on Windows Server 2008 R2 "客户端不支持使用windows资源管理器打开此列表”
    Exception:Collection was modified; enumeration operation may not execute.
    windows 2008 iisapp
    VMware Workstation OEM (MultiBrand) Bios Installer
    Cajviewer 7.0.2 Windows 7下使用方法:
    Delphi与Windows 7下的用户账户控制(UAC)机制
    VS2008 在WINDOWS 7的UAC开启的情况下如何写注册表HKLM(HKEY_LOCAL_MACHIN)下的值
    windows SDK安装出现没有权限的一个解决办法
    Thinkpad Marker SLIC 2.1 激活Windows 7的详细教程
    点击网页中PDF链接时,打开PDF文档不再是直接IE查看,而是提示下载保存
  • 原文地址:https://www.cnblogs.com/heyinwangchuan/p/6337561.html
Copyright © 2011-2022 走看看