後臺傳的json 資料遍歷到HTML 頁面

DreamWeaver_Zhou發表於2018-01-30

因為某些原因,專案中突然需要做自己做個ajax非同步獲取資料後動態向表格中新增資料的頁面,網上找了半天都沒有 看到現成的,決定自己寫個例子

1、HTML頁面

  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>  

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

3、示例圖

備註:css已經刪除了,效果比上面示例圖要差些


原創作品,允許轉載,轉載時請務必以超連結形式標明文章 原始出處 、作者資訊和本宣告

相關文章