pus*_*hya 7 java pagination velocity
任何人都可以提供一些想法/逻辑来为我正在研究的搜索页面编写分页逻辑吗?我所拥有的信息是该搜索的总页数 - 每页10条记录我也被发送了前一页和下一页的编号(没有问题写我需要做的逻辑我拉这些信息并填充.我是也获取我所在页面的信息.我只能显示10页,如下所示
<previous 1 |2 |3 | 4| 5 | 6 | 7 | 8 | 9 | 10 next>
Run Code Online (Sandbox Code Playgroud)
假设总页数为15,当用户点击下一步时,我需要显示如下
<previous 2 |3 |4 |5 |6 |7 |8 |9 |10 |11 next>
Run Code Online (Sandbox Code Playgroud)
在任何时候我只需要在分页中显示10页.
#set($start = 1)
#set($end = $Integer.parseInt($searchTO.getPagination().getNumberofPages()))
#set($range = [$start..$end])
#set($iter = 1)
#foreach($i in $range)
#foreach($link in $searchTO.getPagination().getDirectPageLinks())
#if($i == $iter)
#if ($Integer.parseInt($searchTO.getPagination().getPageNumber())==$iter)
<a class="search_current" href="/?_page=SEARCH&_action=SEARCH$link">$i  |</a>
#else
<a href="/?_page=SEARCH&_action=SEARCH$link">$i  |</a>
#end
#set($iter = 1)
#break
#else
#set($iter=$iter+1)
#end
#end
#end
Run Code Online (Sandbox Code Playgroud)
Att*_*cus 10
以下是我将如何实现它:通常最好创建一个Filter类来过滤数据并为您包含与分页相关的信息.我使用这样的东西:
public abstract class Filter{
/** Member identifier for the current page number */
private int currentPageNo;
/** Member identifier for the current start page number in the page navigation */
private int currentStartPageNo;
/** Member identifier for the current end page number in the page navigation */
private int currentEndPageNo;
/** Member identifier for the number of elements on a page */
private int elementsPerPage;
/** Member identifier for the number of pages you have in the navigation (i.e 2 to 11 or 3 to 12 etc.) */
private int pageNumberInNavigation;
public abstract Query createCountQuery();
public abstract Query createQuery();
public void setCurrentPageNo(){
//Your code here
//Validation, variable setup
}
public Long getAllElementsCount(){
//Now this depends on the presistence framework you use, this code is
//just for guidance and has Hibernate-like syntax
Query query = createCountQuery();
List list = query.list();
return !list.isEmpty() && list.get(0) != null ? query.list().get(0) : 0;
}
public List getElements(){
//Now this depends on the presistence framework you use, this code is
//just for guidance and has Hibernate-like syntax
Query query = createQuery();
int from = ((currentPageNo - 1) * elementsPerPage);
query.setFirstResult(from);
query.setMaxResults(elementsPerPage);
//If you use Hibernate, you don't need to worry for null check since if there are no results then an empty collection is returned
return query.list();
}
public List getAllElements(){
Query query = createQuery();
return query.list();
}
public void refresh(){
//Your code here
}
public List next(){
//Move to the next page if exists
setCurrentPageNo(getCurrentPageNo() + 1);
getElements();
}
public List previoius(){
//Move to the previous page if exists
setCurrentPageNo(getCurrentPageNo() - 1);
getElements();
}
}
Run Code Online (Sandbox Code Playgroud)
你可以有特殊的过滤子类(取决于你想要检索的内容),并且每个子类都会实现它的createCountQuery()
和createQuery()
.
然后,您可以将您Filter
置于Velocity
上下文中,然后您可以从此类中检索所需的所有信息.
当您设置当前页面时,您将更新所需的所有其他信息(即currentStartPageNo,currentEndPageNo).
您还可以使用一种refresh()
方法将过滤器恢复到初始状态.
当然,当用户在Filter
所属的搜索页面上导航时,您应该在会话中保留相同过滤器的实例(我的意思是您的Web框架,如Struts,Turbine等).
这只是一个指导方针,一个想法,它不是完全编写的可执行代码,只是一个让你开始朝着一个方向前进的例子.
我还建议你一个名为jqGrid的jQuery插件,它有分页支持(虽然你必须有一个支持检索数据)和更多很酷的东西.您可以使用它在网格中显示数据.我一起使用它Velocity
没有问题.它有很多非常好的和有用的功能,如过滤器工具栏,可编辑单元格,在数据传输JSON
,XML
等等.老实说,我不知道是否有分页像你需要(我用它导航,你只显示一个页面不能单击页面只需使用下一个上一个按钮进行导航),但它可能有支持.
归档时间: |
|
查看次数: |
58209 次 |
最近记录: |