我有一个h:panelGrid
有两列和不同的行:
<h:panelGrid columns="2">
<p:outputLabel for="attr1" value="#{messages.attr1}" />
<p:inputText id="attr1" value="#{viewBean.attr1}" />
<p:outputLabel for="attr2" value="#{messages.attr2}" />
<p:inputText id="attr2" value="#{viewBean.attr2}">
<p:outputLabel for="attr3" value="#{messages.attr3}" />
<p:inputText id="attr3" value="#{viewBean.attr3}" />
</h:panelGrid>
现在,某些行只能有条件地出现。在示例中,这可能是 attr2 和 attr3。当我构建 <ui:fragment />
在它们周围,条件渲染有效(见下文),但是 jsf 尝试将整个 block 放入一个 <td />
中。表格单元格元素。
<h:panelGrid columns="2">
<p:outputLabel for="attr1" value="#{messages.attr1}" />
<p:inputText id="attr1" value="#{viewBean.attr1}" />
<ui:fragment rendered="#{viewBean.myCondition}">
<p:outputLabel for="attr2" value="#{messages.attr2}" />
<p:inputText id="attr2" value="#{viewBean.attr2}">
<p:outputLabel for="attr3" value="#{messages.attr3}" />
<p:inputText id="attr3" value="#{viewBean.attr3}" />
</ui:fragment>
</h:panelGrid>
另一个选择是创建两个 panelGrid。一个用于第一个 block ,一个用于第二个 block :
<h:panelGrid columns="2">
<p:outputLabel for="attr1" value="#{messages.attr1}" />
<p:inputText id="attr1" value="#{viewBean.attr1}" />
</h:panelGrid>
<h:panelGrid columns="2">
<ui:fragment rendered="#{viewBean.myCondition}">
<p:outputLabel for="attr2" value="#{messages.attr2}" />
<p:inputText id="attr2" value="#{viewBean.attr2}">
<p:outputLabel for="attr3" value="#{messages.attr3}" />
<p:inputText id="attr3" value="#{viewBean.attr3}" />
</ui:fragment>
</h:panelGrid>
这里的问题是,两个网格中的列现在不再简洁。有什么办法可以避免这个问题?
请注意,我不止三行,因此输入 rendered
每个 outputLabel
的属性和inputText
元素会非常乏味。
最佳答案
<h:panelGrid>
渲染器仅考虑直接 UIComponent
子项作为表格单元格,而不是更深层次的嵌套单元格。 <ui:fragment>
实际上也是一个UIComponent
。因此,它成为一个包含所有嵌套子项的单个表格单元格。
您有 2 个选项,具体取决于 rendered
的来源条件:
如果它在 View 构建期间可用,请使用
<c:if>
相反。<h:panelGrid columns="2"> <p:outputLabel ... /> <p:inputText ... /> <c:if test="#{viewBean.myCondition}"> <p:outputLabel ... /> <p:inputText ... /> <p:outputLabel ... /> <p:inputText ... /> </c:if> </h:panelGrid>
否则,如果不能保证在 View 构建期间可用,请删除
<ui:fragment>
并重复rendered
children 的状况。<h:panelGrid columns="2"> <p:outputLabel ... /> <p:inputText ... /> <p:outputLabel ... rendered="#{viewBean.myCondition}" /> <p:inputText ... rendered="#{viewBean.myCondition}" /> <p:outputLabel ... rendered="#{viewBean.myCondition}" /> <p:inputText ... rendered="#{viewBean.myCondition}" /> </h:panelGrid>
另请参阅:
关于jsf - 如何在 panelGrid 中插入条件行,同时保持行简洁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42765538/