如何在默认情况下创建一个包含1行的表

时间:2012-05-13 09:35:18

标签: jsf-2

我有一张桌子,我希望每次运行程序时默认显示1个空白行。我怎么能这样做?

<h:dataTable cellspacing="0" value="#{editQuestion.answersData}" var="answer">
                    <h:column>
                        <f:facet name="header">ID</f:facet>
                        <h:outputText value="#{answer.answer.id}" />
                    </h:column>

                    <h:column>
                        <f:facet name="header"><h:outputText value="#{i18n['admin.edit.rightanswer']}"  /></f:facet>
                        <h:selectBooleanCheckbox value="#{answer.answer.isRight}"/>
                    </h:column>

                    <h:column>
                        <f:facet name="header"><h:outputText value="#{i18n['admin.edit.answers']}"  /></f:facet>
                        <h:inputTextarea id="answer" rows="3" cols="40" value="#{answer.text}" required="true" label ="Answer">
                            <f:validateLength maximum="500"/>
                        </h:inputTextarea>
                        <div>
                           <h:message for="answer" style ="color:red"/>  
                        </div>

                    </h:column>

                    <h:column>
                        <h:commandButton image="/resources/imgs/#{editQuestion.buttonDelete}" immediate ="true" action="#{editQuestion.deleteAnswer(answer)}" disabled="#{!editQuestion.possibleToDelete}" alt="delete" title="#{i18n['img.delete']}">
                            <f:param name="id" value="#{editQuestion.id}"/>
                        </h:commandButton>
                    </h:column>
                </h:dataTable>`

1 个答案:

答案 0 :(得分:1)

Answer后面的bean(post)构建期间,只需将#{editQuestion.answerData}的新实例添加到#{editQuestion}后面的列表中。

E.g。在后期构建期间:

private List<Answer> answerData;

@PostConstruct
public void init() {
    answersData = new ArrayList<Answer>();
    answersData.add(new Answer());
}

请注意,bean必须为@ViewScoped才能在同一视图上的提交之间保留正确的数据。