When developing search engines and other applications, it is necessary to provide a flip-page navigation bar. I have read some related code on the Internet and it is very complicated. Dizzy ~~~ In fact, its mathematical formula is very simple. This article provides the two most commonly used algorithms.
Paging
The style is as follows. 10 page numbers are displayed each time, and "Top 10" and "last 10" pages are provided. [1] 2 3 4 5 6 7 8 9 10 next 10 pages last page
1 2 3 4 5 6 [7] 8 9 10 next 10 pages last page
1 2 3 4 5 6 7 8 9 [10] Next 10 pages last page
Top 10 pages on the home page [11] 12 13 14 15 16 18 19 20 bottom 10 pages last pages
Top 10 pages 11 12 13 14 15 [16] 17 18 19 20 bottom 10 pages last pages
Calculation formula: (the minimum value of the current page number is 1) int x = the current page number/10;
If (current page number % 10 = 0) -- X;
Int startpage = (x * 10) + 1;
Int endpage = math. Min (total number of pages, startpage + 9 );
Generate paging Navigation Code Demonstration: Private string getnavbarhtml (string S, int pageindex, int pagecount)
{
// Calculate the displayed page number
Int x = pageindex/10;
If (pageindex % 10 = 0) -- X;
Int startpage = (x * 10) + 1;
Int endpage = math. Min (pagecount, startpage + 9 );
// Generate the form feed code
System. Text. stringbuilder sb = new stringbuilder ();
String url = "<a href = \" search. aspx? S = {1} & page = {2} \ "> {0} </a> ";
If (startpage> 1)
{
SB. append (string. Format (URL, "Homepage", S, 1 ));
SB. append ("& nbsp ");
SB. append (string. Format (URL, "Top 10", S, startpage-1 ));
SB. append ("& nbsp ");
}
For (INT I = startpage; I <= endpage; I ++)
{
If (I! = Pageindex)
SB. append (string. Format (URL, I, S, I ));
Else
SB. append (string. Format ("[{0}]", I ));
SB. append ("& nbsp ");
}
If (pagecount> endpage)
{
SB. append (string. Format (URL, "Last 10 pages", S, endpage + 1 ));
SB. append ("& nbsp ");
SB. append (string. Format (URL, "Last page", S, pagecount ));
}
Return sb. tostring ();
}
Rolling
The style is as follows. Place the current page number in the middle and scroll through the two pages. [1] 2 3 4 5 6 7 8 9 10 last pages
1 2 3 4 [5] 6 7 8 9 10 last pages
Homepage 2 3 4 5 [6] 7 8 9 10 11 last page
Home Page 5 6 7 8 [9] 10 11 12 13 14 last page
Home Page 8 9 10 11 [12] 13 14 15 16 17 last page
Calculation formula: (the minimum value of the current page number is 1) int startpage = math. Max (the current page number-4, 1 );
Int endpage = math. Min (total number of pages, startpage + 9 );
Generate rolling Navigation Code Demonstration: Private string getnavbarhtml (string S, int pageindex, int pagecount)
{
// Calculate the displayed page number
Int startpage = math. Max (pageindex-4, 1 );
Int endpage = math. Min (pagecount, startpage + 9 );
// Generate the form feed code
System. Text. stringbuilder sb = new stringbuilder ();
String url = "<a href = \" search. aspx? S = {1} & page = {2} \ "> {0} </a> ";
If (startpage> 1)
{
SB. append (string. Format (URL, "Homepage", S, 1 ));
SB. append ("& nbsp ");
}
For (INT I = startpage; I <= endpage; I ++)
{
If (I! = Pageindex)
SB. append (string. Format (URL, I, S, I ));
Else
SB. append (string. Format ("[{0}]", I ));
SB. append ("& nbsp ");
}
If (pagecount> endpage)
{
SB. append (string. Format (URL, "Last page", S, pagecount ));
}
Return sb. tostring ();
}