当前位置: 首页 > 知识库问答 >
问题:

Primefaces数据表选择对象

长孙谦
2023-03-14

我对Primefaces数据表有一个问题,尤其是对Selection对象。

在我下面的代码中,我总是为变量“选定的问题”获得空,该变量绑定到具有选择的数据表。

联合战略框架如下:

    <?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类(AllCountionBean.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中的变量“selectedQuestion”。课程永远不会固定。也就是说,“onRowSelect”中的事件变量包含一个空对象。如您所见,在*中有两个数据表。xhtml。第一个将正常加载,没有问题。Bean的onClick方法应该启动一个与第二个Datatable的对话框,但将使用null指针退出。

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

共有1个答案

王弘和
2023-03-14

在p: dataTable上使用rowKey属性。

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

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

如果你的POJO中没有任何这样独特的字段。然后,创建一个总是很有用的,例如:int rowId;,当您将它们添加到List时,您可以将其递增并放入POJO。

 类似资料:
  • 该页显示列表中的正确值。然而,当我尝试选择一个项目。它不起作用。 我能在列表中选择一个项目吗?

  • 我有一个primefaces p:treeTable,在它里面我定义了一个包含两个组件的列:span标记和h:outputText标记。当前,如果我单击其中一个元素,就会触发select event of table。但我的目的是,仅当我单击文本的某个部分(而不是span元素)时,才会触发select事件。我该怎么做?

  • 我正在使用liveScroll="true"和seltionMode="multi"的primeface v5.3 dataTable。标题复选框仅选择可见行,当我向下滚动时,新行显示为未选中。我希望标题复选框选择所有行:可见和不可见。仅选择可见行是毫无意义和无用的。是否可以修复? 我试图通过处理“toggleSelect、rowSelectCheckbox和rowUnselectCheckbox

  • 我将primefaces数据表与bootsfaces一起使用,我想解决一个CSS冲突。 从primefaces showcase实现过滤器示例: 给我这个结果:datatable过滤器示例很好 但是,在页面中添加bootsfaces组件,例如(唯一的更改是添加一个空

  • 我有一个p:dataTable,它存储已处理表单的结果,我执行以下操作: 填充表单值。 提交。 已填充数据表--对任何列进行筛选。 更改表单值。 提交。 使用新结果填充数据表。 对任意列排序--将显示来自#3的筛选结果。 我有一个用于筛选值的arraylist,但我没有在bean中对它做任何操作。提交时,我调用actionListener,该actionListener将dataTable强制转换

  • 我试图使用primefaces 4.0的可选datatable,但所选对象始终为null。我已经厌倦了像这里和这里所说的那样添加行键,但仍然得到null。。。 这是我的页面: 在我的后盾豆里: newAppraiseBean.modifyAppDetail():(只需打印所选项目) 数据模型: 它总是打印为空,我不知道我遗漏了什么。 这是netbean项目的压缩文件,解压后可以直接用netbean