アプリケーションに遅延読み込みを実装しようとしていますが、フィルタリングに問題があります。この時点で、並べ替えは正しく機能しています。レイジーフィルタリングを使用すると、データテーブルは最初に24レコードのテーブルから正しいカウントをロードしますが、1番目と2番目のレコードを何度も繰り返します。どんな助けでも大歓迎です。
これが私のScreenshotListProducerクラスコードです:
@ManagedBean
@RequestScoped
public class ScreenshotListProducer implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
private EntityManager em;
private List<Screenshot> screenshots;
private LazyDataModel<Screenshot> lazyModel = null;
private int pageSize = 5;
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageSize() {
return pageSize;
}
@Produces
@Named
public List<Screenshot> getScreenshots() {
System.out.println("************* getting screenshots list **************");
return screenshots;
}
@PostConstruct
public void LoadData() {
lazyModel = new LazyDataModel<Screenshot>() {
private static final long serialVersionUID = 1L;
@Override
public List<Screenshot> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,String> filters) {
int start = first;
int end = first + pageSize;
if (sortField == null) {
sortField = "time";
}
try {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Screenshot> criteria = cb.createQuery(Screenshot.class);
Root<Screenshot> screenshot = criteria.from(Screenshot.class);
// sorting
if (sortOrder.equals(SortOrder.ASCENDING))
{
criteria.select(screenshot).orderBy(cb.asc(screenshot.get(sortField)));
}
else if (sortOrder.equals(SortOrder.DESCENDING))
{
criteria.select(screenshot).orderBy(cb.desc(screenshot.get(sortField)));
}
// From
Root<Screenshot> from = criteria.from(Screenshot.class);
List<Predicate> predicates = new ArrayList<Predicate>();
if (filters != null) {
// filters
for(Iterator<String> it = filters.keySet().iterator(); it.hasNext();) {
String filterProperty = it.next(); // table column name = field name
System.out.println("filterProperty is: " + filterProperty);
String filterValue = filters.get(filterProperty);
System.out.println("filterValue is: " + filterValue);
Expression<String> literal = cb.literal((String)filterValue);
predicates.add(cb.like(from.<String>get(filterProperty), literal));
}
criteria.where(predicates.toArray(new Predicate[predicates.size()]));
}
TypedQuery<Screenshot> s = em.createQuery(criteria);
s.setMaxResults(end - start);
s.setFirstResult(start);
screenshots = s.getResultList();
} catch (NullPointerException e) {
e.printStackTrace();
}
return screenshots;
}
};
lazyModel.setRowCount(24);
lazyModel.setPageSize(pageSize);
if (lazyModel.getRowIndex() == -1 || lazyModel.getPageSize() == 0) {
lazyModel.setRowIndex(-1);
}
else
lazyModel.setRowIndex(lazyModel.getRowIndex() % lazyModel.getPageSize());
}
public LazyDataModel<Screenshot> getLazyModel() {
return lazyModel;
}
}
これが私のスクリーンショットクラスです:
@Entity
@XmlRootElement
@Table(name="Test2012", uniqueConstraints = @UniqueConstraint(columnNames = "id"))
public class Screenshot implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "id", columnDefinition="INT")
private Long id;
private Timestamp time;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Timestamp getTime() {
return time;
}
public void setTime(Timestamp time) {
this.time = time;
}
}
これが私のxhtmlコードです:
<p:dataTable id="table1" var="scrshot" rowKey="#{scrshot.id}" value="#{screenshotListProducer.lazyModel}" paginator="true" rows="7" paginatorPosition="bottom"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="7,20,50,100" widgetVar="dataTable" currentPageReportTemplate="(Number of Records: {totalRecords})"
emptyMessage="No screenshot data found with given criteria" lazy="true" scrollable="true" draggableColumns="true" scrollHeight="217" style="width: 100%;">
<f:facet name="header">
</f:facet>
<p:column selectionMode="multiple"/>
<p:column id="time" headerText="Time" sortBy="#{scrshot.time}" filterBy="#{scrshot.time}" filterMatchMode="startsWith">
<h:outputText value="#{scrshot.time}"/>
</p:column>
<p:column id="id" headerText="ID" sortBy="#{scrshot.id}" filterBy="#{scrshot.id}" filterOptions="#{scrshot.id}" filterMatchMode="exact">
<h:outputText value="#{scrshot.id}"/>
</p:column>
</p:dataTable>
Primefaces 3.3.1 Mojarra 2.1.5 JBoss AS 7.1.0.Final