类似土豆的C#分页算法

新童鞋可以拿来做参考哈,老童鞋请自己写

====================================================

 

// 请保留作者版权
// Author:warner
// ZHSoft.inc
using System;
using System.Collections.Generic;
using System.Text;

namespace ZHSoft.Components
{
    public class Units
    {

        public static string[] GetPages(long count, int pageSize, int pageNo)
        {
            string[] data = null;
            int pageCount = count > pageSize ? (int)(count % pageSize > 0 ? count / pageSize + 1 : count / pageSize): 1;
           
            int n = 0;
            #region 小于10页的处理方式
            if (pageCount <= 10)
            {
                data = new string[pageCount];
                for (int i = 0; i < pageCount; i++)
                {
                    data[i] = Convert.ToString(i + 1);
                    n++;
                }
            }
            #endregion
            else
            {
                //大于等于10页的处理方式
                #region 当前页在第1页和第9页之间
                if (pageNo >= 0 && pageNo < 9)
                {
                    #region 如果总页面大于12页情况,则省略中间项只显示1-10页和最后2页
                    if (pageCount > 12)
                    {
                        data = new string[12];
                        for (int i = 0; i < pageCount; i++)
                        {
                            if (i <= 8 && n < data.Length)
                            {
                                data[n] = Convert.ToString(i + 1);
                                n++;
                            }
                            else
                            {
                                if (i <= pageCount - 3)
                                {
                                    if (n == 9)
                                    {
                                        data[n] = "...";
                                        n++;
                                    }

                                }
                                else
                                {
                                    data[n] = Convert.ToString(i + 1);
                                    n++;
                                }
                            }
                        }
                    }
                    #endregion
                    else
                    {
                        data = new string[pageCount];
                        for (int i = 0; i < pageCount; i++)
                        {
                            data[n] = Convert.ToString(i + 1);
                            n++;
                        }
                    }

                }
                #endregion

                //当前页在第10页和第pageCount-8页之间
                else if (pageNo >= 9 && pageNo <= pageCount - 7)
                {
                    if (pageCount <= 13)
                    {
                        data = new string[pageCount];
                        for (int i = 0; i < pageCount; i++)
                        {
                            data[i] = Convert.ToString(i + 1);
                        }
                    }
                    else
                    {
                        data = new string[11];
                        for (int i = 0; i < pageCount; i++)
                        {
                            if ((i == 0 || i == 1 || i == pageCount - 1 || i == pageCount - 2) && n < data.Length)
                            {
                                data[n] = Convert.ToString(i + 1);
                                n++;
                            }
                            else if ((i == 2 || i == pageCount - 3) && n < data.Length)
                            {
                                data[n] = "...";
                                n++;
                            }
                            else if ((i > 3 && i < pageCount - 3) && n < data.Length)
                            {
                                // 在页数大于2 and 页数小于 总数 - 3
                                if (i > pageNo - 4 && i < pageNo + 2)
                                {
                                    data[n] = Convert.ToString(i + 1);
                                    n++;
                                }
                            }
                        }
                    }
                }
                //当前页在第pageCount-8到pageCount页之间
                else
                {
                    if (pageCount <= 13)
                    {
                        data = new string[pageCount];
                        for (int i = 0; i < pageCount; i++)
                        {
                            data[n] = Convert.ToString(i + 1);
                            n++;
                        }
                    }
                    else
                    {
                        data = new string[11];
                        for (int i = 0; i < pageCount; i++)
                        {
                            if (i == 0 || i == 1 || (i >= pageCount - 8 && i <= pageCount - 1))
                            {
                                if (n < 11)
                                {
                                    data[n] = Convert.ToString(i + 1);
                                    n++;
                                }
                            }
                            else if (i == 2)
                            {
                                data[n] = "...";
                                n++;
                            }
                            else { }
                        }
                    }
                }
            }
            return data;
        }

