2013-02-12 73 views
0

我有一個具有30個屬性的Java對象。我從數據庫填充對象。我只想顯示非空的值。在JSF表中顯示值

<table> 
    <col width="280"/><col width="130"/> 

    <ui:repeat var="ud" value="#{TreeViewController.componentData}"> 

     <tr> 
      <td> 
       <h:outputText value="componentStatsId" rendered="#{ud.componentStatsId != 0}"/> 
      </td> 
      <td> 
       <h:outputText value="#{ud.componentStatsId}" rendered="#{ud.componentStatsId != 0}"/> 
      </td> 
     </tr> 

     .......... and 40 more table rows 

    </ui:repeat> 
</table> 

我測試過創建一個簡單的JSF表,如果這些值是空的,那麼這些表就不會呈現。但我注意到,如果值是空的,我得到的微小空間:

enter image description here

我怎麼能解決這個問題?

回答

3

是的,這將是您的代碼的預期行爲,因爲它只是防止呈現<<h:outputText>而不是<tr><td>組件。

爲了解決這個問題,你應該使用<ui:fragment>,並使用rendered屬性控制<tr><td> renderization:

<ui:repeat var="ud" value="#{TreeViewController.componentData}"> 
    <ui:fragment rendered="#{ud.componentStatsId != 0}"> 
     <tr> 
      <td> 
       componentStatsId 
      </td> 
      <td> 
       #{ud.componentStatsId} 
      </td> 
     </tr> 
    </ui:fragment> 
    <!-- .......... and 40 more table rows --> 
    <ui:fragment rendered="#{ud.componentTypeId != 0}"> 
     <tr> 
      <td> 
       ... 
      </td> 
      <td> 
       ... 
      </td> 
     </tr> 
    </ui:fragment> 
</ui:repeat> 
+0

我必須把'UI:下面的'UI fragment':repeat'只有一次,或在每個表格的頂部? – 2013-02-12 21:40:18

+0

''應該在''內。請注意,''只會幫助您瞭解是否應該渲染(或不渲染)「」和「​​」 – 2013-02-12 21:41:23

+0

我已更新答案以提供更好的示例。 – 2013-02-12 21:46:09