public Pagination(IPagingContextAspect pagingContext, IPagedResultsAspect totalResults) { this.Pages = new List <Page>(); if (pagingContext == null) { return; } if (totalResults == null || totalResults.ResultsTotal <= 0) { return; } var totalNumberPages = TotalNumberOfPages(totalResults.ResultsTotal, pagingContext.PageSize.Value); SetPages(pagingContext, totalNumberPages); HasPreviousPage = pagingContext.PageNumber.Value > 1; HasNextPage = pagingContext.PageNumber.Value < totalNumberPages; PreviousPageLink = pagingContext.AsNameValueCollection() .SetKey(Constants.SearchContext.Keys.PageNumber, pagingContext.PageNumber.Value - 1) .ToQueryString(true); NextPageLink = pagingContext.AsNameValueCollection() .SetKey(Constants.SearchContext.Keys.PageNumber, pagingContext.PageNumber.Value + 1) .ToQueryString(true); }
private void SetPages(IPagingContextAspect pagingContext, int totalNumberPages) { if (totalNumberPages == 0) { return; } var currentPage = pagingContext.PageNumber.Value; var addedEmptyPage = false; // for up to 4 pages, always render all pages if (totalNumberPages <= 4) { for (var i = 1; i <= totalNumberPages; i++) { AddPage(i, pagingContext); } return; } // for >4 pages, render: // - first page // - render the current page or if neighbouring by 1 // - last page for (var i = 1; i <= totalNumberPages; i++) { if (i == 1 || i == currentPage || i == currentPage - 1 || i == currentPage + 1 || i == totalNumberPages) { AddPage(i, pagingContext); addedEmptyPage = false; } else { // add ONE empty page only (skip others) if (addedEmptyPage) { continue; } this.AddEmptyPage(); addedEmptyPage = true; } } }
private void AddPage(int pageNumber, IPagingContextAspect pagingContext) { Pages.Add(new Page(pageNumber, pagingContext.PageNumber.Value == pageNumber, pagingContext.AsNameValueCollection().SetKey(Constants.SearchContext.Keys.PageNumber, pageNumber).ToQueryString(true))); }