Primefaces Datatable:复选框选择不指定选定的值

时间:2013-04-26 00:39:46

标签: jsf-2 primefaces

我使用primefaces 3.5,使用glassfish服务器3.1.2。我有一个琐事问题游戏,依赖于用户选择答案。我有两个表,根据它是一个多选问题,还是它的多重选择,生成一个表。虽然我的多选数据表工作得很漂亮,但另一个却没有。我已经按照展示案例中的示例进行了操作,当我选择2个表格,并点击向导上的下一个按钮时,它会取消选择我选择的内容并让我保持在同一页面上。我让它在任何异常上停留在同一页面上,并且异常是一个空指针,因为"选择的答案"其中null。这是我的桌子。

<p:dataTable 
    id="multiQuestionTable"
    value="#{triviaQuestionsBean.dataModel}"
    var="answer"
    selection="#{triviaQuestionsBean.selectAnswers}">
    <p:column selectionMode="multiple" />

    <p:column>
        #{answer.answer.testAnswer}
    </p:column>
</p:dataTable>

设置和获取:

private QuestionAnswers[] selectAnswers;

public QuestionAnswers[] getSelectAnswers() {
    return selectAnswers;
}

public void setSelectAnswers(QuestionAnswers[] selectAnswers) {
    this.selectAnswers = selectAnswers;
}

永远不会调用setter,但使用的数据模型非常适合单选。如果需要找出我的问题,请告诉我。请尽可能提供帮助。

public class QuestionAnswersDataModel扩展了ListDataModel         实现SelectableDataModel {

/**
 * This is the question answers data model used to allow for the sorting,
 * and selection of items in a JSF dataTable. This is the basic no-arg
 * constructor --Important-- This judges the data from the id, so if the ID
 * has not been assigned, there will be unpredictable results.
 *
 */
public QuestionAnswersDataModel() {
}

/**
 * This is the question answers data model used to allow for the sorting,
 * and selection of items in a JSF dataTable. This is the constructor where
 * the list of elements are instantiated. --Important-- This judges the data
 * from the id, so if the ID has not been assigned, there will be
 * unpredictable results.
 *
 * @param data The list of QuestionAnswers to display in the table.
 */
public QuestionAnswersDataModel(List<QuestionAnswers> data) {
    super(data);
}

/**
 * This takes a "row key" and looks through the wrapped data to find the
 * specific QuestionAnswers entity that matches the passed in row key
 *
 * @param rowKey The key to search with
 * @return The QuestionAnswers entity that matches the criteria or null if
 * nothing matches
 */
@Override
public QuestionAnswers getRowData(String rowKey) {

    /**
     * Get the wrapped data (If there was a lot of data you would use a
     * query not just a list)
     */
    List<QuestionAnswers> answers =
            (List<QuestionAnswers>) getWrappedData();

    //for each answer
    for (QuestionAnswers answer : answers) {
        //if the answer's unique identifier matches the row key:
        if (answer.getQuestionAnswersId().toString().equals(rowKey)) {

            //return it
            return answer;
        }
    }

    //if nothing matches return null
    return null;
}

/**
 * This takes a QuestionAnswers entity object and returns a key for the
 * identification of this entity. As this one runs off of the ID of the
 * answer, if nothing is assigned to the value, a null key will be returned.
 *
 * @param answer The answer to generate the key of
 * @return The identifier for this object or null if the ID is null
 */
@Override
public Object getRowKey(QuestionAnswers answer) {
    //if the answer is null, return null
    if (answer == null) {
        return null;
    }
    //else get the answer id
    Long id = answer.getQuestionAnswersId();
    //if it's null return null
    if (id == null) {
        return id;

    }
    //else return the String representation of the id
    return id.toString();
}

0 个答案:

没有答案