显示页码

发布于 2024-11-16 12:57:51 字数 214 浏览 2 评论 0原文

我想模仿此分页的工作原理:

在此处输入图像描述

请注意当前页面如何始终显示两个页面两边?当您考虑到您可能在第 4 页并且 1 和 3 之间没有间隙,或者如果您在第 1 页时,它会在右侧显示两个以上的数字,那么这似乎会是很多条件代码。

有人能让我有一个正确的开始吗?

I'd like to mimic how this paging works:

enter image description here

Notice how the current page will always show two pages on either side? This seems like it would be a lot of conditional code when you factor in the case that you may be on page 4 and there is no gap between 1 and 3 or if you are on page 1 it will show more than two numbers to the right.

Can somebody get me off to the right start?

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

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

发布评论

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

评论(3

云柯 2024-11-23 12:57:51

以下是控制台程序的示例输出,其中包含您要查找的逻辑:

Program.exe 1

1 2 3...100

Program.exe 2

1 2 3 4...100

Program.exe 5

1...3 4 5 6 7。 ..100

using System;

class Program
{
    static void Main(string[] args)
    {
        // usage program.exe page#
        // page# between 1 and 100
        int minPage = 1;
        int maxPage = 100;
        int currentPage = int.Parse(args[0]);

        // output nice pagination
        // always have a group of 5

        int minRange = Math.Max(minPage, currentPage-2);
        int maxRange = Math.Min(maxPage, currentPage+2);

        if (minRange != minPage)
        {
            Console.Write(minPage);
            Console.Write("...");
        }

        for (int i = minRange; i <= maxRange; i++)
        {
            Console.Write(i);
            if (i != maxRange) Console.Write(" ");
        }

        if (maxRange != maxPage)
        {
            Console.Write("...");
            Console.Write(maxPage);
        }
    }
}

Here's sample output from console program with logic you are looking for:

Program.exe 1

1 2 3...100

Program.exe 2

1 2 3 4...100

Program.exe 5

1...3 4 5 6 7...100

using System;

class Program
{
    static void Main(string[] args)
    {
        // usage program.exe page#
        // page# between 1 and 100
        int minPage = 1;
        int maxPage = 100;
        int currentPage = int.Parse(args[0]);

        // output nice pagination
        // always have a group of 5

        int minRange = Math.Max(minPage, currentPage-2);
        int maxRange = Math.Min(maxPage, currentPage+2);

        if (minRange != minPage)
        {
            Console.Write(minPage);
            Console.Write("...");
        }

        for (int i = minRange; i <= maxRange; i++)
        {
            Console.Write(i);
            if (i != maxRange) Console.Write(" ");
        }

        if (maxRange != maxPage)
        {
            Console.Write("...");
            Console.Write(maxPage);
        }
    }
}
离线来电— 2024-11-23 12:57:51
int n = 34 //max page
int current = 5 //current page

if(current > 1)
     //Dislpay 'prev'

if (current < 5){
    for(int i=1; i<current; ++i)
        //Display i
}
else {
    //Display 1 followed by ...
    for(int i=current-2; i<current; ++i)
        //Display i
}

//Display current in red

if (current > n-4) {
    for(int i = current+1; i<=n; ++i)
        //Display i
}
else {
    for(int i=current+1; i<current+3; ++i)
        //Display i
    //Display ... folowed by n
}

if (current < n)
    //Display 'next'
int n = 34 //max page
int current = 5 //current page

if(current > 1)
     //Dislpay 'prev'

if (current < 5){
    for(int i=1; i<current; ++i)
        //Display i
}
else {
    //Display 1 followed by ...
    for(int i=current-2; i<current; ++i)
        //Display i
}

//Display current in red

if (current > n-4) {
    for(int i = current+1; i<=n; ++i)
        //Display i
}
else {
    for(int i=current+1; i<current+3; ++i)
        //Display i
    //Display ... folowed by n
}

if (current < n)
    //Display 'next'
半夏半凉 2024-11-23 12:57:51
  1. 你知道第一页
  2. 你知道最后一页
  3. 你知道当前页

所以

  • 如果第一页和第二页之间有超过 2 页。当前,显示“...”,然后显示最接近当前的两个页面
  • ,否则显示第一个和当前之间的所有页面,
  • 重复最后一个
  1. You know the first page
  2. You know the last page
  3. You know the current page

So

  • If there are more than 2 pages between first & current, show '...' then two pages closest to current
  • else show all pages between first and current
  • repeat for last
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文