我该怎么编码这个分页?

时间:2009-12-18 15:17:47

标签: c# paging

我现在一直在努力寻找几天的分页代码(YES!天),但是无法让它正常工作,可能是因为我还没有任何关于此类问题的经验

我试图做的分页应该是这样的:

  

1 2 3 4 5 6 ... 101

当我点击数字5时,我希望它显示如下数字:

  

1 ... 3 4 5 6 7 ... 101

当我在最后几页时,我希望它看起来与第一页类似:

  

1 ... 96 97 98 99 100 101

粗体编号是您当前正在查看的页面。

我希望只有当有超过7页可用时才出现这些点,如果不是,它看起来应该像普通的分页一样:

  

1 2 3 4 5 6 7

现在我想在每页显示10个项目。

我想使用的语言是C#(ASP.NET),并希望稍后将其设为usercontrol(我应该设置属性TotalNumberOfItems,ItemsPerPage等)。

问题: 如何编写代码来循环出正确位置的数字? :)

3 个答案:

答案 0 :(得分:1)

怎么样(Make it Bold有点伪装,因为我不知道你在用什么用户界面...)

   private static string BuildPaging(int pageNo, int pageCount)
   {
      StringBuilder sb = new StringBuilder();

      for(int i = 1; i < pageCount; i++)
      {
         if (i == pageNo) 
             sb.Append([Make it Bold] + i.ToString("0") + [Make it not Bold]);
         else if (1 > pageNo - 3 && i < pageNo + 3)
             sb.Append(i.ToString("0"));
         else if ((i == 2 && pageNo > 4) ||        
                  (i == PageCount - 1 && pageNo < PageCount - 2))
             sb.Append("...");
      }
      return sb.ToString();
   }

唯一的问题是如何使它变得大胆(取决于你是否在WinForms或ASP.Net ...  ...并添加内容以使其成为可点击的链接...

答案 1 :(得分:0)

您需要最大的寻呼机内部列表大小(点之间的大小),页面大小,文档计数器和当前页面索引。然后你可以使用这样的算法:

_pagesTotal = DocumentsTotal / DocumentsPerPage;
if ( DocumentsTotal % DocumentsPerPage > 0 )
{
    _pagesTotal++;
}
// we want current page in the middle
// PageBlockMaxSize is the size of those dotted out pages
int halfBlock = PageBlockMaxSize / 2;
if ( CurrentPageIndex > PageBlockMaxSize )
{
    // add some code here to show first page link and following dots
    // ...    
    _firstPageInBlockIndex = CurrentPageIndex - halfBlock;
}
else
{
    // we don't need any dots here
_firstPageInBlockIndex = 1;
}

if ( _pagesTotal - CurrentPageIndex > PageBlockMaxSize )
{
    // here show last page link and preceeding dots. you can use _pagesTotal as it's text
    // ...    
    _lastPageInBlockIndex = CurrentPageIndex + halfBlock;
}
else
{
    // we don't need any dots here
    _lastPageInBlockIndex = _pagesTotal;
}

// hide next-previous buttons if they are not needed
if ( CurrentPageIndex == 1 )
{
    spanPrev.Visible = false;
}
else if ( CurrentPageIndex == _pagesTotal )
{
    spanNext.Visible = false;
}

// and when we are ready we build list of page counters
var pages = new List<int>();
for ( int page = _firstPageInBlockIndex;
      page <= _lastPageInBlockIndex;
      page++ )
{
    pages.Add( page );
}

然后我们可以将此列表数据绑定到某个转发器或以其他方式使用它来显示这些点之间的公共链接(或没有它们)。

答案 2 :(得分:0)

我做过类似的事情。它有一些与你想要的完全不同,但应该是有帮助的。这是解决问题的快速而肮脏的解决方案,并且存在许多效率问题,但这是一个良好的开端。

public class PagingHelper
{
    public IEnumerable<int> GetListOfPages(int currentPage, int pagesAroundCurrent, int totalPages)
    {
        var pages = new Dictionary<int, int>();
        double powerOfTenTotalPages = Math.Floor(Math.Log10(totalPages));
        if ((int)powerOfTenTotalPages == 0)
        {
            powerOfTenTotalPages = 1;
        }
        pages.Add(1, 1);
        if (!pages.ContainsKey(totalPages))
        {
            pages.Add(totalPages, totalPages);
        }

        for (int loop = 1; loop <= powerOfTenTotalPages + 1; loop++)
        {
            GetPages(pages, currentPage, pagesAroundCurrent, totalPages, (int)Math.Pow(10, loop - 1));
        }
        return pages.OrderBy(k=>k.Key).Select(p=>p.Key).AsEnumerable();
    }

    private void GetPages(Dictionary<int, int> pages, int currentPage, int pagesAroundCurrent, int totalPages, int jump)
    {
        int startPage = ((currentPage / jump) * jump) - (pagesAroundCurrent * jump);

        if (startPage < 0)
        {
            startPage = 0;
            pagesAroundCurrent = 10;
        }

        int endPage = currentPage + (pagesAroundCurrent * jump);
        if (endPage > totalPages)
        {
            endPage = totalPages;
        }
        AddPagesToDict(pages, startPage, endPage, jump);
    }

    private void AddPagesToDict(Dictionary<int, int> pages, int start, int end, int jump)
    {
        for (int loop = start; loop <= end; loop += jump)
        {
            if (!pages.ContainsKey(loop))
            {
                if (loop > 0)
                {
                    pages.Add(loop, loop);
                }
            }
        }
    }
}

示例输出 -

  

当前页:1总页数:40页显示当前页:5

     

1 2 3 4 5 6 7 8 9 10 11 20 30 40

     

当前页:90总页数:600页显示当前页:5

     

1 40 50 60 70 80 85 86 87 88 89 90 91 92 93 94 95 100 110 120   130 140 200 300 400 500 600

     

当前页数:147总页数:6825显示当前页面周围的页面数:5

有关详细信息,请参阅帖子here