12

I am using JSF2 and Java to build a web application. I want to build a form like the one at the picture below:

alt text

When somebody unchecks the checkbox the form should disappear: alt text

Here is an example with gwt.


So far, I tried some stuff with the <f:ajax> tag and an PropertyActionListener in the managedBean but it didn't work. Can somebody give me an example or at least some hints to accomplish my goal. I would be really thankfull

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Sven
  • 6,288
  • 24
  • 74
  • 116
  • lol, added this question and the first viewer rates it -1 without giving a comment -.- – Sven Sep 13 '10 at 07:47

3 Answers3

14

Use <f:ajax render="idOfPanelContainingInputFields"> in the checkbox and give the component containing the input fields a rendered attribute which depends on the checkbox's state. No need for another JS code clutter.

<h:form>
    <fieldset>
        <legend>
            <h:selectBooleanCheckbox binding="#{showUserInfo}">
                <f:ajax render="idOfPanelContainingTextBox" />
            </h:selectBooleanCheckbox>
            <h:outputText value="User information" />
        </legend>
        <h:panelGroup id="idOfPanelContainingTextBox" layout="block">
            <ui:fragment rendered="#{not empty showUserInfo.value and showUserInfo.value}">
                <p>
                    <h:outputLabel for="firstName" value="First name:" />
                    <h:inputText id="firstName" value="#{bean.user.firstName}" />
                </p>
            </ui:fragment>
        </h:panelGroup>
    </fieldset>
</h:form>

The above example binds the checkbox to the view, you can of course also bind it to a boolean bean property, you can then remove the not empty check from the rendered attribute.

            <h:selectBooleanCheckbox value="#{bean.showUserInfo}">

...

            <ui:fragment rendered="#{bean.showUserInfo}">

See also:

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
11

As suggested by amorfis, the idea of using Ajax here is not the best solution, as you will do a call to your server for a client-side manipulation.

The best solution is to use Javascript to hide your component(s). For example, if all your labels and input texts are nested in a <h:panelGrid> component, you can do that:

<script type="text/javascript">
    function hideOrShow(show) {
        // Get the panel using its ID
        var obj = document.getElementById("myForm:myPanel");
        if (show) {
            obj.style.display = "block";
        } else {
            obj.style.display = "none";
        }
    }
</script>

<h:form id="myForm">
    <h:selectBooleanCheckbox id="myCheckbox" onclick="hideOrShow(this.checked);"/>

    <h:panelGrid id="myPanel" columns="2">
        ...
    </h:panelGrid>
</h:form>
Romain Linsolas
  • 79,475
  • 49
  • 202
  • 273
  • @Sven Maybe the `this.checked` in the `` component is not working correctly. Need to check that and then edit my answer in consequence... – Romain Linsolas Sep 13 '10 at 09:13
  • @Sven I just tried this code snippet, and everything is working on my computer (I am using JSF 1.2, but nothing JSF-specific here). Could you update your question and show your code? – Romain Linsolas Sep 13 '10 at 09:27
  • Okay, works now. I missspelled an id -.-. Thank you very much for that – Sven Sep 13 '10 at 11:03
  • +1, If i could give you more than 1 vote on this post. I was also trying the same thing for so long, but I was not getting it. In this post I was trying other way as what @balusC described, but I guess that is for J2R4, but your solution worked for me. Thanks a lot. – Addicted May 26 '12 at 11:07
  • extremaly easy with this.checked. nice. – ZaoTaoBao Oct 09 '14 at 10:51
  • Even if I did not try @balusC solution, my beginner feeling is that it is easier to have this solution with javascript snippet in this simple example. Is there in stack overflow a good thread about when implementing in back-end code versus in front-end ? – sandwood Nov 29 '17 at 13:37
  • in case the default behaviour is that the checkbox is unchecked (and so the form should not be visible) , the solution for correct initialization is to add style="display: none" for the panelGrid – sandwood Nov 29 '17 at 14:20
0

You could do it by jQuery, or another JavaScript. It is also possible by <f:ajax>, but it connects to the server, which you don't need here.

By jQuery you can just hide the form, not changing the DOM. As far as I understand, it should be enough.

Post some .xhtml if you want more :)

amorfis
  • 15,390
  • 15
  • 77
  • 125