【问题标题】:Dynamic Pagination in C#C#中的动态分页
【发布时间】:2013-01-07 19:31:33
【问题描述】:

我是新来的

我和here中的马库斯有同样的问题

我正在尝试执行的分页应该如下所示:

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

我没有加粗,而是在其中添加了一些 CSS。

我的原始代码是这样的......

if (ListCount > ListPerPage)
        {
            if (Currentpage > PageCount)
            {
                Response.Redirect(Request.Path + "/?p=" + PageCount);
            }


            html += "<ul class=\"productListPaging\">";
            for (int x = 1; x <= PageCount; x++)
            {
                if (x == Currentpage)
                {
                    html += "<li class=\"active\">";
                }
                else
                {
                    html += "<li>";
                }

                html += "<a href=\"javascript:void(0);\" onclick=\"changePage(" + x + ");\">";
                html += x;
                html += "</a>&nbsp;";
                html += "</li> &nbsp;";
            }
            html += "</ul>";
        }

此代码显示所有页面,而不是对其进行分组.. 我已经修改了上面的代码,但它只显示第一页和最后一页, 就像我有 10 页一样,它只显示第 1 页和第 10 页,而不显示 1 2 3 ... 10

任何帮助将不胜感激..

谢谢

约翰

求解算法

        int before = 2;
        int after = 2;
for (int x = 1; x <= Pagecount; x++)
{
if (x == CurrentPage)
{
    if (x == Pagecount)
    {
        html += "";
    }
    else
    {
        html += "<li class=\"active\">";
        #region Page Loop #
        html += "<a href=\"" + Url;

        html += querypage + x;
        if (sortid != "")
        {
            html += querysort;
        }
        if (viewtype != "")
        {
            html += queryviews;
        }
        if (pricing != 0)
        {
            html += queryrange;
        }

        html += "\" >";
        html += x;
        html += "</a>&nbsp;";
        #endregion
        html += "</li>";
    }
}
else if (x < CurrentPage - before)
{
    if (befli == 0)
    {
        html += "<li class=\"dotdotdot\">";
        html += "<a href=\"#\">...</a>";
        html += "</li>";
        befli++;
    }
}
else if (x > CurrentPage - before && x < CurrentPage + after)
{
    if (x == Pagecount)
    {
        html += "";
    }
    else
    {
        html += "<li>";
        #region Page Loop #
        html += "<a href=\"" + Url;

        html += querypage + x;
        if (sortid != "")
        {
            html += querysort;
        }
        if (viewtype != "")
        {
            html += queryviews;
        }
        if (pricing != 0)
        {
            html += queryrange;
        }

        html += "\" >";
        html += x;
        html += "</a>&nbsp;";
        #endregion
        html += "</li>";
    }
}
else if (x > CurrentPage + after)
{
    if (aftli == 0)
    {
        html += "<li class=\"dotdotdot\">";
        html += "<a href=\"#\">...</a>";
        html += "</li>";
        aftli++;
    }
}
else if (x == Pagecount)
{
    html += "";
}

}

只需要使用大于或小于计算for循环

好的逻辑

'

int Before = #How Many Items Before Selected Number
int After = #How Many Items After Selected Number

int PageCount = #How Many Pages
int CurrentPage = #Current Page

//First Page
if (PageCount > 1)
{
    // Here For Page Set Static 1
}
//Previous Button
if (CurrentPage != 1)
{
    //Code Here (CurrentPage - 1)
}

for loop
for(int x = 1; x < PageCount; x++)
{
    if (x == 1)
    {
        Page 1 //Static Page 1
        if (x == CurrentPage)
        {
            //Bold Font / Highlight
        }
        else
        {
            //Normal
        }
    }
    else if ( x == CurrentPage)
    {
        if(x == PageCount)
        {
            //None
        }
        else
        {
            //Bold Font / Highlight
        }
    }
    else if (x < CurrentPage - Before)
    {
        // . . .
    }
    else if (x > CurrentPage - Before && x < CurrentPage + After)
    {
        if(x == PageCount)
        {
            //None
        }
        else
        {
            //Normal Font
        }
    }
    else if (x > CurrentPage + After)
    {
        // . . .
    }
    else if (x == PageCount)
    {
        if (x == CurrentPage)
        {
            //Bold Highlight
        }
        else
        {
            //Normal 
        }
    }
}

