0

我正在为新用户开发注册应用程序。在一页上,用户可以选择免费期限。为此,我使用 PrimeFaces DataTable - Radiobutton。我一直在关注PrimeFaces ShowCase

也许现在我有一个有点愚蠢的问题,但是我怎样才能获得选定行的数据,例如 c_id?我真的解决不了。例如,此尝试不起作用:

public String selectData() {
        String result = null;
        CollectionTerm selectedTerm = this.termsModel.getRowData();
        System.out.println("Row id: " + selectedTerm.getC_id());
        return result;
    }

它返回我错误:java.lang.IllegalArgumentException:行不可用

第二个问题,如何在另一个页面上显示所选行的数据?我做错了什么:

<h:form id="test">
    <h:outputText value="Id of selected row: " />  
    <h:outputText value="#{dl.c_id}" />
</h:form>

感谢您的任何帮助和建议!我是 JSF 的新手,有时这让我很困惑,我找不到任何合适的答案。

JSF 托管 Bean

@ManagedBean
@SessionScoped
public class Register implements Serializable {

    private static final long serialVersionUID = 1L;

    private UserDetails user;
    private Term term;
    private CollectionTerm collectionTerm;

    private List<CollectionTerm> dataList;
    private CollectionTerm selectedTerm;
    private TermDataModel termsModel;

    public Register() {

        dataList = new ArrayList<CollectionTerm>();
        loadDataList();
        termsModel = new TermDataModel(dataList);

        user = new UserDetails();
        term = new Term();
        collectionTerm = new CollectionTerm();
    }

    // getters and setters

private void loadDataList() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    dataList = session.createQuery("from CollectionTerm").list();
}

}

数据模型

import java.util.List;

import javax.faces.model.ListDataModel;

import org.primefaces.model.SelectableDataModel;

import de.ibmt.upb.model.CollectionTerm;

public class TermDataModel extends ListDataModel<CollectionTerm> implements SelectableDataModel<CollectionTerm> {

    public TermDataModel() {    
    }

    public TermDataModel(List<CollectionTerm> data) {
        super(data);
    }

    @Override
    public CollectionTerm getRowData(String rowKey) {

        List<CollectionTerm> dataList = (List<CollectionTerm>) getWrappedData();

        for(CollectionTerm term : dataList) {  
            if(term.getC_id().equals(rowKey))  
                return term;  
        } 

        return null;
    }

    @Override
    public Object getRowKey(CollectionTerm term) {
        return term.getC_id();
    }

}

JSF

<h:form id="collection">
    <p:fieldset legend="Date and time">
        <p:dataTable id="collection" value="#{register.termsModel}" var="dl" selection="#{register.selectedTerm}">
            <p:column selectionMode="single" style="width:2%" /> 
            <p:column>
                #{dl.c_id}
            </p:column>
            <p:column>
                #{dl.dateCollection}
            </p:column>
            <p:column>
                #{dl.timeCollection}
            </p:column>
        </p:dataTable>
        <p:commandButton value="Save" type="submit" action="#{register.selectData()}" ajax="false" />
     </p:fieldset>
</h:form>
4

1 回答 1

0

PrimeFaces<p:dataTable selection"...">不需要实现productModel.

上述问题的解决方法:使用rowKey

<p:dataTable id="collection" value="#{register.dataList}" var="dl" rowKey="#{dl.c_id}" selection="#{register.selectedTerm}">
    <p:column selectionMode="single" style="width:2%" /> 
    <p:column>
        #{dl.c_id}
    </p:column>
    ...
</p:dataTable>

如何获取选定行的数据,例如 c_id:

public String selectData() {
    String result = null;
    System.out.println("Get row data: " + selectedTerm.getC_id());
    return result;
}

如何在另一页上显示所选行的数据:

<h:form id="test">
    <h:outputText value="Id of selected row: " />  
    <h:outputText value="#{register.selectedTerm.c_id}" />
</h:form>
于 2013-05-05T22:19:42.503 回答