Primefaces数据表选择对象 [英] Primefaces Datatable Selection Object

查看:87
本文介绍了Primefaces数据表选择对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Primefaces数据表有疑问,尤其是对选择对象有疑问.

I have a question to the Primefaces Datatable, especially to the Selection Object.

在下面的代码中,变量选定的问题"始终为Null,该变量已与选择"绑定到数据表.

In my following Code I get always Null for the Variable "Selected Question" which is bound to the Datatable with Selection.

jsf如下:

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui"
      xmlns:ui="http://java.sun.com/jsf/facelets">

    <ui:composition template="mainTemplate.xhtml">
        <ui:define name="contentTitle">Your Questions</ui:define>
        <ui:define name="content">
            <h:form id="formAllQuestion">
                <p:growl id="allQuestionGrowl" showDetail="true"/>

                <p:dataTable id="allQuestionsTable" var="question" value="#{allQuestionBean.allQuestionDataHelper}" paginator="true" rows="10"
                            selection="#{allQuestionBean.selectedQuestion}" selectionMode="single">

                    <p:ajax event="rowSelect" listener="#{allQuestionBean.onRowSelect}" update=":formAllQuestion:AnswerToQuestionDialogTable :formAllQuestion:allQuestionGrowl"
                            oncomplete="questDialog.show()"/>
                    <p:ajax event="rowUnselect" listener="#{allQuestionBean.onRowUnselect}" update=":formAllQuestion:allQuestionGrowl"/>


                    <f:facet name="header">Select a Row to display your Question Details</f:facet>

                    <p:column headerText="QuestionID">
                        #{question.questionId}
                    </p:column>
                    <p:column headerText="Question Name">
                        #{question.questionName}
                    </p:column>
                    <p:column headerText="Question Description">
                        #{question.questionText}
                    </p:column>
                    <p:column headerText="Question Short Description">
                        #{question.questionShortText}
                    </p:column>
                    <p:column headerText="Author">
                        #{question.professor.profSurename} #{question.professor.profName}
                    </p:column>
                </p:dataTable>


                <p:dialog header="Question Details" widgetVar="questionDialog" resizable="true" id="questDialog"
                          showEffect="fade" hideEffect="fade" modal="true">
                    <p:dataTable id="AnswerToQuestionDialogTable" var="answer" value="#{allQuestionBean.answers}">

                        <f:facet name="header">
                            Hier kommt der QR_Code rein!

                            #{allQuestionBean.selectedQuestion.questionId} - #{allQuestionBean.selectedQuestion.questionName}
                        </f:facet>

                        <p:column headerText="Answer">
                            <h:outputText value="#{answer.answerText}"/>
                        </p:column>
                        <p:column headerText="Counts For this Answer">
                            <h:outputText value="Bis jetz noch nix!"/>
                        </p:column>
                    </p:dataTable>
                </p:dialog>


            </h:form>
        </ui:define>
    </ui:composition>

</html>

以及关联的Bean类(AllQuestionBean.class):