//Next Button
if (CurrentPage != PageCount)
{
    //Code Here (CurrentPage + 1)
}

//First Page
if (PageCount > 1)
{
    // Here For Page Set Static Last Page
}

'

希望我的逻辑对需要使用 for 循环进行分页的其他用户有所帮助。

约翰

【问题讨论】:

  • 有大量的开源分页库。您可以考虑下载其中一个,看看他们是如何做到的。

标签: c# loops for-loop pagination paging


【解决方案1】:

试试这样的。您需要根据需要对其进行自定义。

    /// <summary>
  /// Builds the paging HTML.
  /// </summary>
  /// <param name="currentpage">The current selected page.</param>
  /// <param name="totalPages">The total pages for paging.</param>
  /// <param name="dotsApearanceCount">The dots apearance count. How many dots (.) you want</param>
  /// <param name="groupCount">The group count. Group that is build based on selected page</param>
  /// <returns></returns>
  public string BuildPagingHTML(int currentpage, int totalPages, int dotsApearanceCount, int groupCount)
  {
    StringBuilder sbPagingHtml = new StringBuilder();
    sbPagingHtml.Append("<ul class=\"productListPaging\">");
    // Display the first page
    sbPagingHtml.Append("<li>");
    sbPagingHtml.Append("<a href=\"javascript:void(0);\" onclick=\"changePage(" + 1 + ");\">");
    sbPagingHtml.Append(1);
    sbPagingHtml.Append("</a>&nbsp;");
    sbPagingHtml.Append("</li> &nbsp;");
    if (totalPages > 1 && currentpage - 2 >= 1)
    {
      sbPagingHtml.Append(GenerateDots(dotsApearanceCount));


      for (var linkCount = currentpage - 2; linkCount <= currentpage + 2; linkCount++)
      {
        if (linkCount >= 2 && linkCount <= totalPages - 2)
        {
          if (currentpage == linkCount)
          {
            sbPagingHtml.Append("<li class='active'>");
          }
          else
          {
            sbPagingHtml.Append("<li>");
          }

          sbPagingHtml.Append("<a href=\"javascript:void(0);\" onclick=\"changePage(" + linkCount + ");\">");
          sbPagingHtml.Append(linkCount);
          sbPagingHtml.Append("</a>&nbsp;");
          sbPagingHtml.Append("</li> &nbsp;");
        }
      }

      sbPagingHtml.Append(GenerateDots(dotsApearanceCount));

      // Display the last page
      sbPagingHtml.Append("<li>");
      sbPagingHtml.Append("<a href=\"javascript:void(0);\" onclick=\"changePage(" + totalPages + ");\">");
      sbPagingHtml.Append(totalPages);
      sbPagingHtml.Append("</a>&nbsp;");
      sbPagingHtml.Append("</li> &nbsp;");
    }

    sbPagingHtml.Append("</ul>");
    return sbPagingHtml.ToString();
  }

  /// <summary>
  /// Generates the dots.
  /// </summary>
  /// <param name="numberofDots">The numberof dots.</param>
  /// <returns></returns>
  public string GenerateDots(int numberofDots)
  {
    StringBuilder sbPagingHtml = new StringBuilder();
    for (var dotCount = 1; dotCount <= numberofDots; dotCount++)
    {
      sbPagingHtml.Append("<li>");
      sbPagingHtml.Append("<a>");
      sbPagingHtml.Append(".");
      sbPagingHtml.Append("</a>&nbsp;");
      sbPagingHtml.Append("</li> &nbsp;");
    }

    return sbPagingHtml.ToString();
  }

【讨论】:

  • 代码似乎工作正常,但是当你有很多页面要查看时..它会显示所有页面,比如 1-50 它会显示所有 1-50...我想要类似的东西会将数字与(...)算法分组...有什么想法吗?比如 1 ... 3 4 5 6 7 ... 101
  • 更新了答案。这是一个工作示例,您可能需要根据需要对其进行自定义。
猜你喜欢
  • 2016-03-08
  • 1970-01-01
  • 2015-11-02
  • 2012-11-07
  • 2017-08-20
  • 1970-01-01
  • 2012-02-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多