簡體   English   中英

多個字段的條件顯示-JSF 2.0 / Primefaces

[英]conditional display for multiple fields - JSF 2.0 / Primefaces

有條件地顯示多個元素(例如,取決於bean值的字段列表)的最佳策略是什么?

我想到的可能解決方案:

  1. JSTL <c:if ... >子句。 據我了解,不建議將JSTL與JSF一起使用
  2. 使用大多數組件的“渲染”屬性。 不幸的是,當我不得不處理很多領域時,將它們設置在每個領域上都變得笨拙...
  3. 將元素放在容器上並在容器上設置渲染屬性

選項3似乎是最明智的選擇,但我不知道該使用哪些組件來包裝這些字段。 它必須是不影響布局的組件...

我可以使用span作為包裝器並設置CSS可見屬性,但是字段仍將呈現,只是不可見。

有什么想法嗎?

更新:

這是一些實際的布局代碼。 我已經嘗試了<h:panelGroup><ui:fragment> 使用任何這些標簽會將我的所有字段都放在一個<td> ,我承認,這是有道理的,因為我將一個頂級元素放置在panelGrid

我想要的唯一起作用的方法是上面列表中的#2。

    <h:panelGrid columns="2">
        <!-- fields if person -->
        <ui:fragment rendered="#{createEntity.entityType eq 'fizica'}">
            <h:outputLabel value="Prenume: " />
                <h:inputText value="#{createEntity.person.firstName}" />
            <h:outputLabel value="Nume familie: " />
                <h:inputText value="#{createEntity.person.familyName}"  />
            <h:outputLabel value="CNP: " />
                <h:inputText value="#{createEntity.person.cnp}" />
            <h:outputLabel value="Date carte identitate: " />
                <h:inputText value="#{createEntity.person.idCardInfo}" />
            <h:outputLabel value="Cetatenie: " />
                <h:inputText value="#{createEntity.person.country}" />
        </ui:fragment>

        <!--  fields for company  -->
        <ui:fragment rendered="#{createEntity.entityType eq 'juridica'}">           
            <h:outputLabel value="Denumire firma"  />
                <h:inputText value="#{createEntity.company.name}" />
            <h:outputLabel value="CUI"  />
                <h:inputText value="#{createEntity.company.cui}" />
            <h:outputLabel value="Registrul Comertului"  />
                <h:inputText value="#{createEntity.company.regCommerceNo}" />
        </ui:fragment>
    </h:panelGrid>  

建議使用“渲染”來顯示(或不顯示)組件。

如果可以將它們分組在一起,則設置容器的渲染屬性是完全有效的。 我會優先於在每個單獨的組件上設置渲染屬性。

唯一的次要缺點是,如果顯示是動態的(更改后備bean的值),並且需要重新顯示容器,則該容器不存在。 您需要將容器包裝在另一個容器中,然后重新渲染。

您可以將h:panelGroup用作此容器。 它具有rendered屬性,並且將導致跨度(默認情況下)。

我不得不訴諸使用,因為當我將列值包裝在另一個組件(如ui:fragment或h:panelGroup)中以使用該組件的呈現時,panelGrid將整個ui片段視為一列,這確實弄亂了我的表。

<c:if test="#{myBean.displayThese}">
   <!-- columns to display for this condition -->
</c:if>
<c:if test="#{!myBean.displayThese}">
   <!-- other stuff to display -->
</c:if>

令人驚訝的是, <c:otherwise>似乎對我沒有用。

丑陋,但也許可以通過條件樣式為您工作:

<h:component value="foo" style="#{!myBean.displayFoo ? 'display: none;' : ''}" />

如果您請求的條件是數據庫變量的值:

<c:if test="#{myObject.mycolumnTableDB==value1}">
   <!-- columns to display for this condition -->
</c:if>
<c:if test="#{myObject.mycolumnTableDB==value2}">
   <!-- other stuff to display -->
</c:if>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM