欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于jQuery的AJAX和JSON實現(xiàn)純html數(shù)據(jù)模板

 更新時間:2016年08月09日 14:16:04   作者:加菲貓  
jQuery內置的AJAX功能,直接訪問后臺獲得JSON格式的數(shù)據(jù),從而實現(xiàn)純html數(shù)據(jù)模板,下面主要給大家介紹實現(xiàn)代碼,感興趣的朋友一起看下吧

通過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+">&nbsp;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>
&nbsp;<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=" >> " />
&nbsp;<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

這里寫圖片描述

這里寫圖片描述

相關文章

最新評論