问题
Is there anybody who have a little example of a lazy loading with ace:dataTable?.. I can't understand how to implement the load method of the class LazyDataModel.. and how to fetch data from a Database through this method..
thanks!
回答1:
ace:dataTable has already a "built in" lazy loading mechanism in ICEFaces (at least for release 3.x and up).
No need to extend an AbstractList for this anymore.
All you need to do is add the lazy="true" to your tag, and make sure the "value" attribute points to a class that extends LazyDataModel... you just need to implement the abstract method there that accepts start page, page size, sorting & filtering arguments.
Also don't forget to use pagination and determine the size of the page ("rows" attribute).
Check: ICEFaces docs Ver. 3 ace:dataTable
回答2:
Here's working sample
myDataTableLazy.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:ace="http://www.icefaces.org/icefaces/components"
xmlns:ice="http://www.icesoft.com/icefaces/component"
xmlns:icecore="http://www.icefaces.org/icefaces/core">
<h:head>
<title>DataTable</title>
</h:head>
<h:body>
<h:form>
<ace:dataTable id="carTable"
value="#{myDataTableLazy.lazyModel}"
var="car"
rows="5"
paginator="true"
lazy="true" >
<ace:column id="exp" rendered="false">
<ace:expansionToggler />
</ace:column>
<ace:column headerText="Id">
<ice:outputText value="#{car.id}" />
</ace:column>
<ace:column headerText="Name">
<ice:outputText value="#{car.name}" />
</ace:column>
</ace:dataTable>
<h:commandButton id="invio" value="Invio" actionListener="#{myDataTableLazy.cicla}" >
</h:commandButton>
</h:form>
MyDataTableLazy
package my;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import org.icefaces.ace.model.table.LazyDataModel;
import org.icefaces.samples.showcase.example.ace.dataTable.DataTableLazyLoading;
import org.icefaces.samples.showcase.metadata.context.ComponentExampleImpl;
@ManagedBean(name="myDataTableLazy")
@SessionScoped
public class MyDataTableLazy implements Serializable {
private LazyDataModel<Car> lazyModel;
@PostConstruct
public void init() {
lazyModel = new LazyCarDataModel();
}
public LazyDataModel<Car> getLazyModel() {
return lazyModel;
}
public void setLazyModel(LazyDataModel<Car> lazyModel) {
this.lazyModel = lazyModel;
}
public void cicla(ActionEvent e) {
List<Car> lista = (List<Car>) lazyModel.getWrappedData();
for (Car car : lista) {
System.out.println( car.getName() );
}
}
}
LazyCarDataModel
package my;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.icefaces.ace.model.table.LazyDataModel;
import org.icefaces.ace.model.table.SortCriteria;
public class LazyCarDataModel extends LazyDataModel<Car> {
List<Car> carList;
public LazyCarDataModel(){
carList = new ArrayList<Car>();
carList.add(new Car(1, "FiatLazy"));
carList.add(new Car(2, "FerrariLazy"));
carList.add(new Car(3, "PorscheLazy"));
carList.add(new Car(4, "MaseratiLazy"));
carList.add(new Car(5, "MercedesLazy"));
carList.add(new Car(6, "BMWLazy"));
carList.add(new Car(7, "ToyotaLazy"));
carList.add(new Car(8, "FordLazy"));
carList.add(new Car(9, "Alfa RomeoLazy"));
carList.add(new Car(10, "SuzukiLazy"));
carList.add(new Car(11, "RenaultLazy"));
setRowCount(carList.size());
}
@Override
public List<Car> load(int first, int pageSize, SortCriteria[] arg2, Map<String, String> arg3) {
ArrayList list = new ArrayList<Car>();
int initial = first;
for (int i = initial; i < initial + pageSize && i < carList.size(); i++) {
list.add(carList.get(i));
}
return list;
}
}
回答3:
ICEFaces ice/ace:DataTable underneath works with native Java Collections. Datatable accesses elements from your collection simply by invoking get(idx) method on your collection.
I suggest you should look into implementing lazy-loading/pagination on the lower level, like implementing your own java.util.AbstractList.
Start by implementing its abstract get() method and debugging, to understand how icefaces datatable works, alternatively you can check out ice/ace:dataTable ice/ace:dataPaginator sources.
回答4:
Since IceFaces Ace is a copy/fork of PrimeFaces 2, PrimeFaces docs and samples may help. primefaces.org/showcase-labs/ui/datatableLazy.jsf
回答5:
Pheraps it's quite off-topic, but to build the showcase examples: go to http://www.icesoft.org/java/downloads/icefaces-downloads.jsf
you can select ICEfaces 4.x ICEfaces 3.x ICEfaces 1.x tab
download the ICEfaces-x.x.0-bin.zip file (you do the registration)
unzip and go to the folder you want compile, for example, in the command shell, go to ...\ICEfaces-3.3.0-bin.zip\ICEfaces-3.3.0-bin\icefaces\samples\showcase\showcase
launch the command (you must have maven): mvn package
the you'll find showcase.war in
\ICEfaces-3.3.0-bin\ICEfaces-3.3.0-bin\icefaces\samples\showcase\showcase\target
来源:https://stackoverflow.com/questions/9638544/icefaces-acedatatable-lazy-loading