        /// <summary>
        /// 获取分页数据源
        /// </summary>
        /// <param name="count">返回的查询记录总数</param>
        /// <param name="pageSize">页面大小</param>
        /// <param name="pageIndex">当前页面</param>
        /// <returns></returns>
        public static string[] GetPages(int count, int pageSize, int pageIndex)
        {
            string[] data = null;
            int pageCount = count > pageSize ? (int)(count % pageSize > 0 ? count / pageSize + 1 : count / pageSize) : 1;

            int n = 0;

            #region 小于10页的处理方式
            if (pageCount <= 10)
            {
                data = new string[pageCount];
                for (int i = 0; i < pageCount; i++)
                {
                    data[i] = Convert.ToString(i + 1);
                    n++;
                }
            }
            #endregion
            else
            {
                //大于等于10页的处理方式
                #region 当前页在第1页和第9页之间
                if (pageIndex >= 0 && pageIndex < 9)
                {
                    #region 如果总页面大于12页情况,则省略中间项只显示1-10页和最后2页
                    if (pageCount > 12)
                    {
                        data = new string[12];
                        for (int i = 0; i < pageCount; i++)
                        {
                            if (i <= 8 && n < data.Length)
                            {
                                data[n] = Convert.ToString(i + 1);
                                n++;
                            }
                            else
                            {
                                if (i <= pageCount - 3)
                                {
                                    if (n == 9)
                                    {
                                        data[n] = "...";
                                        n++;
                                    }

                                }
                                else
                                {
                                    data[n] = Convert.ToString(i + 1);
                                    n++;
                                }
                            }
                        }
                    }
                    #endregion
                    else
                    {
                        data = new string[pageCount];
                        for (int i = 0; i < pageCount; i++)
                        {
                            data[n] = Convert.ToString(i + 1);
                            n++;
                        }
                    }

                }
                #endregion

                //当前页在第10页和第pageCount-8页之间
                else if (pageIndex >= 9 && pageIndex <= pageCount - 7)
                {
                    if (pageCount <= 13)
                    {
                        data = new string[pageCount];
                        for (int i = 0; i < pageCount; i++)
                        {
                            data[i] = Convert.ToString(i + 1);
                        }
                    }
                    else
                    {
                        data = new string[11];
                        for (int i = 0; i < pageCount; i++)
                        {
                            if ((i == 0 || i == 1 || i == pageCount - 1 || i == pageCount - 2) && n < data.Length)
                            {
                                data[n] = Convert.ToString(i + 1);
                                n++;
                            }
                            else if ((i == 2 || i == pageCount - 3) && n < data.Length)
                            {
                                data[n] = "...";
                                n++;
                            }
                            else if ((i > 3 && i < pageCount - 3) && n < data.Length)
                            {
                                // 在页数大于2 and 页数小于 总数 - 3
                                if (i > pageIndex - 4 && i < pageIndex + 2)
                                {
                                    data[n] = Convert.ToString(i + 1);
                                    n++;
                                }
                            }
                        }
                    }
                }
                //当前页在第pageCount-8到pageCount页之间
                else
                {
                    if (pageCount <= 13)
                    {
                        data = new string[pageCount];
                        for (int i = 0; i < pageCount; i++)
                        {
                            data[n] = Convert.ToString(i + 1);
                            n++;
                        }
                    }
                    else
                    {
                        data = new string[11];
                        for (int i = 0; i < pageCount; i++)
                        {
                            if (i == 0 || i == 1 || (i >= pageCount - 8 && i <= pageCount - 1))
                            {
                                if (n < 11)
                                {
                                    data[n] = Convert.ToString(i + 1);
                                    n++;
                                }
                            }
                            else if (i == 2)
                            {
                                data[n] = "...";
                                n++;
                            }
                            else { }
                        }
                    }
                }
            }
            return data;
        }


    }
}

 

posted on 2011-05-11 14:16  Rhina 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/Boyona/archive/2011/05/11/2043192.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值