DataTable - Radiobutton:如何获取所选行的数据

时间:2013-05-03 12:46:58

标签: jsf-2 primefaces datatable

我正在为新用户开发注册申请。在一个页面上,用户可以选择免费期限。为此我使用的是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 Managed 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>

1 个答案:

答案 0 :(得分: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>