【问题标题】:Pagination logic : Inserting ... instead of middle pages when there are lots of pages分页逻辑:当页面很多时插入 ... 而不是中间页面
【发布时间】:2014-06-08 05:18:53
【问题描述】:

我正在尝试在我网站的最新新闻部分创建一些分页。我已经设法让页面导航真正工作,以便每个页面与下一个和上一个按钮一起输出到屏幕底部但是,当我们有大量时,我也想尝试减小分页字段的大小总页数。考虑到这一点,我想尝试模仿以下行为:

When the total number of pages is less than 7, output the pagination as:

    <Previous> 1 2 3 4 5 6 7 <Next>

However, if the total number of pages is not less than 7, output only the first 2
pages, the last 2 pages and the 2 pages either side of the current page as well
as the link for the current page. In place of the missing page, there should be
a single ...

我已经设法使用以下代码来实现这种行为:

@if (totalPages > 1){
  <ul class="pager">
    @if (page > 1){
      <li><a href="?page=@(page-1)">Previous</a></li>
    }
    @for (int p = 1; p < totalPages + 1; p++){
      var linkClass = (p == page) ? "disabled" : "active";
      if ((p >= page - 2 && p <= page + 2 || p <= 2 || p >= totalPages -2) 
          || totalPages <= 7){
            <li class="@Html.Raw(linkClass)">
              <a href="?page=@p">@p</a>
            </li>
      }          
    }

    @if (page < totalPages){
        <li><a href="?page=@(page+1)">Next</a></li>
    }
  </ul>
}

但是,我现在苦苦挣扎的主要部分是如何输出单个 ... 来代替不符合条件的页面。我可以轻松地输出多个 ... 在 if 条件上使用 else 标准,但这不是我正在寻找的行为。

任何帮助将不胜感激。

【问题讨论】:

    标签: c# .net razor pagination umbraco


    【解决方案1】:

    稍微改写一下,你提到的规则就更容易理解了。

    以下规则集是等价的:

    Any page number that is either the 
        first, 
        second, 
        second before current, 
        first before current, 
        current, 
        first after current, 
        second after current, 
        second to last, 
        or last page  
    should be displayed. Any other page should be an ellipsis.
    

    用代码解决,变成:

    //Note: I'm addressing the pages as a 1-based index. 
    //If 0-based is needed, just add -1 to all index values
    
    bool previousPageIsEllipsis = false;
    
    for(int i = 1; i <= totalpages; i++)
    {
        if(i == currentpage) {
            //Print current page number
    
            previousPageIsEllipsis = false;
        }
        else
        {
            if( i == 1
                || i == 2
                || i == currentpage - 2
                || i == currentpage - 1
                || i == currentpage + 1
                || i == currentpage + 2
                || i == totalpages - 1
                || i == totalpages)
            {
                //Print a visible link button
    
                previousPageIsEllipsis = false;
            }
            else
            {
                if(previousPageIsEllipsis)
                {
                    //an ellipsis was already added. Do not add it again. Do nothing.
                    continue;
                }
                else
                {
                    //Print an ellipsis
                    previousPageIsEllipsis = true;
                }
            }
        }
    }
    

    我没有添加实际代码,因为您已经有了。但是在这里,您会看到三个选项:显示页面,显示省略号,或者如果前一个元素已经是省略号,则不显示任何新内容。

    只需在//comment lines 上插入所需的 HTML 输出就可以了 :)

    注意:我做了第 4 个选项(针对当前页面),因为您通常希望将其呈现为不可点击的项目。

    【讨论】:

    • 非常感谢。这是一个很好的解决方案。唯一的问题是这忽略了省略号仅应在页面数量超过一定数量时出现的事实。
    • 这很容易添加,尽管在第一个 else 条件下添加一个额外的规则:||总页数
    • 我认为这条规则在您的原始问题中略有错误,因为您似乎总是会显示 7 个页码,其余的应该是(折叠的)省略号。所以是的,如果在总计数低于或等于 7 时不需要省略号,那么您也需要检查 :)
    • 我明白了。好吧,在我最初的问题中,我确实提到了总页数,它指的是总共可用的页数,但现在已经排序。感谢您的帮助。
    猜你喜欢
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-01
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多