Primefaces数据表:无法检索选定的行,并且选择由于分页而丢失 [英] Primefaces datatable: Unable to retrieve selected rows and selection get lost with pagination
问题描述
我正在使用primefaces数据表进行基于复选框的选择,并尝试实现给出的示例.他们使用Model说来,即 userModel ,它可以实现SelectableDataModel接口.我不想使用模型,因此为此目的使用了 rowKey .
I am using primefaces datatable for checkbox based selection and try to implement given example. They used Model say, userModel which could implement SelectableDataModel interface. I don't want to use model so I used rowKey for this purpose.
例如
datatable.xhtml
datatable.xhtml
<p:dataTable id="table" var="item" value="#{userBean.allItems}"
paginatorPosition="bottom" paginator="true" rows="3"
selection="#{userBean.selectedItems}" rowKey="#{item[0]}">
<p:column selectionMode="multiple"/>
<p:columns value="#{userBean.itemColHeader}" columnIndexVar="colIndex" var="colName" >
<f:facet name="header" >
<h:outputText value="#{colName}"/>
</f:facet>
<h:outputText value="#{item[colIndex]}"/>
</p:columns>
</p:dataTable>
在这里
allItems = ArrayList<ArrayList<String>>
selectedItems = ArrayList<ArrayList<String>>
selectedItems = ArrayList<String>
userBean.java
userBean.java
@ManagedBean(name="userBean")
@ViewScoped
public class userBean implements SelectableDataModel {
private ArrayList<ArrayList<String>> selectedItems;
public ArrayList<ArrayList<String>> getSelectedItems() {
return selectedItems;
}
public void setSelectedItems(ArrayList<ArrayList<String>> selectedUsers) {
this.selectedItems = selectedItems;
}
}
我的问题:
1) When I select mulitple rows, selectedUsers remains empty.
2) After selecting next page, previous selection get lost.
我经历了@BelusC 博客,发现绑定是可能的解决方案,但无法按照他的指示解决我的问题.是否需要转换器..?我的方法有什么问题吗?谢谢
I went through @BelusC blog and found that binding is possible solution but unable to solve my problem with his instructions. Is converter needed..? Is there any thing wrong with my approach. Thanks
更新:1
使用Arraylist( allItems )的Arraylist的原因仅是为了使数据表具有通用性.在绘制数据表时,我不必理会列数.这就是为什么我要检索不依赖于对象的选定项(喜欢:car [] selectedcars)
The reason behind using Arraylist of Arraylist(allItems) is only to make datatable generic. I need not to bother about the no of columns while drawing datatables. That is why I want to retrieve the selected items which should not depend on object(like: car[ ] selectedcars)
推荐答案
我遇到了类似的问题,并使用<p:ajax>
解决了该问题.
I had similar issues and I solved it using <p:ajax>
.
首先,我将按照akoskm所说的做,并具有唯一的行键.
First I would do as akoskm says and have a unique row key.
然后我将为selectedItems
使用数组(List<String>[]
)(List
比ArrayList
更通用)
Then I would use an array (List<String>[]
) (List
is more generic than ArrayList
) for selectedItems
最后您可以添加:
<p:dataTable ...>
<p:ajax event="rowSelectCheckbox" process="@this"/>
<p:ajax event="rowUnselectCheckbox" process="@this"/>
<p:ajax event="toggleSelect" process="@this"/>
</p:dataTable>
只需确保您在桌子旁有<h:form>
Just make sure you have an <h:form>
around the table
这将确保组件将数据推送到Bean.我不确定是否可以解决分页问题,请告诉我.
That will make sure that the component pushes the data to the bean. I'm not sure if it will solve the pagination problem though, let me know.
这篇关于Primefaces数据表:无法检索选定的行,并且选择由于分页而丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!