我有一张桌子,我希望每次运行程序时默认显示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>`
答案 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
才能在同一视图上的提交之间保留正确的数据。