我已经创建了Primefaces'的简单实现。 LazyDataModel
。现在paginator工作正常,但是当我在某个Datatable的列上设置sortBy="..."
属性并尝试通过单击我的JSF页面上的列标题对表进行排序时,在LazyDataModel#load
方法I' m仍然将sortField
设置为null
并将sortOrder
设置为ASCENDING
。发送ajax请求,包含排序字段和顺序,但是,此数据不会传递给load()方法。
我向JSF提供数据模型的支持bean是@Named @ViewScoped
,我使用的是Wildfly 8(JSF 2.2.5),Primefaces 4.任何提示?
@Override
public List<T> load(int first, int pageSize, String sortField,
SortOrder sortOrder, Map<String, String> filters) {
System.out.println("FIELD: " + sortField + ", ORDER: " + sortOrder);
.......
<p:dataTable value="#{logsBacking.appLogModel}"
paginator="true" rows="10" rowsPerPageTemplate="10"
var="appLog" lazy="true" rowKey="#{appLog.id}">
<p:column headerText="test" sortBy="id}">
<h:outputText value="id" />
</p:column>
............
答案 0 :(得分:2)
尝试覆盖其他load(),getRowKey()和getRowData()。
这是我的,它正在运作:
public class EntityDataModel<T extends AbstractEntity> extends LazyDataModel<T>
{
private static final long serialVersionUID = 1L;
protected final Class<T> entityClass;
protected final EntityConverter converter;
protected final PersistenceService service;
public EntityDataModel(Class<T> entityClass)
{
super();
this.entityClass = entityClass;
this.converter = new EntityConverter();
try
{
this.service = PersistenceService.lookup();
}
catch(NamingException e)
{
throw new RuntimeException(e.getMessage(), e);
}
setPageSize(1);
setRowCount(-1);
}
@Override
public List<T> load(int first, int pageSize, List<SortMeta> multiSortMeta, Map<String, String> filterMap)
{
try
{
List<T> resultList = service.queryAll(entityClass, first, pageSize, filterMap, multiSortMeta);
setRowCount(service.count(entityClass, filterMap).intValue());
return resultList;
}
catch(Exception e)
{
throw new RuntimeException(e.getMessage(), e);
}
}
@Override
public List<T> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filterMap)
{
return load(first, pageSize, Lists.newArrayList(new SortMeta(null, sortField, sortOrder, null)), filterMap);
}
@Override
public Object getRowKey(T entity)
{
return converter.getAsString(null, null, entity);
}
@Override
public T getRowData(String rowKey)
{
return (T) converter.getAsObject(null, null, rowKey);
}
@Override
public void setRowIndex(int rowIndex)
{
super.setRowIndex(getPageSize() == 0 ? -1 : rowIndex);
}
}
@PhilHDN
PersistenceService.queryAll()
使用许多内部对象来创建具有动态顺序的动态查询。
以下是量身定制的版本:
@TransactionAttribute(TransactionAttributeType.SUPPORTS)
public <T> List<T> queryAll(Class<T> entityClass, int first, int max, Map<String, ?> filter, List<SortMeta> sortList)
{
return queryAll(entityClass, first, max, createRestriction(filter), createSort(sortList));
}
@TransactionAttribute(TransactionAttributeType.SUPPORTS)
public <T> List<T> queryAll(Class<T> entityClass, int first, int max, Restriction<? super T> restriction, Sort<? super T> sort)
{
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<T> cq = builder.createQuery(entityClass);
Root<T> root = cq.from(entityClass);
cq.select(root).distinct(true);
if(restriction != null)
{
cq.where(restriction.apply(builder, root));
}
if(sort != null)
{
cq.orderBy(sort.apply(builder, root));
}
TypedQuery<T> query = em.createQuery(cq);
if(first > -1)
{
query.setFirstResult(first);
}
if(max > -1)
{
query.setMaxResults(max);
}
return query.getResultList();
}
private <T> Sort<T> createSort(List<SortMeta> sortList)
{
return (builder, root) ->
{
List<Order> ret = new LinkedList<>();
if(sortList != null)
{
for(SortMeta sortMeta : sortList)
{
String field = sortMeta.getSortField();
SortOrder sortOrder = sortMeta.getSortOrder();
if(field == null || field.isEmpty())
{
continue;
}
Expression<?> expr = root.get(field);
if(sortOrder == SortOrder.ASCENDING)
{
ret.add(builder.asc(expr));
}
else if(sortOrder == SortOrder.DESCENDING)
{
ret.add(builder.desc(expr));
}
}
}
return ret;
};
}
public interface Sort<T> extends BiFunction<CriteriaBuilder, Root<? extends T>, List<Order>>
{
// empty
}
答案 1 :(得分:0)
p:dataTable
必须在h:form
内,以便过滤器正常运行。