[英]Sorting in an dynamically filled Datatable with lazy loading
i have issues with my lazy loaded Datatable. 我有我的懒加载数据表的问题。 First of all, here the code:
首先,这里的代码:
<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>
Following my sort-Method of my Lazy-Datamodel: 按照我的惰性数据模型的排序方法:
@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;
}
}
My Issue: The datatable gets rendered, and displays the data how it is supposed to do. 我的问题:呈现数据表,并以预期方式显示数据。 Now I want to sort the table depending on one (or multiple) columns.
现在,我想根据一(或多个)列对表进行排序。 The load method gets called, but the sortfield-string which is handed over to my load method is wrong (to be exact: "property]" gets printed).
调用了load方法,但是移交给我的load方法的sortfield-string是错误的(确切地说:“ property]”已打印)。
As far as i understand my syntax shouldn't be wrong, as I said the display of Data is totally correct. 据我了解,我的语法应该没有错,因为我说过数据的显示是完全正确的。 (so the syntax works just fine with the outputTexts, but doesn't work with my sortBy-clause in p:column?!)
(因此语法在outputTexts上可以正常工作,但在p:column中不能与我的sortBy子句一起使用!)
Is there an issue with my Syntax for primefaces-components? 我的primefaces-components语法有问题吗? And why is only property] handed over, not the complete String?
为什么只交出财产],而不交出完整的String? (i would somehow understand the situation if it was cTable[colC.property] which is handed over, but since its only the later part of the string i am utterly clueless to be honest.
(如果将cTable [colC.property]移交给我,我会以某种方式理解这种情况,但是老实说,由于它只是字符串的后半部分,所以我毫无头绪。
Would be great if so could clear things up for me and in the best case present a workaround :) 如果这样可以为我解决问题,那就太好了,最好是提供一种解决方法:)
Ok, so i have gotten it to work. 好的,所以我已经开始工作了。 Seems like the forEach was indeed the cause of trouble here.Thanks for bringing me onto the right track @perissf
似乎forEach确实是这里的麻烦原因。感谢您将我带入正确的轨道@perissf
Last thing to that would be interesting to understand is why it only handed over a part of the expression, but well^^ 要理解的最后一件事是为什么它只交出了表达式的一部分,但是很好^^
For anyone interested/facing a similar problem here is the code which is working fine for me: 对于任何有兴趣/面临类似问题的人,这里的代码对我来说都很好用:
DataTable: 数据表:
<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: 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;
}
}
}
And last my LazySorter: 最后是我的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();
}
}
}
Note: In the LazySorter i need to go for "cBean.class.getDeclaredField", cause my fields in the cBean are private. 注意:在LazySorter中,我需要输入“ cBean.class.getDeclaredField”,因为我在cBean中的字段是私有的。 If you have public fields you can go with the normal "getField" as shown in the primefaces-Showcase
如果您有公共字段,则可以使用常规的“ getField”,如primefaces-Showcase中所示
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.