利用 Linq+Jquery+Ajax 实现异步分页功能

在Web显示的时候我们经常会遇到分页显示,因此产生了用Ajax来分页,利用返回的Json来处理返回的数据,无非就是异步执行ajax 把所需要的数据用json格式返回来,输出table,你可以输出ui li(输出效率高) 在页面上。

效果图:

Html代码:

  1. 设置它们的Class = "page" 以便于给它们增加Click事件操作分页  
  2. <div id="showPage" style="width: 650px; margin: 0 auto; display: none" class="pages">  
  3.     <div style="float: left">  
  4.         <a id="first" class="pages">首页</a>  
  5.         <a id="prev" class="pages">上页</a>  
  6.         <a id="next" class="pages">下页</a>  
  7.         <a id="last" class="pages">尾页</a>  
  8.         跳转到第<input type="text" id="txtGoPage" style="width: 45px; height: 15px; border: 1px solid" />  
  9.         页  
  10.     </div>  
  11.     <div style="margin: 0; float: left">  
  12.       <input type="button" class="pages btn btn-info" id="go" value="跳转" />  
  13.       共<span id="SumCount"></span> 条数据,每页<span id="ItemCount"></span> 条,  
  14.       当前<span id="Index"></span>/<span id="PageCount"></span>页  
  15.     </div>  
  16. </div>  
  17. 用下面的div输出返回的结果  
  18. <div id="divBadProductInfo"></div>  

Css代码:

  1. /*分页*/  
  2. .pages {  
  3.     cursorpointer;  
  4.     text-aligncenter;  
  5.     margin0 auto;  
  6.     padding-right0px;  
  7.     padding-bottom2px;  
  8.     padding-top2px;  
  9.     font-familyverdanahelveticaarialsans-serif;  
  10. }  
  11.   
  12. .pages a {  
  13.     border-right1px solid;  
  14.     padding-right6px;  
  15.     border-top1px solid;  
  16.     padding-left6px;  
  17.     padding-bottom0px;  
  18.     overflowhidden;  
  19.     border-left1px solid;  
  20.     line-height20px;  
  21.     margin-right2px;  
  22.     padding-top0px;  
  23.     border-bottom1px solid;  
  24.     height30px;  
  25. }  
  26.   
  27. .pages a {  
  28.     border-left-color#e6e7e1;  
  29.     border-bottom-color#e6e7e1;  
  30.     color#09c;  
  31.     border-top-color#e6e7e1;  
  32.     background-color#fff;  
  33.     border-right-color#e6e7e1;  
  34. }  
  35.   
  36. .pages a:hover {  
  37.     text-decorationnone;  
  38.     border-left-color#09c;  
  39.     border-bottom-color#09c;  
  40.     border-top-color#09c;  
  41.     border-right-color#09c;  
  42. }  
  43.   
  44. .pages a.next {  
  45.     border-left-color#09c;  
  46.     border-bottom-color#09c;  
  47.     border-top-color#09c;  
  48.     border-right-color#09c;  
  49. }  

JS代码:

引入:    <script src="assets/js/jquery-1.8.2.min.js"></script>//可以为其他版本

