jQuery分页原理

源码下载地址:

分页插件jquery.simplepager.js:

  1. // JScript 文件
  2. $.fn.mypagination = function(totalProperty,opts){   
  3.     opts = $.extend({   
  4.         perPage:10,   
  5.     
  6.         callback:function(){   
  7.              //$("#feeds").load("htmlpage?page="+page);
  8.         }   
  9.     },opts||{});   
  10.            
  11.     return this.each(function(){   
  12.         function numPages(){   
  13.             return Math.ceil(totalProperty/opts.perPage);   
  14.         }   
  15.     
  16.            
  17.         function selectPage(page){   
  18.             return function(){   
  19.                 currPage = page;   
  20.                 if (page<0) currPage = 0;   
  21.                 if (page>=numPages()) currPage = numPages()-1;   
  22.                 render();   
  23.     
  24.                 $('img.page-wait',panel).attr('src','images/wait.gif');   
  25.                 opts.callback(currPage+1);   
  26.                 $('img.page-wait',panel).attr('src','images/nowait.gif');   
  27.             }   
  28.         }   
  29.            
  30.         function render(){   
  31.     
  32.             var html = '<table><tbody><tr>'    
  33.                 +'<td><a href="#"><img class="page-first"></a></td>'  
  34.                 +'<td><a href="#"><img class="page-prev"></a></td>'  
  35.                 +'<td><span>第<input type="text" class="page-num">页/共'+numPages()+'页</span></td>'  
  36.                 +'<td><a href="#"><img class="page-next"></a></td>'  
  37.                 +'<td><a href="#"><img class="page-last"></a></td>'  
  38.                 +'<td><img src="images/nowait.gif" class="page-wait"></td>'  
  39.                 +'<td><span style="padding-left:50px;">检索到'+totalProperty+'记录</span></td>'  
  40.                 +'</tr></tbody></table>';   
  41.             var imgFirst = 'images/page-first-disabled.gif';   
  42.             var imgPrev = 'images/page-prev-disabled.gif';   
  43.             var imgNext = 'images/page-next-disabled.gif';   
  44.             var imgLast = 'images/page-last-disabled.gif';   
  45.             if (currPage > 0){   
  46.                 imgFirst = 'images/page-first.gif';   
  47.                 imgPrev = 'images/page-prev.gif';   
  48.             }   
  49.             if (currPage < numPages()-1){   
  50.                 imgNext = 'images/page-next.gif';   
  51.                 imgLast = 'images/page-last.gif';   
  52.             }   
  53.             panel.empty();   
  54.             panel.append(html);   
  55.             $('img.page-first',panel)   
  56.                 .bind('click',selectPage(0))   
  57.                 .attr('src',imgFirst);     
  58.             $('img.page-prev',panel)   
  59.                 .bind('click',selectPage(currPage-1))   
  60.                 .attr('src',imgPrev);      
  61.             $('img.page-next',panel)   
  62.                 .bind('click',selectPage(currPage+1))   
  63.                 .attr('src',imgNext);      
  64.             $('img.page-last',panel)   
  65.                 .bind('click',selectPage(numPages()-1))   
  66.                 .attr('src',imgLast);   
  67.             $('input.page-num',panel)   
  68.                 .val(currPage+1)   
  69.                 .change(function(){   
  70.                     selectPage($(this).val()-1)();   
  71.                 });   
  72.         }   
  73.            
  74.         var currPage = 0;   
  75.         var panel = $(this);   
  76.         render();   
  77.     
  78.     });   

使用分页的页面:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="jqPager_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5.     <title>jQuery分页原理(Ajax)</title>
  6.     <script type="text/javascript" src="../js/jquery-1.2.6.js"></script>
  7.     <script type="text/javascript" src="../js/jquery.simplepager.js"></script>
  8.     <script type="text/javascript">
  9.         $(function(){   
  10.             $('#mypage').mypagination(10112,{   
  11.                 callback:function(page){   
  12.                     //alert(page);  
  13.                     //$("#feeds").html("hello world");
  14.                     $("#feeds").load("PageTest.aspx?page="+page); 
  15.                 }   
  16.             });  
  17.             $("#feeds").load("PageTest.aspx?page=1"); 
  18.         }); 
  19.         
  20.     </script>
  21. </head>
  22. <body>
  23.     <form id="form1" runat="server">
  24.         <div>
  25.             <div id="mypage" class="mypagination">
  26.             </div>
  27.             <div id="feeds" style="width: 500px; height: 500px; background: #369;">
  28.             </div>
  29.         </div>
  30.     </form>
  31. </body>
  32. </html>

iframe里调用的页面:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageTest.aspx.cs" Inherits="jqPager_PageTest" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5.     <title>根据地址传来的参数显示某一页</title>
  6. </head>
  7. <body>
  8.     <form id="form1" runat="server">
  9.     <div>
  10.         <asp:Label ID="lblPager" runat="server" Text="第0页"></asp:Label>
  11.     </div>
  12.     </form>
  13. </body>
  14. </html>

后台:

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. public partial class jqPager_PageTest : System.Web.UI.Page
  12. {
  13.     protected void Page_Load(object sender, EventArgs e)
  14.     {
  15.         if (Request.QueryString["page"] != null)
  16.             lblPager.Text = "第" + Request.QueryString["page"].ToString() + "页";
  17.         else
  18.             lblPager.Text = "第0页";
  19.     }
  20. }

end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值