ajax异步获取数据后动态向表格中添加数据的页面

来源:互联网 发布:我的世界pe快速建造js 编辑:程序博客网 时间:2024/04/26 05:55

因为某些原因,项目中突然需要做自己做个ajax异步获取数据后动态向表格中添加数据的页面,网上找了半天都没有 看到现成的,决定自己写个例子

1、HTML页面

[html] view plain copy
print?
  1. <!doctype html>  
  2. <html>  
  3. <head>  
  4.     <meta charset=“utf-8”>  
  5.     <title>xx信息查询</title>  
  6.     <script type=“text/javascript” src=“/js/jquery-1.11.3.min.js”></script>  
  7.     <script type=“text/javascript” src=“/js/ai/ai-lib.js”></script>  
  8.     <script src=“/js/cheat-order.js”></script>     
  9. </head>  
  10.   
  11. <body>  
  12. <div class=“main pusher”>  
  13.     <form class=“ui form vertical segment form-search”>  
  14.         <div class=“fields”>  
  15.             <div class=“halfsixCl wide field js_query_date”>  
  16.                 <label for=“checkDate”>预定截止日期</label>  
  17.                 <input name=“checkDate” type=“text” id=“checkDate”>  
  18.             </div>  
  19.   
  20.             <div class=“sixCl wide field”>  
  21.                 <label>SEQ</label>  
  22.                 <input name=“hotelSeq” id=“hotelSeq” placeholder=“” type=“text”>  
  23.             </div>  
  24.   
  25.             <div class=“sixCl wide field js_query_seq”>  
  26.                 <label>订单号</label>  
  27.                 <input type=“text” maxlength=“50” name=“orderNo” id=“orderNo” placeholder=“”>  
  28.             </div>  
  29.             <div class=“sixCl wide field js_query_btype”>  
  30.                 <label>排序字段</label>  
  31.                 <select name=“sortFiled” id=“sortFiled”>  
  32.                     <option value=“hotel_seq”>酒店seq</option>  
  33.                     <option value=“order_no”>订单号</option>  
  34.                     <option value=“cellid”>cellid</option>  
  35.                 </select>  
  36.             </div>  
  37.             <div>  
  38.                 <label></label>  
  39.                 <input type=“button” value=“搜索” id=“btSearch” class=“ui right floated positive button btn-search”/>  
  40.             </div>  
  41.         </div>  
  42.     </form>  
  43.   
  44.     <div class=“table-container”>  
  45.         <table class=“ui nine column table celled table-result” id=“table-result”>  
  46.             <thead>  
  47.             <tr>  
  48.                 <th>hotelSeq</th>  
  49.                 <th>酒店名称</th>  
  50.                 <th>订单号</th>  
  51.                 <th>联系人手机号</th>  
  52.                 <th>预定时间</th>  
  53.                 <th>userId</th>  
  54.                 <th>cellid</th>  
  55.                 <th>gps定位城市</th>  
  56.                 <th>wifi定位城市</th>  
  57.                 <th>定位距离</th>  
  58.             </tr>  
  59.             </thead>  
  60.             <tbody id=“tbody-result”>  
  61.             </tbody>  
  62.         </table>  
  63.     </div>  
  64. </div>  
  65. </body>  
  66. </html>  
<!doctype html><html><head>    <meta charset="utf-8">    <title>xx信息查询</title>    <script type="text/javascript" src="/js/jquery-1.11.3.min.js"></script>    <script type="text/javascript" src="/js/ai/ai-lib.js"></script>    <script src="/js/cheat-order.js"></script>   </head><body><div class="main pusher">    <form class="ui form vertical segment form-search">        <div class="fields">            <div class="halfsixCl wide field js_query_date">                <label for="checkDate">预定截止日期</label>                <input name="checkDate" type="text" id="checkDate">            </div>            <div class="sixCl wide field">                <label>SEQ</label>                <input name="hotelSeq" id="hotelSeq" placeholder="" type="text">            </div>            <div class="sixCl wide field js_query_seq">                <label>订单号</label>                <input type="text" maxlength="50" name="orderNo" id="orderNo" placeholder="">            </div>            <div class="sixCl wide field js_query_btype">                <label>排序字段</label>                <select name="sortFiled" id="sortFiled">                    <option value="hotel_seq">酒店seq</option>                    <option value="order_no">订单号</option>                    <option value="cellid">cellid</option>                </select>            </div>            <div>                <label></label>                <input type="button" value="搜索" id="btSearch" class="ui right floated positive button btn-search"/>            </div>        </div>    </form>    <div class="table-container">        <table class="ui nine column table celled table-result" id="table-result">            <thead>            <tr>                <th>hotelSeq</th>                <th>酒店名称</th>                <th>订单号</th>                <th>联系人手机号</th>                <th>预定时间</th>                <th>userId</th>                <th>cellid</th>                <th>gps定位城市</th>                <th>wifi定位城市</th>                <th>定位距离</th>            </tr>            </thead>            <tbody id="tbody-result">            </tbody>        </table>    </div></div></body></html>

