使用延迟加载在primefaces数据表中实现全局过滤器

时间:2012-06-27 23:00:40

标签: filter primefaces lazy-loading global

大家好,我有一个lazyDataModel的实现,遵循本教程http://uaihebert.com/?p=1120

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

查看:

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

MY Managed Bean:

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

    return listaClientesLazy;
}

我的LazyDataModel

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

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

Apreciatte your help,Thanks:D

2 个答案:

答案 0 :(得分:4)

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

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

然后,如果您没有使用任何其他过滤器,请使用disjunction(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

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

答案 1 :(得分: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