zoukankan      html  css  js  c++  java
  • 数字分页类

    //分页通用类

    public class Pager

    {

      private int pageIndex;

      public int PageIndex

      {

      get; set;

      }

    private int currentPageCount;

    public int CurrentPageCount

    {

      get;set;

    }

    protected string sbHtml = "";

    if(currentPageCount <= 1)//如果就一夜,不显示分页

    {

      return;

    }

    int start = currentPage -5;//表示起始位置。。。一页有十条

    if(start <=1)

    {

      start = 1;

    }

      int end = start +9;

    if(end >currentPageCount)

    {

      end = currentPageCount;

    start = end -9;

    }

    StringBuilder sb = new StringBuilder();

    if(currentPage > 1)

    {

    sb.Append(string.Format("<a href = ?page={0}>&nbsp;上一页&nbsp;</a>",currentPage-1))

    }

    for(int i = start;i<= end;i++)

    {

      if(i == currentPage)//等于当前页

    {

      sb.Append("&nbsp" + i + "&nbsp");

    }

    else

    {

      sb.Append(string.Format("<a href=?page={0}>&nbsp" + i + "nbsp;</a>));

    }

    if(currentPage <currentCount)

    {

    sb.Append(string.Format("<a href = ?page={0}>&nbsp;下一页&nbsp;</a>",currentPage+1))

    }

    sbHtml = sb.ToString();

    }

    }

    //分页通用类2

    public static string GetPageHTML(int recordTotalCount, int pageIndex,int perSize)
    {
    if (recordTotalCount <= perSize)
    {
    return "";
    }
    StringBuilder strBuilder = new StringBuilder();
    string attr = "";
    int pagecount = 0;//当前页面的总层数
    int floorcount = 0;//分页的总层数
    int currentLastPage = 0;//当前最后一页的页码,用来保存最后一页的号码
    //总页数
    // int pageTotalCount = recordTotalCount / 10 + 1;
    int pageTotalCount = recordTotalCount % perSize == 0 ? recordTotalCount / perSize : recordTotalCount / perSize + 1;
    // strBuilder.Append("<div class="badoo">");
    attr = pageIndex == 1 ? "visible="" + "false"" : ""; //标志当前页第一页是否相等 来控制前俩个按钮的有效性
    strBuilder.AppendFormat(GetAHtml(attr, "首页", pageIndex, 0));//上一页
    if (attr != "visible="false"")
    {
    strBuilder.AppendFormat(GetAHtml(attr, "上一页",pageIndex-1,0));
    }
    pagecount = pageIndex / perSize;//当前页数 0~1~2
    pagecount = pageIndex % perSize == 0 ? pagecount - 1 : pagecount;//清除当 当前页数为分页页码数的整数倍页时除数多一的状况
    floorcount = pageTotalCount / perSize;//页面层数 0~1~2
    currentLastPage = pageTotalCount < perSize * (pagecount + 1) ? pageTotalCount : perSize * (pagecount + 1);
    //currentLastPage = recordTotalCount % 10 == 0 ? recordTotalCount / 10 : recordTotalCount / 10 + 1;

    if (pageIndex > perSize) //如果大于10条则显示...
    {
    strBuilder.AppendFormat(GetAHtml("", "1", 1, 0)); //设置超链接
    strBuilder.AppendFormat(GetAHtml("", "2", 2, 0)); //设置超链接
    strBuilder.AppendFormat(GetAHtml("", "3", 3, 0)); //设置超链接
    strBuilder.AppendFormat(GetAHtml("", "...", perSize * pagecount, 0));
    }
    for (int i = perSize * pagecount + 1; i <= currentLastPage; i++)
    {
    if (i == pageIndex)
    {
    strBuilder.AppendFormat(GetAHtml("", i.ToString(), i,1));//GetSpanHtml(i, "current")
    }
    else
    {
    strBuilder.AppendFormat(GetAHtml("", i.ToString(),i,0)); //设置超链接
    }
    }
    if (pageIndex <= perSize * floorcount) //当当前序号小于倒数第二页页码时显示在后端...
    {
    strBuilder.AppendFormat(GetAHtml("", "", perSize * (pagecount + 1) + 1, 0));
    }

    attr = pageIndex == pageTotalCount ? "visible="" + "false"" : "";//标志当前页最后一页是否相等 来控制后俩个按钮的有效性

    if (attr != "visible="false"")
    {
    strBuilder.AppendFormat(GetAHtml(attr, "下一页",(pageIndex+1),0)); //下一页
    }



    strBuilder.AppendFormat(GetAHtml(attr, "末页",pageTotalCount,0));//末页

    //strBuilder.AppendFormat("</div>");

    return strBuilder.ToString();
    }

    /// <summary>
    /// get the html of a label
    /// </summary>
    /// <param name="title">a's title</param>
    /// <param name="url">the url of a</param>
    /// <param name="attr">the attribute</param>
    /// <returns>return html string</returns>
    private static string GetAHtml(string attr, string title,int index,int isCurrent)
    {
    string str = "";
    // string temp = index.ToString() + "^";
    switch (title)
    {
    case "首页":
    str = "<a id = "first" " + attr + " href = 'javascript:void(0)' style="margin-right:5px;" onclick = "GetDataByPage(" + index + ")">首页</a> ";
    break;
    case "末页":
    str = "<a id = "end" " + attr + " href = 'javascript:void(0)' style="margin-right:5px;" onclick = "GetDataByPage(" + index + ")">末页</a> ";
    break;
    case "上一页":
    str = "<a id ="prev" " + attr + " href = 'javascript:void(0)' style="margin-right:5px;" onclick = "GetDataByPage(" + index + ")">上一页</a> ";
    break;
    case "下一页":
    str = "<a id = "next" " + attr + " href = 'javascript:void(0)' style="margin-right:5px;" onclick = "GetDataByPage(" + index + ")">下一页</a> ";
    break;
    default:
    if (isCurrent == 1)
    {
    str = "<a id = "page"+index+"" " + attr + " class = "current" href = 'javascript:void(0)' style="margin-right:5px;" onclick = "GetDataByPage(" + index + ")">" + index + "</a> ";
    }
    else
    {
    str = "<a id = "page" + index + "" " + attr + " href = 'javascript:void(0)' style="margin-right:5px;" onclick = "GetDataByPage(" + index + ")">" + index + "</a> ";
    }
    break;
    }
    return str;
    }

    //这个方法的目的是固定住当前用户点击的页索引
    private static string GetSpanHtml(int num, string className)
    {
    return "<span class="" + className + "">" + num + "</span> ";
    }

    /*别人写的分页类*/

    ///<summary>
    2 /// 生成分页标签
    3 ///</summary>
    4  publicclass Pager
    5 {
    6 privateint pageIndex;
    7 ///<summary>
    8 /// 当前请求的页码
    9 ///</summary>
    10  publicint PageIndex
    11 {
    12 get { return pageIndex; }
    13 privateset { pageIndex = value; }
    14 }
    15
    16 privateint pageSize;
    17 ///<summary>
    18 /// 请求的当前页的大小
    19 ///</summary>
    20 publicint PageSize
    21 {
    22 get { return pageSize; }
    23 privateset { pageSize = value; }
    24 }
    25
    26 privateint rowCount;
    27 ///<summary>
    28 /// 数据总量,可以根据这个判断出能够显示多少页
    29 ///</summary>
    30 publicint RowCount
    31 {
    32 get { return rowCount; }
    33 privateset { rowCount = value; }
    34 }
    35
    36 privatestring hrefFormat;
    37 ///<summary>
    38 /// 页码连接所要指向的url格式化字符串例如:aticleList.aspx?{0}&delegate=wenxue
    39 ///</summary>
    40 publicstring HrefFormat
    41 {
    42 get { return hrefFormat; }
    43 set { hrefFormat = value; }
    44 }
    45 privateint pageCount;
    46
    47 private Pager() { }
    48
    49 public Pager(int pageIndex, int pageSize, int rowCount)
    50 {
    51 //计算出总共有多少页
    52 this.pageCount = (int)Math.Ceiling(rowCount / (double)pageSize);
    53 //如果pageIndex<1 就让当前页码等于1,如果pageIndex大于最大页码.
    54 this.PageIndex = GetPageIndex(pageIndex, pageSize, rowCount);
    55
    56 this.PageSize = pageSize;
    57 }
    58
    59 publicstring CreatePager()
    60 {
    61 StringBuilder sb =new StringBuilder();
    62 sb.AppendLine("<div class='pager'>");
    63 if (this.PageIndex >1)
    64 {
    65 sb.AppendLine(" <a id='pre' href='"+string.Format(this.HrefFormat, "page="+ GetPageIndex(this.pageIndex -1, this.PageSize, this.RowCount)) +"'>"+"上一页"+"</a>");
    66 }
    67 else
    68 {
    69 sb.AppendLine(" <a id='pre' class='false'>"+"上一页"+"</a>");
    70 }
    71 CreatePageIndex(sb);
    72 if (this.pageIndex <this.pageCount)
    73 {
    74 sb.AppendLine(" <a id='next' href='"+string.Format(this.HrefFormat, "page="+ GetPageIndex(this.pageIndex +1, this.PageSize, this.RowCount)) +"'>"+"下一页"+"</a>");
    75 }
    76
    77 sb.AppendLine("</div>");
    78 return sb.ToString();
    79 }
    80
    81 privatevoid CreatePageIndex(StringBuilder sb)
    82 {
    83 if (this.pageIndex <=7)
    84 {
    85 int i =1;
    86 for (; i <= (this.pageIndex >this.pageCount ?10 : this.pageCount); i++)
    87 {
    88 if (i ==this.pageIndex)
    89 {
    90 sb.AppendLine(" <a href='"+string.Format(HrefFormat, "page="+ i) +"' class='Active'>"+ i +"</a>");
    91 continue;
    92 }
    93 sb.AppendLine(" <a href='"+string.Format(HrefFormat, "page="+ i) +"'>"+ i +"</a>");
    94 }
    95 if (i <this.pageCount)
    96 {
    97 sb.AppendLine("...");
    98 sb.AppendLine(" <a href='"+string.Format(HrefFormat, "page="+this.pageCount +"") +"'>"+this.pageCount +"</a>");
    99
    100 }
    101 }
    102 elseif (this.PageIndex >12&&this.pageCount >this.pageIndex +5)
    103 {
    104 sb.AppendLine(" <a href='"+string.Format(HrefFormat, "page=1") +"'>"+1+"</a>");
    105 sb.AppendLine(" <a href='"+string.Format(HrefFormat, "page=2") +"'>"+2+"</a>");
    106 sb.AppendLine(" <a href='"+string.Format(HrefFormat, "page=3") +"'>"+3+"</a>");
    107 sb.AppendLine("...");
    108 for (int i =this.pageIndex -4; i <= (this.pageIndex +4>this.pageCount ?this.pageCount : this.pageIndex +4); i++)
    109 {
    110 if (i ==this.pageIndex)
    111 {
    112 sb.AppendLine(" <a href='"+string.Format(HrefFormat, "page="+ i) +"' class='Active'>"+ i +"</a>");
    113 continue;
    114 }
    115 sb.AppendLine(" <a href='"+string.Format(HrefFormat, "page="+ i) +"'>"+ i +"</a>");
    116 }
    117 sb.AppendLine("...");
    118 for (int j =this.pageCount -2; j <=this.pageCount; j++)
    119 {
    120 sb.AppendLine(" <a href='"+string.Format(HrefFormat, "page="+ j) +"'>"+ j +"</a>");
    121 }
    122 }
    123 elseif (this.pageIndex >7)
    124 {
    125
    126 int i =this.pageIndex -4;
    127 if (i >2)
    128 {
    129 sb.AppendLine(" <a href='"+string.Format(HrefFormat, "page=1") +"'>"+1+"</a>");
    130 sb.AppendLine("...");
    131 }
    132 for (; i <= (this.pageIndex +4>this.pageCount ?this.pageCount : this.pageIndex +4); i++)
    133 {
    134 if (i ==this.pageIndex)
    135 {
    136 sb.AppendLine(" <a href='"+string.Format(HrefFormat, "page="+ i) +"' class='Active'>"+ i +"</a>");
    137 continue;
    138 }
    139 sb.AppendLine(" <a href='"+string.Format(HrefFormat, "page="+ i) +"'>"+ i +"</a>");
    140
    141 }
    142 if (i <this.pageCount)
    143 {
    144 sb.AppendLine("...");
    145 sb.AppendLine(" <a href='"+string.Format(HrefFormat, "page="+this.pageCount +"") +"'>"+this.pageCount +"</a>");
    146 }
    147 }
    148 }
    149
    150 privateint GetPageIndex(int pageIndex, int pageSize, int rowCount)
    151 {
    152 int pageCount = (int)Math.Ceiling(rowCount / (double)pageSize);
    153 if (pageIndex <1)
    154 {
    155 return1;
    156 }
    157 elseif (pageIndex >this.pageCount)//就让他等于最大页码
    158 {
    159 return pageCount;
    160 }
    161 else
    162 {
    163 return pageIndex;
    164 }
    165 }
    166 }
    复制代码
    css:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    div.pager a
    {
        display:block;
        float:left;
        margin-left:3px;
        text-align:center;
        text-decoration:none;
        border:1px solid Gray;
        color:Gray;
        26px;
    }
    #pre
    {
        80px;
    }
    #pre.false:hover
    {
        border:1px solid Gray;
        color:Gray;
        background-Color:White;
    }
    #next
    {
        80px;
    }
    div.pager a:hover,div.pager a.Active
    {
        border:1px solid black;
        background-Color:#4b6c9e;
        color:#f9f9f9;
    }
     
  • 相关阅读:
    创建百度地图实例
    IntelliJ IDEA使用(1)——IDEA配置JDK
    IntelliJ IDEA使用(2)——IDEA配置Tomcat
    怎样使用 GitHub?
    IntelliJ IDEA使用(3)——IDEA连接Git
    AS3.0杂记——Dictionary、Object与Array
    AIR文件操作:使用文件对象操作文件和目录 .
    As3.0 TextField
    关于乘机
    Html 嵌入 swf
  • 原文地址:https://www.cnblogs.com/taomylife/p/3290513.html
Copyright © 2011-2022 走看看