只需使用參數輸入創建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的值。
我不能使用兩個diferente類嗎?如此處所述http://www.primefaces.org/showcase/ui/data/datatable/lazy.xhtml。 – RMartins
從理論上講,您應該可以使用不同的類,但處理一些事情會更困難:對於數據源注入,模型應該是EJB或視圖範圍的bean。嘗試一下,如有必要,在新的問題中發佈代碼。 – perissf
這工作(我有點相同)。你甚至可以把它變成一個基類,並且很容易添加像'多租戶'這樣的基本事物 – Kukeltje