And the associated Bean Class (AllQuestionBean.class):

 @ManagedBean(name = "allQuestionBean")
    @ViewScoped
    public class AllQuestionBean implements Serializable {


    private static final long serialVersionUID = 7038894302985973905L;

    @ManagedProperty(value = "#{questionDAO}")
    private QuestionDAO questionDAO;

    @ManagedProperty(value = "#{profSession.professor}")
    private Professor professor;

    @ManagedProperty(value = "#{answerDAO}")
    private AnswerDAO answerDAO;

    @ManagedProperty(value = "#{answeredDAO}")
    private AnsweredDAO answeredDAO;


    private List<Question> questions;
    private Question selectedQuestion;
    private List<Answer> answers;
    private AllQuestionDataHelper allQuestionDataHelper;


    public AllQuestionBean(){
        System.out.println("Starting Bean: "+this.getClass().getName());
    }

    @PostConstruct
    public void initVariables(){
        questions = questionDAO.readByProfessor(professor);
    }

    public void onRowSelect(SelectEvent event) {



        FacesMessage msg = new FacesMessage("Question Selected", selectedQuestion.getQuestionId()+" -- "+selectedQuestion.getQuestionName());

        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void onRowUnselect(UnselectEvent event) {

        FacesMessage msg = new FacesMessage("Question Selected", selectedQuestion.getQuestionId()+" -- "+selectedQuestion.getQuestionName());

        FacesContext.getCurrentInstance().addMessage(null, msg);
    }


    //---GETTER and SETTER

    public AllQuestionDataHelper getAllQuestionDataHelper() {
        allQuestionDataHelper = new AllQuestionDataHelper(questions);
        return allQuestionDataHelper;
    }


    public void setAllQuestionDataHelper(AllQuestionDataHelper allQuestionDataHelper) {
        this.allQuestionDataHelper = allQuestionDataHelper;
    }



    public QuestionDAO getQuestionDAO() {
        return questionDAO;
    }

    public void setQuestionDAO(QuestionDAO questionDAO) {
        this.questionDAO = questionDAO;
    }

    public Professor getProfessor() {
        return professor;
    }

    public void setProfessor(Professor professor) {
        this.professor = professor;
    }

    public AnswerDAO getAnswerDAO() {
        return answerDAO;
    }

    public void setAnswerDAO(AnswerDAO answerDAO) {
        this.answerDAO = answerDAO;
    }

    public AnsweredDAO getAnsweredDAO() {
        return answeredDAO;
    }

    public void setAnsweredDAO(AnsweredDAO answeredDAO) {
        this.answeredDAO = answeredDAO;
    }

    public List<Question> getQuestions() {
        return questions;
    }

    public void setQuestions(List<Question> questions) {
        this.questions = questions;
    }

    public Question getSelectedQuestion() {
        System.out.println("getSelectedQuestion");
        return selectedQuestion;
    }

    public void setSelectedQuestion(Question selectedQuestion) {
        System.out.println("Set selected Question: "+selectedQuestion);
        this.selectedQuestion = selectedQuestion;
    }

    public List<Answer> getAnswers() {
        answers = answerDAO.getAllAnswersForQuestion(selectedQuestion);
        return answers;
    }

    public void setAnswers(List<Answer> answers) {
        this.answers = answers;
    }
    }

数据模型:

public class AllQuestionDataHelper extends ListDataModel<Question> implements SelectableDataModel<Question> {


    public AllQuestionDataHelper() {
    }

    public AllQuestionDataHelper(List<Question> list) {
        super(list);
    }

    @Override
    public Object getRowKey(Question question) {
        if(!(question == null)){
        System.out.println("Your Questions --> Getting RowKey");
        System.out.println("RowKey: "+question);
        System.out.println("RowKey: "+question.getQuestionId());
        }else{
            System.out.println("Warning Row Key is null");
        }
        return question.getQuestionId();
    }

    @Override
    public Question getRowData(String rowKey) {
        System.out.println("Your Questions --> Getting RowData");
        System.out.println("RowData: "+rowKey);

        List<Question> questionList = (List<Question>) getWrappedData();

        for(Question q : questionList){
            if(rowKey.equals(q.getQuestionId())){
                System.out.println("Returning "+q.getQuestionId());
                return q;
            }
        }
        return null;
    }
}

我调试了几次运行,并提到从未设置AllQuestionBean.class中的变量"selectedQuestion".将会说,"onRowSelect"中的事件变量拥有一个NULL对象. 如您所见,*.xhtml中有两个数据表.第一个将正常加载,没有问题. Bean的onClick-Method应该会启动一个包含第二个数据表的对话框,但会以Nullpointer退出.

I debugged a few runs and mentioned that the Variable "selectedQuestion" in AllQuestionBean.class is never set. Will say, the event Variable in "onRowSelect" holds a NULL-Object. As you can see there are two Datatables in the *.xhtml. The first one will load normally, no problems. The onClick-Method of the Bean should launch a Dialog with the Second Datatable, but will quit with a Nullpointer.

对于数据表,我遵循了Primefaces上的教程(http://www.primefaces.org/showcase-labs/ui/datatableRowSelectionInstant.jsf)

For the Datatable I followed the Tutorial at Primefaces (http://www.primefaces.org/showcase-labs/ui/datatableRowSelectionInstant.jsf)

推荐答案

p:dataTable上使用rowKey属性.

rowKey是唯一的标识符,可帮助Primefaces引擎根据选择返回选定的对象.

rowKey is a unique Identifier that helps Primefaces engine to return the selected Object based on selection.

通常,您提供给rowKey属性的值是您要填充到p:dataTable的POJO的唯一属性.

Usually the value you supply to the rowKey attribute is the unique property of the POJO that you are populating in to p:dataTable.

如果您的POJO中没有任何此类唯一字段.然后创建一个总是很有用的,例如:int rowId;,在将它们添加到列表时可以增加并放入POJO.

If you don't have any such unique fields in your POJO. Then its always useful to make one, for example: int rowId;, which you might increment and put in to POJO while you adding them to the List.

这篇关于Primefaces数据表选择对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