2、cheat-order.js

[javascript] view plain copy
print?
  1. $$(’#btSearch’).click(function () {  
  2.         var checkDate = $(‘#checkDate’).val();  
  3.         var orderNo = $(‘#orderNo’).val();  
  4.         var sortFiled = $(‘#sortFiled’).val();  
  5.         var hotelSeq = $(‘#hotelSeq’).val();  
  6.         var tbody=window.document.getElementById(“tbody-result”);  
  7.   
  8.         $.ajax({  
  9.             type: ”post”,  
  10.             dataType: ”json”,  
  11.             url: ”ac/order/queryCheatOrder”,  
  12.             data: {  
  13.                 hotelSeq: hotelSeq,  
  14.                 orderNo: orderNo,  
  15.                 sortFiled: sortFiled,  
  16.                 checkDate: checkDate  
  17.             },  
  18.             success: function (msg) {  
  19.                 if (msg.ret) {  
  20.                     var str = “”;  
  21.                     var data = msg.data;  
  22.   
  23.                     for (i in data) {  
  24.                         str += ”<tr>” +  
  25.                         ”<td>” + data[i].hotel_seq + “</td>” +  
  26.                         ”<td>” + data[i].hotel_name + “</td>” +  
  27.                         ”<td>” + data[i].order_no + “</td>” +  
  28.                         ”<td>” + data[i].user_phone + “</td>” +  
  29.                         ”<td>” + data[i].create_time + “</td>” +  
  30.                         ”<td>” + data[i].user_id + “</td>” +  
  31.                         ”<td>” + data[i].cellid + “</td>” +  
  32.                         ”<td>” + data[i].gps_city + “</td>” +  
  33.                         ”<td>” + data[i].cell_city + “</td>” +  
  34.                         ”<td>” + data[i].distance + “</td>” +  
  35.                         ”</tr>”;  
  36.                     }  
  37.                     tbody.innerHTML = str;  
  38.                 }  
  39.             },  
  40.             error: function () {  
  41.                 alert(”查询失败”)  
  42.             }  
  43.         });  
  44.     });  
  45. });  
$(function () {    $('#btSearch').click(function () {        var checkDate = $('#checkDate').val();        var orderNo = $('#orderNo').val();        var sortFiled = $('#sortFiled').val();        var hotelSeq = $('#hotelSeq').val();        var tbody=window.document.getElementById("tbody-result");        $.ajax({            type: "post",            dataType: "json",            url: "ac/order/queryCheatOrder",            data: {                hotelSeq: hotelSeq,                orderNo: orderNo,                sortFiled: sortFiled,                checkDate: checkDate            },            success: function (msg) {                if (msg.ret) {                    var str = "";                    var data = msg.data;                    for (i in data) {                        str += "<tr>" +                        "<td>" + data[i].hotel_seq + "</td>" +                        "<td>" + data[i].hotel_name + "</td>" +                        "<td>" + data[i].order_no + "</td>" +                        "<td>" + data[i].user_phone + "</td>" +                        "<td>" + data[i].create_time + "</td>" +                        "<td>" + data[i].user_id + "</td>" +                        "<td>" + data[i].cellid + "</td>" +                        "<td>" + data[i].gps_city + "</td>" +                        "<td>" + data[i].cell_city + "</td>" +                        "<td>" + data[i].distance + "</td>" +                        "</tr>";                    }                    tbody.innerHTML = str;                }            },            error: function () {                alert("查询失败")            }        });    });});

3、示例图

备注:css已经删除了,效果比上面示例图要差些


原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明

阅读全文
0 0
原创粉丝点击