c# - Pagination logic : Inserting ... instead of middle pages when there are lots of pages -


i trying create pagination on latest news section of site. i've managed page navigation working each page output @ bottom of screen along next , previous buttons however, want try , reduce size of pagination field when have large number of pages overall. in mind want try , mimic following behaviour:

when total number of pages less 7, output pagination as:      <previous> 1 2 3 4 5 6 7 <next>  however, if total number of pages not less 7, output first 2 pages, last 2 pages , 2 pages either side of current page link current page. in place of missing page, there should single ... 

i've managed way towards behavour using following code:

@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> } 

however, main part struggling how output single ... in place of pages not match criteria. can output multiple ... using else criteria on if condition not behaviour looking for.

any appreciated.

with slight rephrase, rules mentioned become easier understand.

the following ruleset equivalent:

any page number either      first,      second,      second before current,      first before current,      current,      first after current,      second after current,      second last,      or last page   should displayed. other page should ellipsis. 

worked out in code, becomes:

//note: i'm addressing pages 1-based index.  //if 0-based needed, add -1 index values  bool previouspageisellipsis = false;  for(int = 1; <= totalpages; i++) {     if(i == currentpage) {         //print current page number          previouspageisellipsis = false;     }     else     {         if( == 1             || == 2             || == currentpage - 2             || == currentpage - 1             || == currentpage + 1             || == currentpage + 2             || == totalpages - 1             || == totalpages)         {             //print visible link button              previouspageisellipsis = false;         }         else         {             if(previouspageisellipsis)             {                 //an ellipsis added. not add again. nothing.                 continue;             }             else             {                 //print ellipsis                 previouspageisellipsis = true;             }         }     } } 

i did not add actual code, because have already. here, see there 3 options: either show page, show ellipsis, or show nothing new if previous element ellipsis.

just insert needed html output on //comment lines , should go :)

note: made 4th option (for current page), because want render non-clickable item.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -