Icefaces ace:dataTable 惰性加载

3

有没有人能给一个ace:dataTable的懒加载示例?我不太理解如何实现LazyDataModel类中的load方法,以及如何通过该方法从数据库获取数据。

谢谢!

5个回答

4
ace:dataTable已经在ICEFaces的3.x版本及以上内置了惰性加载机制,不需要再为此扩展AbstractList
您只需要将lazy="true"添加到标签中,并确保"value"属性指向一个扩展LazyDataModel的类……您只需要在那里实现接受起始页面、页面大小、排序和过滤参数的抽象方法。
还要记得使用分页功能并确定每页数据的数量("rows"属性)。
请参考:ICEFaces文档Ver. 3 ace:dataTable

感谢@Nir M的帮助,我已经查看了展示示例,但是我无法运行示例。如果可能的话,您能否为我提供一个简单的示例?谢谢。 - vinod

2
这是一个工作样例。
我的DataTableLazy.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> 

我的懒加载数据表格

  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;
    }

  }

0

也许这有点偏题,但是要构建展示示例:

请前往http://www.icesoft.org/java/downloads/icefaces-downloads.jsf

你可以选择 ICEfaces 4.x、ICEfaces 3.x 或 ICEfaces 1.x 标签页。

下载 ICEfaces-x.x.0-bin.zip 文件(需要先注册)。

解压缩并进入您要编译的文件夹。例如,在命令行中,进入 ...\ICEfaces-3.3.0-bin.zip\ICEfaces-3.3.0-bin\icefaces\samples\showcase\showcase。

输入以下命令(需要安装 maven):mvn package

然后你会在以下位置找到 showcase.war 文件。

\ICEfaces-3.3.0-bin\ICEfaces-3.3.0-bin\icefaces\samples\showcase\showcase\target


0

ICEFaces ice/ace:DataTable底层使用本地Java集合。Datatable通过在集合上调用get(idx)方法来访问元素。

我建议您应该在较低级别上实现延迟加载/分页,比如实现自己的java.util.AbstractList

首先实现它的抽象get()方法并进行调试,以了解icefaces datatable的工作原理,或者您可以查看ice/ace:dataTable ice/ace:dataPaginator源代码。


0

由于IceFaces Ace是PrimeFaces 2的复制/分支,所以可以参考PrimeFaces文档和示例。primefaces.org/showcase-labs/ui/datatableLazy.jsf


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接