1. 程式人生 > >後臺傳的json 資料遍歷到HTML 頁面

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

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

1、HTML頁面

  1. <!doctype html>
  2. <html>
  3. <head>
  4.     <metacharset="utf-8">
  5.     <title>xx資訊查詢</title>
  6.     <scripttype="text/javascript"src="/js/jquery-1.11.3.min.js"></script>
  7.     <scripttype="text/javascript"
    src="/js/ai/ai-lib.js"></script>
  8.     <scriptsrc="/js/cheat-order.js"></script>
  9. </head>
  10. <body>
  11. <divclass="main pusher">
  12.     <formclass="ui form vertical segment form-search">
  13.         <divclass="fields">
  14.             <divclass="halfsixCl wide field js_query_date">
  15.                 <labelfor="checkDate">預定截止日期</label>
  16.                 <inputname="checkDate"type="text"id="checkDate">
  17.             </div>
  18.             <divclass="sixCl wide field">
  19.                 <label>SEQ</label>
  20.                 <inputname="hotelSeq"id="hotelSeq"
    placeholder=""type="text">
  21.             </div>
  22.             <divclass="sixCl wide field js_query_seq">
  23.                 <label>訂單號</label>
  24.                 <inputtype="text"maxlength="50"name="orderNo"id="orderNo"placeholder="">
  25.             </div>
  26.             <divclass="sixCl wide field js_query_btype">
  27.                 <label>排序欄位</label>
  28.                 <selectname="sortFiled"id="sortFiled">
  29.                     <optionvalue="hotel_seq">酒店seq</option>
  30.                     <optionvalue="order_no">訂單號</option>
  31.                     <optionvalue="cellid">cellid</option>
  32.                 </select>
  33.             </div>
  34.             <div>
  35.                 <label></label>
  36.                 <inputtype="button"value="搜尋"id="btSearch"class="ui right floated positive button btn-search"/>
  37.             </div>
  38.         </div>
  39.     </form>
  40.     <divclass="table-container">
  41.         <tableclass="ui nine column table celled table-result"id="table-result">
  42.             <thead>
  43.             <tr>
  44.                 <th>hotelSeq</th>
  45.                 <th>酒店名稱</th>
  46.                 <th>訂單號</th>
  47.                 <th>聯絡人手機號</th>
  48.                 <th>預定時間</th>
  49.                 <th>userId</th>
  50.                 <th>cellid</th>
  51.                 <th>gps定位城市</th>
  52.                 <th>wifi定位城市</th>
  53.                 <th>定位距離</th>
  54.             </tr>
  55.             </thead>
  56.             <tbodyid="tbody-result">
  57.             </tbody>
  58.         </table>
  59.     </div>
  60. </div>
  61. </body>
  62. </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.         $.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.                     for (i in data) {  
  23.                         str += "<tr>" +  
  24.                         "<td>" + data[i].hotel_seq + "</td>" +  
  25.                         "<td>" + data[i].hotel_name + "</td>" +  
  26.                         "<td>" + data[i].order_no + "</td>" +  
  27.                         "<td>" + data[i].user_phone + "</td>" +  
  28.                         "<td>" + data[i].create_time + "</td>" +  
  29.                         "<td>" + data[i].user_id + "</td>" +  
  30.                         "<td>" + data[i].cellid + "</td>" +  
  31.                         "<td>" + data[i].gps_city + "</td>" +  
  32.                         "<td>" + data[i].cell_city + "</td>" +  
  33.                         "<td>" + data[i].distance + "</td>" +  
  34.                         "</tr>";  
  35.                     }  
  36.                     tbody.innerHTML = str;  
  37.                 }  
  38.             },  
  39.             error: function () {  
  40.                 alert("查詢失敗")  
  41.             }  
  42.         });  
  43.     });  
  44. });  

3、示例圖

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

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