2

我在本教程http://uaihebert.com/?p=1120之后实现了lazyDataModel

我的代码与本教程有点不同,这里是:

看法:

<p:dataTable id="tablaClientes"  value="#{AgendaManualMBean.allClientes}" 
   var="tablaClientes" 
   widgetVar="clienteTable" 
   rowKey="#{tablaClientes.clDocid}" 
   selection="#{AgendaManualMBean.ciatt001}" selectionMode="single"  rows="10" 
   lazy="true" paginatorPosition="top" 
   paginatorTemplate="{RowsPerPageDropdown}{FirstPageLink}{PreviousPageLink} 
   {CurrentPageReport} {NextPageLink} {LastPageLink}" rowsPerPageTemplate="5,10,15"
   emptyMessage="No existen clientes">

                <f:facet name="header" >  
                    <p:outputPanel>  
                        <h:outputText value="Busqueda " />  
                        <p:inputText id="globalFilter" onkeyup="clienteTable.filter()" 
                         style="width:150px"  size="10"/>  
                    </p:outputPanel>  
                </f:facet>  

                <p:column id="numOrdenColumn" filterBy="#{tablaClientes.clDocid}"  
                          width="50"              
                          headerText="Identificación" 
                         
                          filterMatchMode="contains">  
                    <h:outputText value="#{tablaClientes.clDocid}" />  
                </p:column>  
                <p:column id="nomCliColumn" 
                          filterBy="#{tablaClientes.clNombre1}"   
                          width="250"
                      
                          headerText="Cliente" 
                          filterMatchMode="contains">  
                    <h:outputText value="#{tablaClientes.clNombre1}" />  
                </p:column>  

            </p:dataTable> 

我的托管豆:

 public LazyDataModel<Ciatt001> getAllClientes() {
    if (listaClientesLazy == null) {
        listaClientesLazy = new LazyClienteModel(ambiente.getCodCia(),ambiente.getCodAge());
        //listaClientesLazy = new LazyClienteModelMBean();
    }

    return listaClientesLazy;
}

我的懒惰数据模型

public List<Ciatt001> load(int startingAt, int maxPerPage, String sortField, SortOrder sortOrder, Map<String, String> filters) {
    String a = "";
    try {   
    listaClientes = new ArrayList<Ciatt001>();
    a = String.valueOf(agendamientoSession.countClientes2(cia, age));
    listaClientes = agendamientoSession.listaClientes2(cia, age, startingAt, maxPerPage);
    } catch (Exception e) {
        e.printStackTrace();
    }
    
    if (getRowCount() <= 0) {
      setRowCount(Integer.parseInt(a));
     }
    setPageSize(maxPerPage);
     return listaClientes;  
    }

@Override
public Object getRowKey(Ciatt001 ciatt001) {
    return ciatt001.getClDocid();
}

@Override
public Ciatt001 getRowData(String docid) {
    //Integer id = Integer.valueOf(idBandeja);

    for (Ciatt001 ciatt001 : listaClientes) {
        if (docid.equals(ciatt001.getClDocid())) {
            return ciatt001;
        }
    }

    return null;
}       

和 ejb 方法:

 public List<Ciatt001> listaClientes2(String cia, String age  ,int startingAt, int maxPerPage) {

    Query query = em.createNamedQuery("Ciatt001.listaClientesPorCiaPorAge2");
    query.setParameter(1, cia);
    query.setParameter(2, age);
    query.setFirstResult(startingAt);
    query.setMaxResults(maxPerPage);
    return query.getResultList();
}
    public String countClientes2(String cia, String age) {

    Query query = em.createNamedQuery("Ciatt001.countClientes2");
    query.setParameter(1, cia);
    query.setParameter(2, age);     
    return query.getSingleResult().toString();
}

如何使用此延迟加载实现实现全局过滤器?

4

2 回答 2

5

在加载功能上,获取过滤器值(检查是否不为空)。

String filterValue = filters.get("globalFilter");

然后,如果您没有使用任何其他过滤器,请使用析取 (OR) 进行查询:

"select * from table where x = ? OR y = ?" //replace ? for globalFilter value

如果您使用其他字段,您应该这样做:

//normal filters 
filtersCondition  = "(x = ? AND y = ?)" //replace ? for filters values
//global filters
globalFilterCondition = "(x = globalFilter OR y = globalFilter)" //replace ? for globalFilter value
//normal filters + global filter
query = "select * from table where " +filtersCondition+ " AND "+ globalFilterCondition

当然,这个查询只是一个例子,你应该构建一个很好的并且参数化的 =)

于 2012-06-28T00:37:05.003 回答
1

这就是我如何让它工作,分页和过滤:D

LazyDataModel 现在看起来像这样:

在加载方法中:

       Set set = filters.entrySet();
       Iterator i = set.iterator();
       if (i.hasNext()) {
            Map.Entry me = (Map.Entry) i.next();
            filterValue = (String) me.getValue();
        }
        a = String.valueOf(agendamientoSession.countClientes2(cia, age, filterValue));
        listaClientes = agendamientoSession.listaClientes2(cia, age, startingAt, maxPerPage, filterValue);
        setRowCount(Integer.parseInt(a));

ejb:

    public List<Ciatt001> listaClientes2(String cia, String age  ,int startingAt, int maxPerPage,String filtro)      {
    Query query = em.createNamedQuery("Ciatt001.listaClientesPorCiaPorAge2");
    query.setParameter(1, cia);
    query.setParameter(2, age);
    query.setParameter(3,"%"+filtro+"%");
    query.setParameter(4,"%"+filtro+"%");
    query.setFirstResult(startingAt);
    query.setMaxResults(maxPerPage);
    }

和我的查询:

  select *  from ciatt001 c where c.cl_co_cia=? and c.cl_ag_agencia =?
  and c.cl_estado='A' and (c.cl_docid like ? or c.cl_nombre1 like ? .. more filters if needed)

就是这样,现在将寻找排序实现:D

于 2012-06-28T22:50:58.337 回答