几天来,我一直在为分页的代码而挣扎(是的!)可能是因为我在这类问题上还没有任何经验。
我要做的分页应该如下所示:
1 2 3 4 5 6.
当我单击数字5时,我希望它显示如下数字:
1.3 4 5 6 7. 101
当我读到最后几页时,我希望它看起来与第一页相似:
1. 96 97 98 99 100 101
粗体数字是当前正在查看的页面。
我希望只有在有超过7页的可用页面时才会出现点,如果没有,则应该像普通的分页一样:
1 2 3 4 5 6 7
现在,我想显示每页10个项目。
我想要使用的语言是C# (ASP.NET),并希望稍后将它变成一个用户控件(在这里,我应该设置属性TotalNumberOfItems、ItemsPerPage等)。
问题:如何编写代码以循环出正确位置上的数字?)
发布于 2009-12-18 15:34:37
不如( Bold有点psuedoCode,因为我不知道你在什么UI中.)
private static string BuildPaging(int pageNo, int pageCount)
{
StringBuilder sb = new StringBuilder();
for(int i = 1; i < pageCount; i++)
{
if (i == pageNo)
sb.Append([Make it Bold] + i.ToString("0") + [Make it not Bold]);
else if (1 > pageNo - 3 && i < pageNo + 3)
sb.Append(i.ToString("0"));
else if ((i == 2 && pageNo > 4) ||
(i == PageCount - 1 && pageNo < PageCount - 2))
sb.Append("...");
}
return sb.ToString();
}唯一的事情是如何使它大胆(取决于你是在WinForms还是ASP.Net...。添加一些东西让它成为一个可点击的链接..。
发布于 2009-12-18 15:37:32
您需要最大寻呼机内部列表大小(点之间的页面大小)、页大小、文档计数器和当前页索引。然后您可以使用这样的算法:
_pagesTotal = DocumentsTotal / DocumentsPerPage;
if ( DocumentsTotal % DocumentsPerPage > 0 )
{
_pagesTotal++;
}
// we want current page in the middle
// PageBlockMaxSize is the size of those dotted out pages
int halfBlock = PageBlockMaxSize / 2;
if ( CurrentPageIndex > PageBlockMaxSize )
{
// add some code here to show first page link and following dots
// ...
_firstPageInBlockIndex = CurrentPageIndex - halfBlock;
}
else
{
// we don't need any dots here
_firstPageInBlockIndex = 1;
}
if ( _pagesTotal - CurrentPageIndex > PageBlockMaxSize )
{
// here show last page link and preceeding dots. you can use _pagesTotal as it's text
// ...
_lastPageInBlockIndex = CurrentPageIndex + halfBlock;
}
else
{
// we don't need any dots here
_lastPageInBlockIndex = _pagesTotal;
}
// hide next-previous buttons if they are not needed
if ( CurrentPageIndex == 1 )
{
spanPrev.Visible = false;
}
else if ( CurrentPageIndex == _pagesTotal )
{
spanNext.Visible = false;
}
// and when we are ready we build list of page counters
var pages = new List<int>();
for ( int page = _firstPageInBlockIndex;
page <= _lastPageInBlockIndex;
page++ )
{
pages.Add( page );
}然后,我们可以将该列表数据库到某个中继器,或者使用其他方式显示这些点之间的公共链接(或不使用它们)。
发布于 2013-10-21 21:19:21
我做过类似的事。它有一些与你想要的不一样,但应该是有帮助的。这是一个快速和肮脏的问题解决方案,并有许多效率问题,但这是一个良好的开端。
public class PagingHelper
{
public IEnumerable<int> GetListOfPages(int currentPage, int pagesAroundCurrent, int totalPages)
{
var pages = new Dictionary<int, int>();
double powerOfTenTotalPages = Math.Floor(Math.Log10(totalPages));
if ((int)powerOfTenTotalPages == 0)
{
powerOfTenTotalPages = 1;
}
pages.Add(1, 1);
if (!pages.ContainsKey(totalPages))
{
pages.Add(totalPages, totalPages);
}
for (int loop = 1; loop <= powerOfTenTotalPages + 1; loop++)
{
GetPages(pages, currentPage, pagesAroundCurrent, totalPages, (int)Math.Pow(10, loop - 1));
}
return pages.OrderBy(k=>k.Key).Select(p=>p.Key).AsEnumerable();
}
private void GetPages(Dictionary<int, int> pages, int currentPage, int pagesAroundCurrent, int totalPages, int jump)
{
int startPage = ((currentPage / jump) * jump) - (pagesAroundCurrent * jump);
if (startPage < 0)
{
startPage = 0;
pagesAroundCurrent = 10;
}
int endPage = currentPage + (pagesAroundCurrent * jump);
if (endPage > totalPages)
{
endPage = totalPages;
}
AddPagesToDict(pages, startPage, endPage, jump);
}
private void AddPagesToDict(Dictionary<int, int> pages, int start, int end, int jump)
{
for (int loop = start; loop <= end; loop += jump)
{
if (!pages.ContainsKey(loop))
{
if (loop > 0)
{
pages.Add(loop, loop);
}
}
}
}
}样本输出-
当前页:1总页数:40页在当前页面周围显示:5页 1 2 3 4 5 6 7 8 9 10 11 20 30 40 当前页:90页总数:600页在当前页面周围显示:5 1 40 50 60 70 80 85 86 87 88 89 90 91 92 93 95 100 110 120 130 140 200 300 400 500 600 当前页:147页,总页数:6825页,显示在当前页面周围:5
有关详细信息,请参阅post 这里。
https://stackoverflow.com/questions/1928809
复制相似问题