Lazy JSF Primefaces Datatable Pagination – Part 2
The page code is very simple and there is no complication. Check the “index.xhtml” code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<f:view>
<h:form>
<p:dataTable id="lazyDataTable" value="#{playerMB.allPlayers}" var="player" paginator="true" rows="10"
selection="#{playerMB.player}" selectionMode="single"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5,10,15" style="width: 80%;margin-left: 10%;margin-right: 10%;">
<p:ajax event="rowSelect" update=":playerDialogForm" oncomplete="playerDetails.show()" />
<p:column>
<f:facet name="header">Name</f:facet>
<h:outputText value="#{player.name}" />
</p:column>
<p:column>
<f:facet name="header">Age</f:facet>
<h:outputText value="#{player.age}" />
</p:column>
</p:dataTable>
</h:form>
<p:dialog widgetVar="playerDetails" header="Player" modal="true">
<h:form id="playerDialogForm">
<h:panelGrid columns="2">
<h:outputText value="Id: " />
<h:outputText value="#{playerMB.player.id}" />
<h:outputText value="Name: " />
<h:outputText value="#{playerMB.player.name}" />
<h:outputText value="Age: " />
<h:outputText value="#{playerMB.player.age}" />
</h:panelGrid>
</h:form>
</p:dialog>
</f:view>
</h:body>
</html>
We got a lazy datatable that will display a selected value in a dialog.
In our Managed Bean we have a simpler code than the page:
package com.mb;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.primefaces.model.LazyDataModel;
import com.model.Player;
@ViewScoped
@ManagedBean
public class PlayerMB implements Serializable {
private static final long serialVersionUID = 1L;
private LazyDataModel<Player> players = null;
private Player player;
public LazyDataModel<Player> getAllPlayers() {
if (players == null) {
players = new PlayerLazyList();
}
return players;
}
public Player getPlayer() {
if(player == null){
player = new Player();
}
return player;
}
public void setPlayer(Player player) {
this.player = player;
}
}
We got a get/set to the Player entity and a get to the an object of the LazyDataModel type.
Check bellow the implementation of the PlayerLazyList code:
package com.mb;
import java.util.List;
import java.util.Map;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import com.connection.MyTransaction;
import com.dao.PlayerDAO;
import com.model.Player;
public class PlayerLazyList extends LazyDataModel<Player> {
private static final long serialVersionUID = 1L;
private List<Player> players;
private MyTransaction transaction;
private PlayerDAO playerDAO;
@Override
public List<Player> load(int startingAt, int maxPerPage, String sortField, SortOrder sortOrder, Map<String, String> filters) {
try {
try {
transaction = MyTransaction.getNewTransaction();
playerDAO = new PlayerDAO(transaction);
transaction.begin();
// with datatable pagination limits
players = playerDAO.findPlayers(startingAt, maxPerPage);
// If there is no player created yet, we will create 100!!
if (players == null || players.isEmpty()) {
playerDAO.create100Players();
// we will do the research again to get the created players
players = playerDAO.findPlayers(startingAt, maxPerPage);
}
} finally {
transaction.commit();
}
} catch (Exception e) {
e.printStackTrace();
}
// set the total of players
if(getRowCount() <= 0){
setRowCount(playerDAO.countPlayersTotal());
}
// set the page dize
setPageSize(maxPerPage);
return players;
}
@Override
public Object getRowKey(Player player) {
return player.getId();
}
@Override
public Player getRowData(String playerId) {
Integer id = Integer.valueOf(playerId);
for (Player player : players) {
if(id.equals(player.getId())){
return player;
}
}
return null;
}
}
About the code above:
■The load method: the Primefaces will invoke this method every time that the pagination is fired. It will have all parameters with valid values; with these parameters you will be able to do a query in the database getting only for the needed data. If you want to sort your query by a field you can use the sortField attribute that will have the column datatable value (it will be null if the user do not order); the sortOrder will indicate if the user wants ascending or descending.
■The getRowKey method: this method return an id to each line, the Primefaces will invoke this method when needed.
■The getRowData method: will return a selected Player in the datatable.
■When you run this application the first time it will persist 100 players in the database. In a real application this would not be necessary.
A last configuration need to be added in the “web.xml” file:
<persistence-context-ref>
<persistence-context-ref-name>JSFPU</persistence-context-ref-name>
<persistence-unit-name>JSFPU</persistence-unit-name>
</persistence-context-ref>
We will use this configuration to do the JNDI Lookup.
Running our application
Now we just need to start up the application.
To access the application you can use the link:
http://localhost:8080/DatatableLazyPrimefaces/
from:
http://www.javacodegeeks.com/2012/04/lazy-jsf-primefaces-datatable_11.html
分享到:
相关推荐
lazyload-2.x 最新版本包括api使用
Lazy
Lazy_Theta_star是在 Theta_star上的进一步改进,Theta_star是当节点加入open表时和当前点的父节点进行比较g值是否更小,对一些不必要的节点计算浪费了时间,而Lazy_Theta_star则是在弹出open表后进行比较,减少了...
jQuery_lazyload插件示例Demo
一个组件实现lazyload图片当在视窗内(或附近)时才加载
Lazyload是通过延迟加载来实现按需加载,达到节省资源,加快浏览速度的目的。 网上也有不少类似的效果,这个Lazyload主要特点是: 支持使用window(窗口)或元素作为容器对象; 对静态(位置大小不变)元素做了大量...
Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预加载的处理方式正好是相反的. 在包含很多...
Lazy Foo' Productions - Beginning Game Programming v2.0 SDL 游戏编程教程 by Lazy Foo CHM 打包下载,包含示例代码,页面代码使用JavaScript Chill着色,看着更舒服了。 官网:lazyfoo.net/tutorials/SDL/index...
lazyload, JavaScript, 图片懒加载, h5
图片懒加载lazyload,增快页面访问速度。
Lazy Load Plugin for jQuery
macOS Mojave 10.14 18A391 Lazy Installer.cdr
lazyload技术内幕,当下最流行的图片浏览技术
很好的资源,快来下载,里面有经典的例子。让开发lazy起来吧
C# Lazy<T> 延迟优化算法对比 C# Lazy<T> 延迟优化算法对比
jquery.lazyload.min.js插件 直接下载引入即可~~~~~~~
lazy binomial heaps的oython实现,优先队列。采用双向循环链表实现,api:merge,insert,find_min,extractMin,coalesce_step,updateMin。
优化网站加载速度,使用lazyload.js,有完整的demo,有jquery.lazyload.js和jquery-1.11.0.min.js
JQuery Lazyload加载图片实例
lazyload1.9.3,图片延迟随滚动条显示。$(function() { $("img.lazyload").lazyload()});