【发布时间】:2014-06-08 05:18:53
【问题描述】:
我正在尝试在我网站的最新新闻部分创建一些分页。我已经设法让页面导航真正工作,以便每个页面与下一个和上一个按钮一起输出到屏幕底部但是,当我们有大量时,我也想尝试减小分页字段的大小总页数。考虑到这一点,我想尝试模仿以下行为:
When the total number of pages is less than 7, output the pagination as:
<Previous> 1 2 3 4 5 6 7 <Next>
However, if the total number of pages is not less than 7, output only the first 2
pages, the last 2 pages and the 2 pages either side of the current page as well
as the link for the current page. In place of the missing page, there should be
a single ...
我已经设法使用以下代码来实现这种行为:
@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>
}
但是,我现在苦苦挣扎的主要部分是如何输出单个 ... 来代替不符合条件的页面。我可以轻松地输出多个 ... 在 if 条件上使用 else 标准,但这不是我正在寻找的行为。
任何帮助将不胜感激。
【问题讨论】:
标签: c# .net razor pagination umbraco