我的延迟加载数据表存在问题。 首先,这里是代码:
<p:tabView>
<!-- Tabs A and B, working fine -->
<p:tab title="C">
<p:commandButton value="get C" id="openC" actionListener="#{backingBean.initC}" render="cTable" update="cTable"></p:commandButton>
<p:separator/>
<p:dataTable id="cTable" var="cTable" value="#{backingBean.lazyC}" paginator="true"
paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
rowsPerPageTemplate="5,10,15" rows="10" sortMode="multiple" lazy="true" rendered="#{backingBean.cAvailable}">
<c:forEach var="colC" items="#{backingBean.headerAllocationC}">
<p:column headerText="#{colC.header}" sortBy="#{cTable[colC.property]}">
<div align="center">
<h:outputText value="#{cTable[colC.property]}"></h:outputText>
</div>
</p:column>
</c:forEach>
</p:dataTable>
</p:tab>
</p:tabView>
遵循我的惰性数据模型的排序方法:
@Override
public List<cBean> load(int first, int pageSize, List<SortMeta> multiSortMeta, Map<String,Object> filters){
List<cBean> data = new ArrayList<cBean>();
if (multiSortMeta != null) {
for (SortMeta sortMeta : multiSortMeta) {
System.out.println("SORTFIELD:" +sortMeta.getSortField());
System.out.println("SORTORDER:" +sortMeta.getSortOrder());
//System.out.println("SORTFUNCTION:"+sortMeta.getSortFunction());
System.out.println("COLUMN:" +sortMeta.getColumn());
System.out.println("CLASS:" +sortMeta.getClass());
}
}
for (cBean c : datasource) {
boolean match = true;
if (filters != null) {
for (Iterator<String> it = filters.keySet().iterator(); it.hasNext();) {
try {
String filterProperty = it.next();
Object filterValue = filters.get(filterProperty);
String fieldValue = String.valueOf(c.getClass().getField(filterProperty).get(c));
if (filterValue == null || fieldValue.startsWith(filterValue.toString())) {
match = true;
}
else {
match = false;
continue;
}
} catch (Exception e) {
match = false;
}
}
}
if (match) {
data.add(c);
}
}
int dataSize = data.size();
this.setRowCount(dataSize);
if (dataSize > pageSize) {
try {
return data.subList(first, first+pageSize);
} catch (IndexOutOfBoundsException e) {
return data.subList(first, first+(dataSize%pageSize));
}
} else {
return data;
}
}
我的问题: 数据表将被呈现,并显示数据应该如何执行。 现在,我想根据一(或多个)列对表进行排序。调用了load方法,但是移交给我的load方法的sortfield-string是错误的(确切地说,是“ property]”被打印出来)。
据我了解,我的语法应该没有错,因为我说过数据的显示是完全正确的。 (所以语法对outputTexts来说很好用,但对我在p:column中的sortBy子句不起作用?!)
primefaces-components的语法是否存在问题? 为什么只交出财产],而不交出完整的String? (如果将cTable [colC.property]移交给我,我会以某种方式了解这种情况,但是由于这只是字符串的后半部分,因此我实在毫无头绪。
如果这样可以为我解决问题,那就太好了,最好是提供一种解决方法:)
答案 0 :(得分:0)
好,所以我已经开始工作了。 似乎forEach确实是这里的麻烦原因。感谢您将我带入正确的轨道@perissf
最后一个有趣的事情是为什么它只交出了一部分表达式,但是很好^^
对于任何有兴趣/面临类似问题的人,这里的代码对我来说都很好:
数据表:
<p:dataTable id="cTable" var="cTable" value="#{backingbean.lazyC}" paginator="true"
paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
rowsPerPageTemplate="5,10,15" rows="10" sortMode="multiple" lazy="true" rendered="#{backingBean.cAvailable}">
<p:columns value="#{backingBean.headerAllocationC}" var="colC" sortBy="#{cTable[colC.property]}">
<f:facet name="header">
<h:outputText value="#{colC.header}"/>
</f:facet>
<h:outputText value="#{cTable[colC.property]}"/>
</p:columns>
</p:dataTable>
LazyDataModel:
public class LazyCDataModel extends LazyDataModel<cBean>{
private static final long serialVersionUID = 1L;
private List<cBean> datasource;
public LazyCDataModel (List<cBean> datasource) {
this.datasource = datasource;
}
@Override
public List<cBean> load(int first, int pageSize, List<SortMeta> multiSortMeta, Map<String,Object> filters){
List<cBean> data = new ArrayList<cBean>();
for (cBean c : datasource) {
boolean match = true;
if (filters != null) {
for (Iterator<String> it = filters.keySet().iterator(); it.hasNext();) {
try {
String filterProperty = it.next();
Object filterValue = filters.get(filterProperty);
String fieldValue = String.valueOf(bestand.getClass().getField(filterProperty).get(bestand));
if (filterValue == null || fieldValue.startsWith(filterValue.toString())) {
match = true;
}
else {
match = false;
continue;
}
} catch (Exception e) {
match = false;
}
}
}
if (match) {
data.add(c);
}
}
if (multiSortMeta != null) {
for (SortMeta sortMeta : multiSortMeta) {
if (sortMeta.getSortField() != null) {
Collections.sort(data, new LazyCSort(sortMeta.getSortField(),sortMeta.getSortOrder()));
}
}
}
int dataSize = data.size();
this.setRowCount(dataSize);
if (dataSize > pageSize) {
try {
return data.subList(first, first+pageSize);
} catch (IndexOutOfBoundsException e) {
return data.subList(first, first+(dataSize%pageSize));
}
} else {
return data;
}
}
}
最后是我的LazySorter:
public class LazyCSort implements Comparator<cBean>{
private String sortField;
private SortOrder sortOrder;
public LazyBestandsSort(String sortField, SortOrder sortOrder) {
this.sortField = sortField;
this.sortOrder = sortOrder;
}
public int compare(cBean c1, cBean c2) {
try {
Field field1 = BestandsBean.class.getDeclaredField(this.sortField);
Field field2 = BestandsBean.class.getDeclaredField(this.sortField);
field1.setAccessible(true);
field2.setAccessible(true);
Object value1 = field1.get(bestand1);
Object value2 = field2.get(bestand2);
int value = ((Comparable) value1).compareTo(value2);
return SortOrder.ASCENDING.equals(sortOrder) ? value : -1 * value;
} catch (Exception e){
e.printStackTrace();
}
}
}
注意: 在LazySorter中,我需要输入“ cBean.class.getDeclaredField”,因为我在cBean中的字段是私有的。如果您有公共字段,则可以使用普通的“ getField”,如primefaces-Showcase所示