public virtual PaginationModel BuildPaginationModel(Func <int, string> generateUrl) { int pageCount; if (this.pagerOptions.UseItemCountAsPageCount) { // Set page count directly from total item count instead of calculating. Then calculate totalItemCount based on pageCount and pageSize; pageCount = this.totalItemCount; this.totalItemCount = pageCount * this.pageSize; } else { pageCount = (int)Math.Ceiling(totalItemCount / (double)pageSize); } var model = new PaginationModel { PageSize = this.pageSize, CurrentPage = this.currentPage, TotalItemCount = this.totalItemCount, PageCount = pageCount }; // First page if (this.pagerOptions.DisplayFirstPage) { model.PaginationLinks.Add(new PaginationLink { Active = (currentPage > 1 ? true : false), DisplayText = this.pagerOptions.FirstPageText, DisplayTitle = this.pagerOptions.FirstPageTitle, PageIndex = 1, Url = generateUrl(1) }); } // Previous page if (!this.pagerOptions.HidePreviousAndNextPage) { var previousPageText = this.pagerOptions.PreviousPageText; model.PaginationLinks.Add(currentPage > 1 ? new PaginationLink { Active = true, DisplayText = previousPageText, DisplayTitle = this.pagerOptions.PreviousPageTitle, PageIndex = currentPage - 1, Url = generateUrl(currentPage - 1) } : new PaginationLink { Active = false, DisplayText = previousPageText }); } var start = 1; var end = pageCount; var nrOfPagesToDisplay = this.pagerOptions.MaxNrOfPages; if (pageCount > nrOfPagesToDisplay) { var middle = (int)Math.Ceiling(nrOfPagesToDisplay / 2d) - 1; var below = (currentPage - middle); var above = (currentPage + middle); if (below < 2) { above = nrOfPagesToDisplay; below = 1; } else if (above > (pageCount - 2)) { above = pageCount; below = (pageCount - nrOfPagesToDisplay + 1); } start = below; end = above; } if (!this.pagerOptions.HideFirstPageNumber && start > 1) { model.PaginationLinks.Add(new PaginationLink { Active = true, PageIndex = 1, DisplayText = "1", Url = generateUrl(1) }); if (start > 3) { model.PaginationLinks.Add(new PaginationLink { Active = true, PageIndex = 2, DisplayText = "2", Url = generateUrl(2) }); } if (start > 2) { model.PaginationLinks.Add(new PaginationLink { Active = false, DisplayText = "...", IsSpacer = true }); } } for (var i = start; i <= end; i++) { if (i == currentPage || (currentPage <= 0 && i == 1)) { model.PaginationLinks.Add(new PaginationLink { Active = true, PageIndex = i, IsCurrent = true, DisplayText = i.ToString() }); } else { model.PaginationLinks.Add(new PaginationLink { Active = true, PageIndex = i, DisplayText = i.ToString(), Url = generateUrl(i) }); } } if (!this.pagerOptions.HideLastPageNumber && end < pageCount) { if (end < pageCount) { model.PaginationLinks.Add(new PaginationLink { Active = false, DisplayText = "...", IsSpacer = true }); } if (end < pageCount - 2) { model.PaginationLinks.Add(new PaginationLink { Active = true, PageIndex = pageCount, DisplayText = (pageCount).ToString(), Url = generateUrl(pageCount) }); } } // Next page if (!this.pagerOptions.HidePreviousAndNextPage) { var nextPageText = this.pagerOptions.NextPageText; model.PaginationLinks.Add(currentPage < pageCount ? new PaginationLink { Active = true, PageIndex = currentPage + 1, DisplayText = nextPageText, DisplayTitle = this.pagerOptions.NextPageTitle, Url = generateUrl(currentPage + 1) } : new PaginationLink { Active = false, DisplayText = nextPageText }); } // Last page if (this.pagerOptions.DisplayLastPage) { model.PaginationLinks.Add(new PaginationLink { Active = (currentPage < pageCount ? true : false), DisplayText = this.pagerOptions.LastPageText, DisplayTitle = this.pagerOptions.LastPageTitle, PageIndex = pageCount, Url = generateUrl(pageCount) }); } // AjaxOptions if (pagerOptions.AjaxOptions != null) { model.AjaxOptions = pagerOptions.AjaxOptions; } model.Options = pagerOptions; return(model); }
public PaginationModel BuildPaginationModel(Func <int, string> generateUrl) { var model = new PaginationModel(); var pageCount = (int)Math.Ceiling(totalItemCount / (double)pageSize); // Previous model.PaginationLinks.Add(currentPage > 1 ? new PaginationLink { Active = true, DisplayText = "«", PageIndex = currentPage - 1, Url = generateUrl(currentPage - 1) } : new PaginationLink { Active = false, DisplayText = "«" }); var start = 1; var end = pageCount; var nrOfPagesToDisplay = this.pagerOptions.MaxNrOfPages; if (pageCount > nrOfPagesToDisplay) { var middle = (int)Math.Ceiling(nrOfPagesToDisplay / 2d) - 1; var below = (currentPage - middle); var above = (currentPage + middle); if (below < 2) { above = nrOfPagesToDisplay; below = 1; } else if (above > (pageCount - 2)) { above = pageCount; below = (pageCount - nrOfPagesToDisplay + 1); } start = below; end = above; } if (start > 1) { model.PaginationLinks.Add(new PaginationLink { Active = true, PageIndex = 1, DisplayText = "1", Url = generateUrl(1) }); if (start > 3) { model.PaginationLinks.Add(new PaginationLink { Active = true, PageIndex = 2, DisplayText = "2", Url = generateUrl(2) }); } if (start > 2) { model.PaginationLinks.Add(new PaginationLink { Active = true, DisplayText = "..." }); } } for (var i = start; i <= end; i++) { if (i == currentPage || (currentPage <= 0 && i == 0)) { model.PaginationLinks.Add(new PaginationLink { Active = true, PageIndex = i, IsCurrent = true, DisplayText = i.ToString() }); } else { model.PaginationLinks.Add(new PaginationLink { Active = true, PageIndex = i, DisplayText = i.ToString(), Url = generateUrl(i) }); } } if (end < pageCount) { if (end < pageCount - 1) { model.PaginationLinks.Add(new PaginationLink { Active = true, DisplayText = "..." }); } if (pageCount - 2 > end) { model.PaginationLinks.Add(new PaginationLink { Active = true, PageIndex = pageCount - 1, DisplayText = (pageCount - 1).ToString(), Url = generateUrl(pageCount - 1) }); } model.PaginationLinks.Add(new PaginationLink { Active = true, PageIndex = pageCount, DisplayText = pageCount.ToString(), Url = generateUrl(pageCount) }); } // Next model.PaginationLinks.Add(currentPage < pageCount ? new PaginationLink { Active = true, PageIndex = currentPage + 1, DisplayText = "»", Url = generateUrl(currentPage + 1) } : new PaginationLink { Active = false, DisplayText = "»" }); // AjaxOptions if (pagerOptions.AjaxOptions != null) { model.AjaxOptions = pagerOptions.AjaxOptions; } model.Options = pagerOptions; return(model); }
public PaginationModel BuildPaginationModel(Func <int, string> generateUrl) { var model = new PaginationModel(); var pageCount = (int)Math.Ceiling(totalItemCount / (double)pageSize); model.PaginationLinks.Add(new PaginationLink { Active = true, DisplayText = "# [ #" }); model.PaginationLinks.Add(currentPage > 1 ? new PaginationLink { Active = true, DisplayText = "第一頁", PageIndex = 1, Url = generateUrl(1) } : new PaginationLink { Active = false, DisplayText = "#第一頁#" }); model.PaginationLinks.Add(new PaginationLink { Active = true, DisplayText = "# | #" }); // Previous model.PaginationLinks.Add(currentPage > 1 ? new PaginationLink { Active = true, DisplayText = "上一頁", PageIndex = currentPage - 1, Url = generateUrl(currentPage - 1) } : new PaginationLink { Active = false, DisplayText = "#上一頁#" }); model.PaginationLinks.Add(new PaginationLink { Active = true, DisplayText = "# | #" }); var start = 1; var end = pageCount; var nrOfPagesToDisplay = this.pagerOptions.MaxNrOfPages; if (pageCount > nrOfPagesToDisplay) { var middle = (int)Math.Ceiling(nrOfPagesToDisplay / 2d) - 1; var below = (currentPage - middle); var above = (currentPage + middle); if (below < 2) { above = nrOfPagesToDisplay; below = 1; } else if (above > (pageCount - 2)) { above = pageCount; below = (pageCount - nrOfPagesToDisplay + 1); } start = below; end = above; } //if (start > 1) //{ // model.PaginationLinks.Add(new PaginationLink { Active = true, PageIndex = 1, DisplayText = "1", Url = generateUrl(1) }); // if (start > 3) // { // model.PaginationLinks.Add(new PaginationLink { Active = true, PageIndex = 2, DisplayText = "2", Url = generateUrl(2) }); // } // if (start > 2) // { // model.PaginationLinks.Add(new PaginationLink { Active = true, DisplayText = "..." }); // } //} //if (end < pageCount) //{ // if (end < pageCount - 1) // { // model.PaginationLinks.Add(new PaginationLink { Active = true, DisplayText = "..." }); // } // if (pageCount - 2 > end) // { // model.PaginationLinks.Add(new PaginationLink { Active = true, PageIndex = pageCount - 1, DisplayText = (pageCount - 1).ToString(), Url = generateUrl(pageCount - 1) }); // } // model.PaginationLinks.Add(new PaginationLink { Active = true, PageIndex = pageCount, DisplayText = pageCount.ToString(), Url = generateUrl(pageCount) }); //} // Next model.PaginationLinks.Add(currentPage < pageCount ? new PaginationLink { Active = true, PageIndex = currentPage + 1, DisplayText = "下一頁", Url = generateUrl(currentPage + 1) } : new PaginationLink { Active = false, DisplayText = "#下一頁#" }); model.PaginationLinks.Add(new PaginationLink { Active = true, DisplayText = "# | #" }); model.PaginationLinks.Add(currentPage < pageCount ? new PaginationLink { Active = true, DisplayText = "最末頁", PageIndex = pageCount, Url = generateUrl(pageCount) } : new PaginationLink { Active = false, DisplayText = "#最末頁#" }); model.PaginationLinks.Add(new PaginationLink { Active = true, DisplayText = "# ] #" }); model.PaginationLinks.Add(new PaginationLink { Active = true, DisplayText = "#目前在第#" }); model.PaginationLinks.Add(new PaginationLink { Active = true, DisplayText = "#<select id=selpage name=selpage onchange=javascript:submitform('L',this.value,0,'')>#" }); for (var i = start; i <= end; i++) { if (i == currentPage || (currentPage <= 0 && i == 0)) { model.PaginationLinks.Add(new PaginationLink { Active = true, DisplayText = "#<option value='" + i + "' selected>" + i + "</option>#" }); } else { model.PaginationLinks.Add(new PaginationLink { Active = true, DisplayText = "#<option value='" + i + "'>" + i + "</option>#" }); } } model.PaginationLinks.Add(new PaginationLink { Active = true, DisplayText = "#</select>#" }); model.PaginationLinks.Add(new PaginationLink { Active = true, DisplayText = "#頁 | 共有" + totalItemCount + " 筆 | " + pageCount + "頁#" }); //for (var i = start; i <= end; i++) //{ // if (i == currentPage || (currentPage <= 0 && i == 0)) // { // model.PaginationLinks.Add(new PaginationLink { Active = true, PageIndex = i, IsCurrent = true, DisplayText = i.ToString() }); // } // else // { // model.PaginationLinks.Add(new PaginationLink { Active = true, PageIndex = i, DisplayText = i.ToString(), Url = generateUrl(i) }); // } //} // AjaxOptions if (pagerOptions.AjaxOptions != null) { model.AjaxOptions = pagerOptions.AjaxOptions; } model.Options = pagerOptions; return(model); }