我实现了一个延迟加载Datatable,其中包含实现
的primefacesload(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,Object> filters)
现在我需要将参数从我的页面传递给此方法(即,我的页面中有一个过滤器部分,过滤器不是表格的一部分,而且是独立的对象!)。我的参数存储在页面的托管bean中。
我怎样才能做到这一点?
谢谢!
答案 0 :(得分:1)
使参数成为bean的属性,并将它们直接传递给从db获取数据的服务(在本例中,服务是EJB MyObjFacade myObjFacade
):
@ManagedBean
@ViewScoped
public class MyBean {
@EJB
private MyObjFacade myObjFacade;
private LazyDataModel<MyObjType> model; // getter
private MyParameter myParameter;
@PostConstruct
public void init() {
model = new LazyDataModel<MyObjType> () {
@Override
public List<MyObjType> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
model.setRowCount(myObjFacade.count(filters, myParameter));
return myObjFacade.getResultList(first, pageSize, sortField, sortOrder, filters, myParameter);
}
};
model.setRowCount(myObjFacade.count(new HashMap<String, String> ()));
}
}
您只需提供实施count
和getResultList
方法的服务。
答案 1 :(得分:-1)
经过几个小时的搜索,我终于做到了。我将这个解决方案留在这里以供进一步参考。
//Managed Bean - View
public class DummyLazySearchPageBean {
private LazyDataModel<DummyObject> results;
private String searchFilter;//to simulate search filter
private DummyBusinessClass dummyBusinessClass;
public DummyLazySearchPageBean() {
results = new SearchsLazyLoader();
}
// GETTERS AND SETTERS
@SuppressWarnings("serial")
private final class SearchsLazyLoader extends LazyDataModel<DummyObject> {
@Override
public List<DummyObject> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,Object> filters) {
//Simulate BD model
List<DummyObject> data = new ArrayList<DummyObject>();
dummyBusinessClass = new DummyBusinessClass();
data = dummyBusinessClass.search(searchFilter, 5000);
//rowCount
this.setRowCount(data.size());
//paginate
if(data.size() > pageSize) {
try {
return data.subList(first, first + pageSize);
}
catch(IndexOutOfBoundsException e) {
return data.subList(first, first + (data.size() % pageSize));
}
}
else {
return data;
}
}
}
public final void search(){
results = new SearchsLazyLoader();
}
}
答案 2 :(得分:-1)
使用参数输入创建SearchsLazyLoader的构造函数。 这是我的代码示例:
package com.mandiri.askes.model.lazy;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import com.mandiri.askes.model.Participant;
import com.mandiri.askes.utils.CallAble;
import com.mandiri.askes.utils.HibernateUtil;
public class LazyPesertaDataModel extends LazyDataModel<Participant> {
private static final long serialVersionUID = 1L;
private String keyword;
public LazyPesertaDataModel(String keyword) {
if (StringUtils.isNotEmpty(keyword)) {
this.keyword = keyword;
}else{
this.keyword = "";
}
}
/**
* Collect Peserta Badan Usaha as Data Model Primefaces
*
*/
@Override
public List<Participant> load(int first, int pageSize, String sortField,
SortOrder sortOrder, Map<String, String> filters) {
List<Participant> listPesertaBpjs = new ArrayList<Participant>();
listPesertaBpjs = getLazyDataPeserta(first, pageSize, sortField);
this.setRowCount(countAllRecord());
return listPesertaBpjs;
}
/**
* Get Peserta Data using hibernate pagination <b>(Lazy Loading)</b> Technique.
*
* @author Dibrut
* @param firstRecord
* @param maxResult
* @return
*/
@SuppressWarnings("unchecked")
public List<Participant> getLazyDataPeserta(final int firstRecord, final int maxResult, final String sortField) {
return HibernateUtil.doInTransaction(new CallAble<List<Participant>>() {
@Override
public List<Participant> call(Session session) throws Exception {
List<Participant> pesertaList = new ArrayList<Participant>();
String hql = "FROM com.dibrut.learn.model.Participant P"
+ " WHERE P.name LIKE :keyword";
Query query = session.createQuery(hql).setFirstResult(firstRecord).
setMaxResults(maxResult).setString("keyword", "%"+keyword+"%");
pesertaList = query.list();
return pesertaList;
}
});
}
/**
* Count total participant
* @return
* @author Dibrut
*/
public int countAllRecord(){
return HibernateUtil.doInTransaction(new CallAble<Integer>() {
@Override
public Integer call(Session session) throws Exception {
String hql = "FROM com.dibrut.learn.model.Participant P"
+ " WHERE P.name LIKE :keyword";
Long totalPesertaBu = (Long) session.createQuery(hql).
setString("keyword", "%"+ keyword+"%").
uniqueResult();
return totalPesertaBu.intValue();
}
});
}
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
}
如果要搜索数据,只需使用managedBean属性的参数调用构造函数。例如,您在ManagedBean中有searchParticipant方法:
public void searchParticipant() {
this.participantLazyModel = new LazyPesertaDataModel(this.keyword);
}
其中关键字是页面中inputText的值。