[javascript] view plain copy 在CODE上查看代码片 派生到我的代码片
  1.  $(document).ready(function ()   
  2. {  
  3.  //检索条件  
  4.             var search = $("#txtFactroy").val() + "_" + $("#txtTimeSelect").val()  
  5.                 + "_" + $("#txtPinfan").val() + "_" +   
  6.         $('input[type="checkbox"][name="option1"]:checked').val();  
  7.                     $.ajax({  
  8.                         type: "post",<span style="color:#ff0000;">//回传格式  
  9.                         url: "ResponseHandler.ashx"//回传到一般处理程序中处理//回传参数表示请求的是第几页,encodeURIComponent 格式化中文以防乱码  
  10.                 data: "BadProductWhere=" + encodeURIComponent(search) + "&currPage=1",  
  11.                 datatype: "json",//把返回来的数据 json  
  12.                 async: false,//禁止使用浏览器缓存   
  13.         success: function (returnData, textstatus, xmlhttprequest)   
  14.         {  
  15.             $("#showPage").css('display''block');//显示分页  
  16.              $("#divBadProductInfo").html(returnData.split('_')[0]);//返回值分割显示  
  17.             var page = returnData.split('_')[1].split(',');  
  18.             $("#SumCount").text(page[0]);//共多少条数据  
  19.             $("#ItemCount").text(page[1]);//每页多少条数据  
  20.             $("#Index").text(page[2]);//当前页  
  21.             $("#PageCount").text(page[3]);//共多少页 }  
  22.          });  
  23.         //清除转向页面  
  24.         $("#txtGoPage").val("");  
  25.   
  26.  //分页操作动作  
  27.     $(".pages").click(function () {  
  28.         //总页数大于1的情况下上下首末页可用  
  29.         if (parseFloat($("#PageCount").html()) > 1) {  
  30.             //取得控件类型是ID还是class  
  31.             var type = $(this).attr("id");  
  32.             //取得当前是多少页  
  33.             var thisindex = $("#Index").text();  
  34.             var search = $("#txtFactroy").val() + "_" + $("#txtTimeSelect").val()  
  35.                             + "_" + $("#txtPinfan").val() + "_" +  
  36.             $('input[type="checkbox"][name="option1"]:checked').val();  
  37.             switch (type) {  
  38.                 case 'first':  
  39.                     {  
  40.                         $("#txtGoPage").val("");  
  41.                         badpageindex = 1;  
  42.                         BadPageIndex(1, search);//Ajax 回传函数  
  43.                         return;  
  44.                     }  
  45.                 case 'prev':  
  46.                     {  
  47.                         $("#txtGoPage").val("");  
  48.                         badpageindex = parseInt(thisindex) - 1;  
  49.                         if (badpageindex < 1) return;  
  50.                         BadPageIndex(badpageindex, search);  
  51.                         return;  
  52.                     }  
  53.                 case 'next':  
  54.                     {  
  55.                         $("#txtGoPage").val("");  
  56.                         badpageindex = parseInt(thisindex) + 1;  
  57.                         if (badpageindex > parseInt($("#PageCount").html())) return;  
  58.                         else  
  59.                             BadPageIndex(badpageindex, search);  
  60.                         return;  
  61.                     }  
  62.                 case 'last':  
  63.                     {  
  64.                         var max = parseInt($("#PageCount").html());  
  65.                         $("#txtGoPage").val("");  
  66.                         badpageindex = max;  
  67.                         BadPageIndex(max, search);  
  68.                         return;  
  69.                     }  
  70.                 case 'go':  
  71.                     {  
  72.                         var _go = $("#txtGoPage").val();  
  73.                         badpageindex = _go;  
  74.                         BadPageIndex(_go, search);  
  75.                         return;  
  76.                     }  
  77.             }  
  78.         }  
  79.     })  
  80. });  

[javascript] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. var badpageindex;  
  2. //index,页面索引例如1,2,3  
  3. //BadProductWhere 查询条件  
  4. function BadPageIndex(index, searchwhere) {  
  5.     $.ajax({  
  6.         type: "post",  
  7.         url: "ResponseHandler.ashx",  
  8.         data: "BadProductWhere=" + encodeURIComponent(searchwhere) + "&currPage=" + index,  
  9.         datatype: "json",  
  10.         async: false,  
  11.         success: function (returnData, textstatus, xmlhttprequest) {  
  12.             $("#divDisplay").css('display''none');  
  13.             $("#showPage").css('display''block');  
  14.             $("#divBadProductInfo").html(returnData.split('_')[0]);  
  15.             var page = returnData.split('_')[1].split(',');  
  16.             $("#SumCount").text(page[0]);  
  17.             $("#ItemCount").text(page[1]);  
  18.             $("#Index").text(page[2]);  
  19.             $("#PageCount").text(page[3]);  
  20.         },  
  21.         error: function () {  
  22.             alert("服务错误");  
  23.         }  
  24.     });  
  25.   
  26. }  


C# 代码:(ResponseHandler.ashx)
[csharp] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /// <summary>  
  2.         /// 每页显示条数  
  3.         /// </summary>  
  4.         private int pageSize = 20;  
  5. StringBuilder sbBadProductInfo = new StringBuilder();  
  6.                 if (!string.IsNullOrEmpty(context.Request["BadProductWhere"]) &&  
  7.  !string.IsNullOrEmpty(context.Request["currPage"]))  
  8.                 {  
  9.                      #region // B品标题信息  
  10.                      sbBadProductInfo.Append(@"<div class='row-fluid'>  
  11.                                                 <div class='span12 widget'><div class='widget-header'>  
  12.                                                         <span class='title'>  
  13.                                                             <i class='icol-blog'></i>B品箱单信息  
  14.                                                         </span>  
  15.                                                     </div>");  
  16.                      sbBadProductInfo.Append(@"<div class='widget-content summary-list'>  
  17.                                                         <div class='row-fluid' style='padding-top: 2px;'>  
  18.                                                             <div class='span12 section'>  
  19.                                                                 <table class='table table-bordered table-striped'>  
  20.                                                                     <thead>  
  21.                                                                         <tr>  
  22.                                                                             <th>箱单编号</th>  
  23.                                                                             <th>装箱生成日期</th>  
  24.                                                                             <th>工厂名称</th>  
  25.                                                                             <th>装箱品番</th>  
  26.                                                                             <th>装箱箱号</th>  
  27.                                                                             <th>装箱件数</th>  
  28.                                                                             <th>仓库确认</th>  
  29.                                                                             <th>出库确认人</th>  
  30.                                                                             <th>出库日期</th>  
  31.                                                                             <th>查看明细</th>  
  32.                                                                         </tr>  
  33.                                                                     </thead><tbody>");  
  34.                      #endregion  
  35.                      List<bstate_view> lstGetBadProductData = (from p in lstGetBadProductData  
  36.                                              where !string.IsNullOrEmpty(p.出库确认人) && p.出库日期 != null  
  37.                                              select p).ToList<bstate_view>();  
  38.                      string pageInfo = lstGetBadProductData.Count() + "," + pageSize + "," + context.Request["currPage"] +  
  39.                         "," + (lstGetBadProductData.Count() % 20 == 0 ? (lstGetBadProductData.Count() / 20) :  
  40.  (lstGetBadProductData.Count() / 20 + 1));  
  41.                      List<bstate_view> lstGetBadItemData = (lstGetBadProductData.Count > pageSize ?   
  42. lstGetBadProductData.Skip(int.Parse(context.Request["currPage"]) == 1 ? 0 :  
  43.                                         pageSize * (int.Parse(context.Request["currPage"]) - 1)).Take(pageSize)  
  44.  : lstGetBadProductData).ToList<bstate_view>();  
  45.                      #region ==>B品箱单信息  
  46.                      foreach (var item in lstGetBadItemData)  
  47.                      {  
  48.                           var cssName = rowCount % 2 == 0 ? "warning" : "error";  
  49.                           sbBadProductInfo.Append(@"<tr class='" + cssName + "'>");  
  50.                           sbBadProductInfo.Append(@"<td>" + item.箱单编号 + "</td>");  
  51.                           sbBadProductInfo.Append(@"<td>" + item.装箱生成日期 + "</td>");  
  52.                           sbBadProductInfo.Append(@"<td>" + item.工厂名称 + "</td>");  
  53.                           sbBadProductInfo.Append(@"<td>" + item.装箱品番 + "</td>");  
  54.                           sbBadProductInfo.Append(@"<td>" + item.装箱箱号 + "</td>");  
  55.                           sbBadProductInfo.Append(@"<td>" + item.装箱件数 + "</td>");  
  56.                           sbBadProductInfo.Append(@"<td>" + item.仓库确认 + "</td>");  
  57.                           sbBadProductInfo.Append(@"<td>" + item.出库确认人 + "</td>");  
  58.                           sbBadProductInfo.Append(@"<td>" + item.出库日期 + "</td>");  
  59.                           sbBadProductInfo.Append(@"<td><input type='button' class='btn  btn-primary'   
  60. style='width: 80px; height: 30px;'  value='查看明细'");  
  61.                           sbBadProductInfo.Append(@" οnclick='OpenBadInfo(" + item.箱编号 + ");'/></td></tr>");  
  62.                           rowCount++;  
  63.                      }  
  64.                      sbBadProductInfo.Append(@"</tbody></table>  
  65.                                                             </div>  
  66.                                                         </div>  
  67.                                                     </div>  
  68.                                                 </div>  
  69.                                             </div>  
  70.                                             </div>  
  71.                                             </div>");  
  72.                      #endregion  
  73.   
  74.                      context.Response.Write(sbBadProductInfo.ToString() + "_" + pageInfo);  
  75.                      context.Response.End();  

分页效果:


其他Linq 分页资料

http://www.codesky.net/article/201004/103987.html  asp.net Linq TO Sql 分页方法

http://www.open-open.com/lib/view/open1390304270929.html  12款优秀 jQuery Ajax 分页插件和教程.

http://hi.baidu.com/hjywyj/item/8f7cc84740556a36fa8960b3 linq+Repeater实现分页功能
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值