基于jQuery的AJAX和JSON實現(xiàn)純html數(shù)據(jù)模板
通過jQuery內置的AJAX功能,直接訪問后臺獲得JSON格式的數(shù)據(jù),然后通過jQuer把數(shù)據(jù)綁定到事先設計好的html模板上,直接在頁面上顯示。
我們先來看一下html模板:
<table id="datas" border="1" cellspacing="0" style="border-collapse: collapse"> <tr> <th> 訂單ID</th> <th> 客戶ID</th> <th> 雇員ID</th> <th> 訂購日期</th> <th> 發(fā)貨日期</th> <th> 貨主名稱</th> <th> 貨主地址</th> <th> 貨主城市</th> <th> 更多信息</th> </tr> <tr id="template"> <td id="OrderID"> </td> <td id="CustomerID"> </td> <td id="EmployeeID"> </td> <td id="OrderDate"> </td> <td id="ShippedDate"> </td> <td id="ShippedName"> </td> <td id="ShippedAddress"> </td> <td id="ShippedCity"> </td> <td id="more"> </td> </tr> </table>
一定要注意的就是里面所有的id屬性,這個是一個關鍵。再來看一下AJAX請求和綁定數(shù)據(jù)的代碼。
$.ajax({ type: "get",//使用get方法訪問后臺 dataType: "json",//返回json格式的數(shù)據(jù) url: "BackHandler.ashx",//要訪問的后臺地址 data: "pageIndex=" + pageIndex,//要發(fā)送的數(shù)據(jù) complete :function(){$("#load").hide();},//AJAX請求完成時隱藏loading提示 success: function(msg){//msg為返回的數(shù)據(jù),在這里做數(shù)據(jù)綁定 var data = msg.table; $.each(data, function(i, n){ var row = $("#template").clone(); row.find("#OrderID").text(n.訂單ID); row.find("#CustomerID").text(n.客戶ID); row.find("#EmployeeID").text(n.雇員ID); row.find("#OrderDate").text(ChangeDate(n.訂購日期)); if(n.發(fā)貨日期!== undefined) row.find("#ShippedDate").text(ChangeDate(n.發(fā)貨日期)); row.find("#ShippedName").text(n.貨主名稱); row.find("#ShippedAddress").text(n.貨主地址); row.find("#ShippedCity").text(n.貨主城市); row.find("#more").html("<a href=OrderInfo.aspx?id=" + n.訂單ID + "&pageindex="+pageIndex+"> More</a>"); row.attr("id","ready");//改變綁定好數(shù)據(jù)的行的id row.appendTo("#datas");//添加到模板的容器中 });
這個是jQuery的AJAX方法,返回數(shù)據(jù)并不復雜,主要說明一下怎么把數(shù)據(jù)按模板的定義顯示到到頁面上。首先是這個“var row = $(“#template”).clone();”先把模板復制一份,接下來row.find(“#OrderID”).text(n.訂單ID);,表示找到id=OrderID的標記,設置它的innerText為相應的數(shù)據(jù),當然也可以設置為html格式的數(shù)據(jù)?;蛘呤峭ㄟ^外部的函數(shù)把數(shù)據(jù)轉換成需要的格式,比如這里row.find(“#OrderDate”).text(ChangeDate(n.訂購日期));有點服務器控件做模板綁定數(shù)據(jù)的感覺。
所有的這些,都是放在一個靜態(tài)的頁面里,只通過AJAX方法從后臺獲取數(shù)據(jù),所有html代碼如下:
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>test1</title> <script language="javascript" type="text/javascript" src="js/jquery-latest.pack.js"></script> <script language="javascript" type="text/javascript" src="js/PageDate.js"></script> </head> <body> <div> <div> <br /> <input id="first" type="button" value=" << " /><input id="previous" type="button" value=" < " /><input id="next" type="button" value=" > " /><input id="last" type="button" value=" >> " /> <span id="pageinfo"></span> <table id="datas" border="1" cellspacing="0" style="border-collapse: collapse"> <tr> <th> 訂單ID</th> <th> 客戶ID</th> <th> 雇員ID</th> <th> 訂購日期</th> <th> 發(fā)貨日期</th> <th> 貨主名稱</th> <th> 貨主地址</th> <th> 貨主城市</th> <th> 更多信息</th> </tr> <tr id="template"> <td id="OrderID"> </td> <td id="CustomerID"> </td> <td id="EmployeeID"> </td> <td id="OrderDate"> </td> <td id="ShippedDate"> </td> <td id="ShippedName"> </td> <td id="ShippedAddress"> </td> <td id="ShippedCity"> </td> <td id="more"> </td> </tr> </table> </div> <div id="load" style="left: 0px; position: absolute; top: 0px; background-color: red"> LOADING.... </div> <input type="hidden" id="pagecount" /> </div> </body> </html>
PageData.js就是包括上面AJAX請求和綁定數(shù)據(jù)代碼的js,整個頁面連form都不用,這樣做有什么好處呢。再看下面一個模板
<ul id="datas"> <li id="template"> <span id="OrderID"> fsdfasdf </span> <span id="CustomerID"> </span> <span id="EmployeeID"> </span> <span id="OrderDate"> </span> <span id="ShippedDate"> </span> <span id="ShippedName"> </span> <span id="ShippedAddress"> </span> <span id="ShippedCity"> </span> <span id="more"> </span> </li> </ul>
還是要注意id屬性。大家看到這里應該明白了,不管用什么樣的表現(xiàn)形式,只要id屬性相同,就可以把數(shù)據(jù)綁定到對應的位置。這樣的話,我們這些做程序的就不會因為美工的修改而修改代碼了,而且美工也只要做出html就可以了,不需要為服務器控件做模板(不過我還沒遇到過這樣的美工,都是美工設計好了我來改成服務器控件的模板)。
再簡單說一下AJAX請求的后臺,用的是Access的Northwind數(shù)據(jù)庫,把訂單表放到DataTable里,然后通過DataTable2JSON(www.baidu.com)轉化成JSON數(shù)據(jù)格式傳回來就完了,不過后臺用了一些分頁和緩存的方法,希望對初學者有一些幫助。
test.htm
- jQuery中ajax請求后臺返回json數(shù)據(jù)并渲染HTML的方法
- JQuery的ajax獲取數(shù)據(jù)后的處理總結(html,xml,json)
- JQuery處理json與ajax返回JSON實例代碼
- jquery的ajax異步請求接收返回json數(shù)據(jù)實例
- jQuery Ajax異步處理Json數(shù)據(jù)詳解
- jQuery中使用Ajax獲取JSON格式數(shù)據(jù)示例代碼
- jquery序列化form表單使用ajax提交后處理返回的json數(shù)據(jù)
- 跨域請求之jQuery的ajax jsonp的使用解惑
- 詳談 Jquery Ajax異步處理Json數(shù)據(jù).
- jquery ajax跨域解決方法(json方式)
- jQuery+Ajax+js實現(xiàn)請求json格式數(shù)據(jù)并渲染到html頁面操作示例
相關文章
jQuery中將json數(shù)據(jù)顯示到頁面表格的方法
今天小編就為大家分享一篇jQuery中將json數(shù)據(jù)顯示到頁面表格的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05基于jQuery的Spin Button自定義文本框數(shù)值自增或自減
這個jquery 插件可以讓你的文本框增加一個自增或自減的小按鈕,方便輸入數(shù)值的控制。2010-07-07在Web項目中引入Jquery插件報錯的完美解決方案(圖解)
這篇文章主要介紹了在Web項目中引入Jquery插件報錯的完美解決方案的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09