您的位置:首页 > Web前端 > JQuery

基于jQuery的AJAX请求Table显示

2014-03-26 11:33 417 查看
一:通过jQuery内置的AJAX功能,直接访问后台获得JSON格式的数据,然后通过jQuer把数据绑定到事先设计好的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>


                        发货日期</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>

二:再来看一下AJAX请求和绑定数据的代码
      $.ajax({


            type: "get",//使用get方法访问后台


            dataType: "json",//返回json格式的数据


            url: "BackHandler.ashx",//要访问的后台地址


            data: "pageIndex=" + pageIndex,//要发送的数据




            complete :function(){$("#load").hide();},//AJAX请求完成时隐藏loading提示




            success: function(msg){//msg为返回的数据,在这里做数据绑定


                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.发货日期!== undefined) row.find("#ShippedDate").text(ChangeDate(n.发货日期));


                    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");//改变绑定好数据的行的id


                    row.appendTo("#datas");//添加到模板的容器中


                });

 

三:这个是jQuery的AJAX方法,返回数据并不复杂,主要说明一下怎么把数据按模板的定义显示到到页面上。首先是这个“var row = $("#template").clone();”先把模板复制一份,接下来row.find("#OrderID").text(n.订单ID);,表示找到id=OrderID的标记,设置它的innerText为相应的数据,当然也可以设置为html格式的数据。或者是通过外部的函数把数据转换成需要的格式,比如这里row.find("#OrderDate").text(ChangeDate(n.订购日期));有点服务器控件做模板绑定数据的感觉。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: