我应该如何编码这个分页?

发布于 2024-08-15 03:33:51 字数 678 浏览 10 评论 0原文

我已经在一些分页代码上苦苦挣扎了几天(是的!几天),但无法让它正常工作,可能是因为我对此类问题还没有任何经验。

我想做的分页应该是这样的:

1 2 3 4 5 6 ... 101

当我点击数字 5 时,我希望它显示这样的数字:

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

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

1 ... 96 97 98 99 100 101

粗体数字是您当前正在查看的页面。

我希望仅当有超过 7 个可用页面时才显示点,如果没有,它应该看起来像正常的分页:

12 3 4 5 6 7

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

我想要使​​用的语言是 C# (ASP.NET),并且希望稍后将其设为用户控件(我应该在其中设置属性 TotalNumberOfItems、ItemsPerPage 等)。

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

I've been struggling with some code for a paging for a couple of days (YES! days) now but can't get it to work properly, probably because I don't have any experience on this kind of problems yet.

the paging I'm trying to do should look something like this:

1 2 3 4 5 6 ... 101

When i click on number 5 i would like it to display the numbers like this:

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

when I'm at the last couple of pages i want it to look similar to the first one:

1 ... 96 97 98 99 100 101

The bold number is the page that you're currently viewing.

I want the dots to appear only when there is more than 7 pages available, if not it should look like a normal paging would look like:

1 2 3 4 5 6 7

Right now i want to display 10 items per page.

The language i want to use is C# (ASP.NET) and would like to, later on make this a usercontrol (Where i should set the properties TotalNumberOfItems, ItemsPerPage and so on).

The question:
How do i write the code to loop out the numbers on the correct places? :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟凡古楼 2024-08-22 03:33:51

怎么样(让它加粗有点伪代码,因为我不知道你在什么 UI...)

   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...
...并添加内容以使其成为可点击的链接...

How about (Make it Bold is somewhat psuedoCode cause I don't know what UI you're in...)

   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();
   }

Only thing is how to make it bold (Depends on whether you're in WinForms or ASP.Net...
... And add stuff to make it a clickable link...

如果没有你 2024-08-22 03:33:51

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

_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 );
}

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

You need maximum pager inner list size (those between dots), page size, documents counter and current page index. Then you can use algorithm like this:

_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 );
}

and then we can databind this list to some repeater or use it some other way to display common links between those dots (or without them).

孤千羽 2024-08-22 03:33:51

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

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

有关完整详细信息,请参阅帖子 此处

I've done something like that. It has a few that are different from what you want exactly, but should be helpful. This was a quick and dirty solution to a problem and has a number of efficiency issues, but it's a good start.

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);
                }
            }
        }
    }
}

Sample output -

Current Page:1 Total Pages:40 Pages to display around current page:5

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

Current Page:90 Total Pages:600 Pages to display around current page: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

Current Page:147 Total Pages:6825 Pages to display around current page:5

For full details see a post here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文