简体   繁体   English

Primefaces数据表:无法检索所选行,并且选择由于分页而丢失

[英]Primefaces datatable: Unable to retrieve selected rows and selection get lost with pagination

I am using primefaces datatable for checkbox based selection and try to implement given example . 我正在使用primefaces数据表进行基于复选框的选择,并尝试实现给定的示例 They used Model say, userModel which could implement SelectableDataModel interface. 他们使用模型,例如userModel ,可以实现SelectableDataModel接口。 I don't want to use model so I used rowKey for this purpose. 我不想使用模型,因此我为此使用了rowKey

Eg 例如

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>

Here, 这里,

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;
    }
}  

My problem: 我的问题:

1) When I select mulitple rows, selectedUsers remains empty.
2) After selecting next page, previous selection get lost. 

I went through @BelusC blog and found that binding is possible solution but unable to solve my problem with his instructions. 我浏览了@BelusC 博客 ,发现绑定是可能的解决方案,但无法用他的说明解决我的问题。 Is converter needed..? 是否需要转换器..? Is there any thing wrong with my approach. 我的方法有什么问题吗? Thanks 谢谢

Update:1 更新:1

The reason behind using Arraylist of Arraylist( allItems ) is only to make datatable generic. 使用Arraylist( allItems )的Arraylist的原因仅是为了使数据表通用。 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) 这就是为什么我要检索不依赖于对象的选定项( 例如 :car [] selectedcars)

rowKey="#{item[0]}" makes no sense to me. rowKey="#{item[0]}"对我来说毫无意义。 it will point to the same (0) object in every iteration. 它将在每次迭代中指向同一(0)对象。 While in the dataTable demo it points to the current car.model . 在dataTable演示中,它指向当前的car.model

Have you tried to change to #{item}? 您是否尝试过更改为#{item}?

I had similar issues and I solved it using <p:ajax> . 我有类似的问题,我使用<p:ajax>解决了。

First I would do as akoskm says and have a unique row key. 首先,我会按照akoskm所说的做,并且有一个唯一的行键。

Then I would use an array ( List<String>[] ) ( List is more generic than ArrayList ) for selectedItems 然后我将为selectedItems使用数组( List<String>[] )( ListArrayList更通用)

And finally you can add: 最后,您可以添加:

<p:dataTable ...>
    <p:ajax event="rowSelectCheckbox" process="@this"/>
    <p:ajax event="rowUnselectCheckbox" process="@this"/>
    <p:ajax event="toggleSelect" process="@this"/>
</p:dataTable>

Just make sure you have an <h:form> around the table 只要确保您在桌子周围有<h:form>

That will make sure that the component pushes the data to the bean. 这将确保组件将数据推送到Bean。 I'm not sure if it will solve the pagination problem though, let me know. 我不确定是否可以解决分页问题。

Correct Answer : Primefaces datatable with checkbox selection ONLY 正确答案: 仅带有复选框选择的Primefaces数据表

You can solve this problem using rowSelectionMode="add" 您可以使用rowSelectionMode="add"解决此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM