2012-12-08 79 views
5

我有一個組件,我想在用戶點擊commandButton後顯示/隱藏。AJAX渲染屬性不適用於渲染=「false」組件

是這樣的:

<h:commandButton id="showButton" value="#{bean.wasPressed ? 'Hide' : 'Show'}"> 
    <f:ajax listener="#{bean.toggle()}" render="explanation showButton" /> 
</h:commandButton> 

<h:panelGroup id="explanation" rendered="#{bean.wasPressed}"> 
    <h:outputText value="something" /> 
</h:panelGroup> 

bean.toggle()只是適當的wasPressed屬性設置爲true或false。我正在使用<h:form prependId="false">

問題是我的按鈕的render屬性的值。它明確列舉了:explanationshowButton

只要showButton總是存在(它只會更改其標籤),則explanation僅在wasPressed屬性爲true時才存在。否則,它說:

malformedXML:在更新:explanaition沒有找到

我怎樣才能解決這個問題?

我不想回復隱藏源代碼中的元素,所以我不想使用任何jQuery切換( - )或任何隱藏元素使用style="display: none"或任何這些東西。

它甚至可以在JSF 2.1中實現嗎?

+2

包裝添加到panelGroup中有一個ID,並指它....你不能指不會呈現JSF elememt的http://計算器。 com/a/10707789/617373 – Daniel

+0

工程就像一個魅力,謝謝!你能否給這個問題添加答案,以便我能夠接受它? –

+0

不客氣。 – Daniel

回答

9

不能更新未渲染的元素,呈現=假「是一個JSF辦法」中刪除從DOM樹中的元素,

它不是如CSS display:none或visibility:hidden的< - 這兩將保留DOM樹中的元素但隱藏,而JSF呈現= false將不會在DOM樹中呈現(保留)元素(甚至不會在頁面的「視圖源」中看到它)

所以在你的情況下,你需要用另一個`panelGroup'包裝panelGroup並更新包裝的ID

<h:commandButton id="showButton" value="#{bean.wasPressed ? 'Hide' : 'Show'}"> 
    <f:ajax listener="#{bean.toggle()}" render="explanationWrapper showButton" /> 
</h:commandButton> 


<h:panelGroup id="explanationWrapper"> 
    <h:panelGroup id="explanation" rendered="#{bean.wasPressed}"> 
     <h:outputText value="something" /> 
    </h:panelGroup> 
</h:panelGroup> 

還看類似的問題

Can you update an h:outputLabel from a p:ajax listener?