基于jQuery的AJAX和JSON實(shí)現(xiàn)純html數(shù)據(jù)模板
通過jQuery內(nèi)置的AJAX功能,直接訪問后臺(tái)獲得JSON格式的數(shù)據(jù),然后通過jQuer把數(shù)據(jù)綁定到事先設(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屬性,這個(gè)是一個(gè)關(guān)鍵。再來看一下AJAX請(qǐng)求和綁定數(shù)據(jù)的代碼。
$.ajax({
type: "get",//使用get方法訪問后臺(tái)
dataType: "json",//返回json格式的數(shù)據(jù)
url: "BackHandler.ashx",//要訪問的后臺(tái)地址
data: "pageIndex=" + pageIndex,//要發(fā)送的數(shù)據(jù)
complete :function(){$("#load").hide();},//AJAX請(qǐng)求完成時(shí)隱藏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");//添加到模板的容器中
});
這個(gè)是jQuery的AJAX方法,返回?cái)?shù)據(jù)并不復(fù)雜,主要說明一下怎么把數(shù)據(jù)按模板的定義顯示到到頁面上。首先是這個(gè)“var row = $(“#template”).clone();”先把模板復(fù)制一份,接下來row.find(“#OrderID”).text(n.訂單ID);,表示找到id=OrderID的標(biāo)記,設(shè)置它的innerText為相應(yīng)的數(shù)據(jù),當(dāng)然也可以設(shè)置為html格式的數(shù)據(jù)。或者是通過外部的函數(shù)把數(shù)據(jù)轉(zhuǎn)換成需要的格式,比如這里row.find(“#OrderDate”).text(ChangeDate(n.訂購日期));有點(diǎn)服務(wù)器控件做模板綁定數(shù)據(jù)的感覺。
所有的這些,都是放在一個(gè)靜態(tài)的頁面里,只通過AJAX方法從后臺(tái)獲取數(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請(qǐng)求和綁定數(shù)據(jù)代碼的js,整個(gè)頁面連form都不用,這樣做有什么好處呢。再看下面一個(gè)模板
<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屬性。大家看到這里應(yīng)該明白了,不管用什么樣的表現(xiàn)形式,只要id屬性相同,就可以把數(shù)據(jù)綁定到對(duì)應(yīng)的位置。這樣的話,我們這些做程序的就不會(huì)因?yàn)槊拦さ男薷亩薷拇a了,而且美工也只要做出html就可以了,不需要為服務(wù)器控件做模板(不過我還沒遇到過這樣的美工,都是美工設(shè)計(jì)好了我來改成服務(wù)器控件的模板)。
再簡單說一下AJAX請(qǐng)求的后臺(tái),用的是Access的Northwind數(shù)據(jù)庫,把訂單表放到DataTable里,然后通過DataTable2JSON(www.baidu.com)轉(zhuǎn)化成JSON數(shù)據(jù)格式傳回來就完了,不過后臺(tái)用了一些分頁和緩存的方法,希望對(duì)初學(xué)者有一些幫助。
test.htm


- jQuery中ajax請(qǐng)求后臺(tái)返回json數(shù)據(jù)并渲染HTML的方法
- JQuery的ajax獲取數(shù)據(jù)后的處理總結(jié)(html,xml,json)
- JQuery處理json與ajax返回JSON實(shí)例代碼
- jquery的ajax異步請(qǐng)求接收返回json數(shù)據(jù)實(shí)例
- jQuery Ajax異步處理Json數(shù)據(jù)詳解
- jQuery中使用Ajax獲取JSON格式數(shù)據(jù)示例代碼
- jquery序列化form表單使用ajax提交后處理返回的json數(shù)據(jù)
- 跨域請(qǐng)求之jQuery的ajax jsonp的使用解惑
- 詳談 Jquery Ajax異步處理Json數(shù)據(jù).
- jquery ajax跨域解決方法(json方式)
- jQuery+Ajax+js實(shí)現(xiàn)請(qǐng)求json格式數(shù)據(jù)并渲染到html頁面操作示例
相關(guān)文章
jQuery中將json數(shù)據(jù)顯示到頁面表格的方法
今天小編就為大家分享一篇jQuery中將json數(shù)據(jù)顯示到頁面表格的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05
基于jQuery的Spin Button自定義文本框數(shù)值自增或自減
這個(gè)jquery 插件可以讓你的文本框增加一個(gè)自增或自減的小按鈕,方便輸入數(shù)值的控制。2010-07-07
一個(gè)超簡單的jQuery回調(diào)函數(shù)例子(分享)
下面小編就為大家?guī)硪黄粋€(gè)超簡單的jQuery回調(diào)函數(shù)例子(分享) 。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08
基于ajax及jQuery實(shí)現(xiàn)局部刷新過程解析
這篇文章主要介紹了基于ajax及jQurey實(shí)現(xiàn)局部刷新過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
animate 實(shí)現(xiàn)滑動(dòng)切換效果【實(shí)例代碼】
下面小編就為大家?guī)硪黄猘nimate 實(shí)現(xiàn)滑動(dòng)切換效果【實(shí)例代碼】。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05
Eclipse引入jquery報(bào)錯(cuò)如何解決
有朋友問我,在是使用eclipse的過程中遇到了這么一個(gè)問題--eclipse導(dǎo)入jquery包后報(bào)錯(cuò)——究竟是什么原因?qū)е逻@一問題發(fā)生的呢?該如何解決此問題呢?下面小編給大家?guī)砹私鉀Q辦法,不妨一起看看吧2015-12-12
在Web項(xiàng)目中引入Jquery插件報(bào)錯(cuò)的完美解決方案(圖解)
這篇文章主要介紹了在Web項(xiàng)目中引入Jquery插件報(bào)錯(cuò)的完美解決方案的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
jQuery plugin animsition使用小結(jié)
本文通過實(shí)例代碼給大家分享了jQuery plugin animsition用法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-09-09

