基于jQuery的AJAX和JSON实现纯html数据模板

通过jQuery内置的AJAX功能,直接访问后台获得JSON格式的数据,然后通过jQuer把数据绑定到事先设计好的html模板上,直接在页面上显示。

我们先来看一下html模板:
基于jQuery的AJAX和JSON实现纯html数据模板<tableid="datas"border="1"cellspacing="0"style="border-collapse:collapse">
基于jQuery的AJAX和JSON实现纯html数据模板
<tr>
基于jQuery的AJAX和JSON实现纯html数据模板
<th>
基于jQuery的AJAX和JSON实现纯html数据模板订单ID
</th>
基于jQuery的AJAX和JSON实现纯html数据模板
<th>
基于jQuery的AJAX和JSON实现纯html数据模板客户ID
</th>
基于jQuery的AJAX和JSON实现纯html数据模板
<th>
基于jQuery的AJAX和JSON实现纯html数据模板雇员ID
</th>
基于jQuery的AJAX和JSON实现纯html数据模板
<th>
基于jQuery的AJAX和JSON实现纯html数据模板订购日期
</th>
基于jQuery的AJAX和JSON实现纯html数据模板
<th>
基于jQuery的AJAX和JSON实现纯html数据模板发货日期
</th>
基于jQuery的AJAX和JSON实现纯html数据模板
<th>
基于jQuery的AJAX和JSON实现纯html数据模板货主名称
</th>
基于jQuery的AJAX和JSON实现纯html数据模板
<th>
基于jQuery的AJAX和JSON实现纯html数据模板货主地址
</th>
基于jQuery的AJAX和JSON实现纯html数据模板
<th>
基于jQuery的AJAX和JSON实现纯html数据模板货主城市
</th>
基于jQuery的AJAX和JSON实现纯html数据模板
<th>
基于jQuery的AJAX和JSON实现纯html数据模板更多信息
</th>
基于jQuery的AJAX和JSON实现纯html数据模板
</tr>
基于jQuery的AJAX和JSON实现纯html数据模板
<trid="template">
基于jQuery的AJAX和JSON实现纯html数据模板
<tdid="OrderID">
基于jQuery的AJAX和JSON实现纯html数据模板
</td>
基于jQuery的AJAX和JSON实现纯html数据模板
<tdid="CustomerID">
基于jQuery的AJAX和JSON实现纯html数据模板
</td>
基于jQuery的AJAX和JSON实现纯html数据模板
<tdid="EmployeeID">
基于jQuery的AJAX和JSON实现纯html数据模板
</td>
基于jQuery的AJAX和JSON实现纯html数据模板
<tdid="OrderDate">
基于jQuery的AJAX和JSON实现纯html数据模板
</td>
基于jQuery的AJAX和JSON实现纯html数据模板
<tdid="ShippedDate">
基于jQuery的AJAX和JSON实现纯html数据模板
</td>
基于jQuery的AJAX和JSON实现纯html数据模板
<tdid="ShippedName">
基于jQuery的AJAX和JSON实现纯html数据模板
</td>
基于jQuery的AJAX和JSON实现纯html数据模板
<tdid="ShippedAddress">
基于jQuery的AJAX和JSON实现纯html数据模板
</td>
基于jQuery的AJAX和JSON实现纯html数据模板
<tdid="ShippedCity">
基于jQuery的AJAX和JSON实现纯html数据模板
</td>
基于jQuery的AJAX和JSON实现纯html数据模板
<tdid="more">
基于jQuery的AJAX和JSON实现纯html数据模板
</td>
基于jQuery的AJAX和JSON实现纯html数据模板
</tr>
基于jQuery的AJAX和JSON实现纯html数据模板
</table>
基于jQuery的AJAX和JSON实现纯html数据模板
一定要注意的就是里面所有的id属性,这个是一个关键。再来看一下AJAX请求和绑定数据的代码。
基于jQuery的AJAX和JSON实现纯html数据模板$.ajax({
基于jQuery的AJAX和JSON实现纯html数据模板type:
"get",//使用get方法访问后台
基于jQuery的AJAX和JSON实现纯html数据模板
dataType:"json",//返回json格式的数据
基于jQuery的AJAX和JSON实现纯html数据模板
url:"BackHandler.ashx",//要访问的后台地址
基于jQuery的AJAX和JSON实现纯html数据模板
data:"pageIndex="+pageIndex,//要发送的数据
基于jQuery的AJAX和JSON实现纯html数据模板
complete:function(){$("#load").hide();},//AJAX请求完成时隐藏loading提示
基于jQuery的AJAX和JSON实现纯html数据模板
success:function(msg){//msg为返回的数据,在这里做数据绑定
基于jQuery的AJAX和JSON实现纯html数据模板
vardata=msg.table;
基于jQuery的AJAX和JSON实现纯html数据模板$.each(data,
function(i,n){
基于jQuery的AJAX和JSON实现纯html数据模板
varrow=$("#template").clone();
基于jQuery的AJAX和JSON实现纯html数据模板row.find(
"#OrderID").text(n.订单ID);
基于jQuery的AJAX和JSON实现纯html数据模板row.find(
"#CustomerID").text(n.客户ID);
基于jQuery的AJAX和JSON实现纯html数据模板row.find(
"#EmployeeID").text(n.雇员ID);
基于jQuery的AJAX和JSON实现纯html数据模板row.find(
"#OrderDate").text(ChangeDate(n.订购日期));
基于jQuery的AJAX和JSON实现纯html数据模板
if(n.发货日期!==undefined)row.find("#ShippedDate").text(ChangeDate(n.发货日期));
基于jQuery的AJAX和JSON实现纯html数据模板row.find(
"#ShippedName").text(n.货主名称);
基于jQuery的AJAX和JSON实现纯html数据模板row.find(
"#ShippedAddress").text(n.货主地址);
基于jQuery的AJAX和JSON实现纯html数据模板row.find(
"#ShippedCity").text(n.货主城市);
基于jQuery的AJAX和JSON实现纯html数据模板row.find(
"#more").html("<ahref=OrderInfo.aspx?id="+n.订单ID+"&pageindex="+pageIndex+">&nbsp;More</a>");
基于jQuery的AJAX和JSON实现纯html数据模板row.attr(
"id","ready");//改变绑定好数据的行的id
基于jQuery的AJAX和JSON实现纯html数据模板
row.appendTo("#datas");//添加到模板的容器中
基于jQuery的AJAX和JSON实现纯html数据模板
}
);
基于jQuery的AJAX和JSON实现纯html数据模板
这个是jQuery的AJAX方法,返回数据并不复杂,主要说明一下怎么把数据按模板的定义显示到到页面上。首先是这个“varrow = $("#template").clone();”先把模板复制一份,接下来row.find("#OrderID").text(n.订单ID);,表示找到id=OrderID的标记,设置它的innerText为相应的数据,当然也可以设置为html格式的数据。或者是通过外部的函数把数据转换成需要的格式,比如这里row.find("#OrderDate").text(ChangeDate(n.订购日期));有点服务器控件做模板绑定数据的感觉。
所有的这些,都是放在一个静态的页面里,只通过AJAX方法从后台获取数据,所有html代码如下:
基于jQuery的AJAX和JSON实现纯html数据模板<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
基于jQuery的AJAX和JSON实现纯html数据模板
<htmlxmlns="http://www.w3.org/1999/xhtml">
基于jQuery的AJAX和JSON实现纯html数据模板
<head>
基于jQuery的AJAX和JSON实现纯html数据模板
<title>test1</title>
基于jQuery的AJAX和JSON实现纯html数据模板
基于jQuery的AJAX和JSON实现纯html数据模板
<scriptlanguage="javascript"type="text/javascript"src="js/jquery-latest.pack.js"></script>
基于jQuery的AJAX和JSON实现纯html数据模板
基于jQuery的AJAX和JSON实现纯html数据模板
<scriptlanguage="javascript"type="text/javascript"src="js/PageDate.js"></script>
基于jQuery的AJAX和JSON实现纯html数据模板
基于jQuery的AJAX和JSON实现纯html数据模板
</head>
基于jQuery的AJAX和JSON实现纯html数据模板
<body>
基于jQuery的AJAX和JSON实现纯html数据模板
<div>
基于jQuery的AJAX和JSON实现纯html数据模板
&nbsp;<div>
基于jQuery的AJAX和JSON实现纯html数据模板
<br/>
基于jQuery的AJAX和JSON实现纯html数据模板
<inputid="first"type="button"value="<<"/><inputid="previous"type="button"
基于jQuery的AJAX和JSON实现纯html数据模板value
="<"/><inputid="next"type="button"value=">"/><inputid="last"type="button"
基于jQuery的AJAX和JSON实现纯html数据模板value
=">>"/>
基于jQuery的AJAX和JSON实现纯html数据模板
&nbsp;<spanid="pageinfo"></span>
基于jQuery的AJAX和JSON实现纯html数据模板
<tableid="datas"border="1"cellspacing="0"style="border-collapse:collapse">
基于jQuery的AJAX和JSON实现纯html数据模板
<tr>
基于jQuery的AJAX和JSON实现纯html数据模板
<th>
基于jQuery的AJAX和JSON实现纯html数据模板订单ID
</th>
基于jQuery的AJAX和JSON实现纯html数据模板
<th>
基于jQuery的AJAX和JSON实现纯html数据模板客户ID
</th>
基于jQuery的AJAX和JSON实现纯html数据模板
<th>
基于jQuery的AJAX和JSON实现纯html数据模板雇员ID
</th>
基于jQuery的AJAX和JSON实现纯html数据模板
<th>
基于jQuery的AJAX和JSON实现纯html数据模板订购日期
</th>
基于jQuery的AJAX和JSON实现纯html数据模板
<th>
基于jQuery的AJAX和JSON实现纯html数据模板发货日期
</th>
基于jQuery的AJAX和JSON实现纯html数据模板
<th>
基于jQuery的AJAX和JSON实现纯html数据模板货主名称
</th>
基于jQuery的AJAX和JSON实现纯html数据模板
<th>
基于jQuery的AJAX和JSON实现纯html数据模板货主地址
</th>
基于jQuery的AJAX和JSON实现纯html数据模板
<th>
基于jQuery的AJAX和JSON实现纯html数据模板货主城市
</th>
基于jQuery的AJAX和JSON实现纯html数据模板
<th>
基于jQuery的AJAX和JSON实现纯html数据模板更多信息
</th>
基于jQuery的AJAX和JSON实现纯html数据模板
</tr>
基于jQuery的AJAX和JSON实现纯html数据模板
<trid="template">
基于jQuery的AJAX和JSON实现纯html数据模板
<tdid="OrderID">
基于jQuery的AJAX和JSON实现纯html数据模板
</td>
基于jQuery的AJAX和JSON实现纯html数据模板
<tdid="CustomerID">
基于jQuery的AJAX和JSON实现纯html数据模板
</td>
基于jQuery的AJAX和JSON实现纯html数据模板
<tdid="EmployeeID">
基于jQuery的AJAX和JSON实现纯html数据模板
</td>
基于jQuery的AJAX和JSON实现纯html数据模板
<tdid="OrderDate">
基于jQuery的AJAX和JSON实现纯html数据模板
</td>
基于jQuery的AJAX和JSON实现纯html数据模板
<tdid="ShippedDate">
基于jQuery的AJAX和JSON实现纯html数据模板
</td>
基于jQuery的AJAX和JSON实现纯html数据模板
<tdid="ShippedName">
基于jQuery的AJAX和JSON实现纯html数据模板
</td>
基于jQuery的AJAX和JSON实现纯html数据模板
<tdid="ShippedAddress">
基于jQuery的AJAX和JSON实现纯html数据模板
</td>
基于jQuery的AJAX和JSON实现纯html数据模板
<tdid="ShippedCity">
基于jQuery的AJAX和JSON实现纯html数据模板
</td>
基于jQuery的AJAX和JSON实现纯html数据模板
<tdid="more">
基于jQuery的AJAX和JSON实现纯html数据模板
</td>
基于jQuery的AJAX和JSON实现纯html数据模板
</tr>
基于jQuery的AJAX和JSON实现纯html数据模板
</table>
基于jQuery的AJAX和JSON实现纯html数据模板
</div>
基于jQuery的AJAX和JSON实现纯html数据模板
<divid="load"style="left:0px;position:absolute;top:0px;background-color:red">
基于jQuery的AJAX和JSON实现纯html数据模板LOADING....
基于jQuery的AJAX和JSON实现纯html数据模板
</div>
基于jQuery的AJAX和JSON实现纯html数据模板
<inputtype="hidden"id="pagecount"/>
基于jQuery的AJAX和JSON实现纯html数据模板
</div>
基于jQuery的AJAX和JSON实现纯html数据模板
</body>
基于jQuery的AJAX和JSON实现纯html数据模板
</html>
基于jQuery的AJAX和JSON实现纯html数据模板
PageData.js就是包括上面AJAX请求和绑定数据代码的js,整个页面连form都不用,这样做有什么好处呢。再看下面一个模板
基于jQuery的AJAX和JSON实现纯html数据模板<ulid="datas">
基于jQuery的AJAX和JSON实现纯html数据模板
<liid="template">
基于jQuery的AJAX和JSON实现纯html数据模板
<spanid="OrderID">
基于jQuery的AJAX和JSON实现纯html数据模板fsdfasdf
基于jQuery的AJAX和JSON实现纯html数据模板
</span>
基于jQuery的AJAX和JSON实现纯html数据模板
<spanid="CustomerID">
基于jQuery的AJAX和JSON实现纯html数据模板
</span>
基于jQuery的AJAX和JSON实现纯html数据模板
<spanid="EmployeeID">
基于jQuery的AJAX和JSON实现纯html数据模板
</span>
基于jQuery的AJAX和JSON实现纯html数据模板
<spanid="OrderDate">
基于jQuery的AJAX和JSON实现纯html数据模板
</span>
基于jQuery的AJAX和JSON实现纯html数据模板
<spanid="ShippedDate">
基于jQuery的AJAX和JSON实现纯html数据模板
</span>
基于jQuery的AJAX和JSON实现纯html数据模板
<spanid="ShippedName">
基于jQuery的AJAX和JSON实现纯html数据模板
</span>
基于jQuery的AJAX和JSON实现纯html数据模板
<spanid="ShippedAddress">
基于jQuery的AJAX和JSON实现纯html数据模板
</span>
基于jQuery的AJAX和JSON实现纯html数据模板
<spanid="ShippedCity">
基于jQuery的AJAX和JSON实现纯html数据模板
</span>
基于jQuery的AJAX和JSON实现纯html数据模板
<spanid="more">
基于jQuery的AJAX和JSON实现纯html数据模板
</span>
基于jQuery的AJAX和JSON实现纯html数据模板
</li>
基于jQuery的AJAX和JSON实现纯html数据模板
</ul>
基于jQuery的AJAX和JSON实现纯html数据模板
还是要注意id属性。大家看到这里应该明 白了,不管用什么样的表现形式,只要id属性相同,就可以把数据绑定到对应的位置。这样的话,我们这些做程序的就不会因为美工的修改而修改代码了,而且美 工也只要做出html就可以了,不需要为服务器控件做模板(不过我还没遇到过这样的美工,都是美工设计好了我来改成服务器控件的模板)。
再简单说一下AJAX请求的后台,用的是Access的Northwind数据库,把订单表放到DataTable里,然后通过DataTable2JSON(http://blog.****.net/luq885/archive/2007/06/05/1639853.aspx)转化成JSON数据格式传回来就完了,不过后台用了一些分页和缓存的方法,希望对初学者有一些帮助。
全部例子到http://www1.51ok.com/down.do?7A5EFE8D0FD43CDF61D8980635F8708A下载,其中test.htm是用table模板实现的,test2.htm是用ul模板实现的。效果如下:
test.htm
基于jQuery的AJAX和JSON实现纯html数据模板
test2.htm
基于jQuery的AJAX和JSON实现纯html数据模板