问题描述:

I have a problem with these two commandButton : Join and Leave.

I want to hide Join if I click on leave and vice-versa.

When I put ajax on false, there is no problem (but all the page is refresh and I don't find this optimal).

But when ajax attribut is on true with specific updating (cf comment in the code), the rendering is good but the new button whitch appear become inactive. If I click on it, nothing happens (well it's seems the actionListener trigger but the view is not refreshed, I have to manual refresh to see the difference)

Thanks for reading.

<h:form id="formWaitingList" rendered="#{connexion.connected}" >

<p:commandButton id="Join"

actionListener = "#{connexion.joinWaitingList()}"

rendered="#{!connexion.waiting}"

ajax="false"

<!-- ajax="true"

update="Join,Leave"-->

value="Join"/>

<p:commandButton id="Leave"

value="Leave"

ajax="false"

<!-- ajax="true"

udpate="Join,Leave"-->

rendered="#{connexion.waiting}"

actionListener ="#{connexion.leaveWaitingList()}" />

</h:form>

网友答案:

It seems that you're not entirely familiar with HTML/JavaScript. You know, JSF is basically a HTML/JavaScript(/CSS) code generator. Ajax updating works basically like this in JavaScript:

  • After sending the ajax request to JSF via XMLHttpRequest, retrieve a XML response which contains all elements which needs to be updated along with their client IDs.
  • For every to-be-updated element, use document.getElementById(clientId) to find it in the current HTML DOM tree.
  • Replace that element by new element as specified in ajax XML response.

However, if a JSF component has not generated its HTML representation because of rendered="false", then there's nothing in the HTML DOM tree which can be found and replaced. That totally explains the symptoms you're "seeing".

You basically need to wrap conditionally rendered JSF components in a component whose HTML representation is always rendered and then reference it instead in the ajax update.

For example,

<h:form>
    ...

    <h:panelGroup id="buttons">
         <p:commandButton ... update="buttons" rendered="#{condition}" />
         <p:commandButton ... update="buttons" rendered="#{not condition}" />
    </h:panelGroup>
</h:form>

See also:

  • Why do I need to nest a component with rendered="#{some}" in another component when I want to ajax-update it?
相关阅读:
Top