问题
I'm new here
I have the same problem as Marcus in here
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
instead of making it bold, i have added added a few CSS in it..
my original code is something like this...
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> ";
html += "</li> ";
}
html += "</ul>";
}
this code display all the pages, not grouping it.. I have modified the code above, but it only displays the first and last Pages, Like if I have 10 Pages it only displays page 1 and 10 and not 1 2 3 ... 10
Any Help Would be appreciated..
Thanks
Johan
Solved the Algorithm
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> ";
#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> ";
#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 += "";
}
}
Just Needed to calculate the for loop using the greater or less than
Ok the Logic
'
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
}
'
Hope My Logic Helps For Other Users Who Needs Pagination using for loops.
Johan
回答1:
Try something like this. You need to customize it as per your need.
/// <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> ");
sbPagingHtml.Append("</li> ");
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> ");
sbPagingHtml.Append("</li> ");
}
}
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> ");
sbPagingHtml.Append("</li> ");
}
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> ");
sbPagingHtml.Append("</li> ");
}
return sbPagingHtml.ToString();
}
来源:https://stackoverflow.com/questions/9870486/dynamic-pagination-in-c-sharp