[SalesForce] Rendering section if checkbox is unchecked

Is it possible to render a section if checkbox is unchecked.

I tried this.

<apex:pageBlockSection columns="1" id="Test" >
  <apex:inputcheckbox value="{!test.Same_Address__c}" label="Same as Above" selected="True">
        <apex:actionSupport event="onchange" rerender="Test" />
    </apex:inputcheckbox>
    <apex:outputPanel rendered="{!test.Same_Address__c == false}"> <apex:inputField value="{!test.Contact_Surname__c}" />
    <apex:inputField value="{!test.Contact_First_Name__c}" />
    <apex:inputField value="{!test.Pos__c}" />
    <apex:inputField value="{!test.Addr__c}" />
 </apex:outputPanel>
</apex:pageBlockSection> 

Class code:-

        public with sharing class test{

        public Test__c test{get;set;}

            public Test(ApexPages.StandardController stdCtrl) {
            this.test=(Test__c)stdCtrl.getRecord();
           }

       }

Query:-
I am just not able to see label… but the input field are visible .. i want to hide that as well. This field should be visible only if checkbox is unchecked.

I have added the set of field in output panel and this output panel is rendered only if checkbox is unchecked. But not working as expected.

The checkbox should be by default set to true when page loads. The user can uncheck it if he wishes and see more fields.

Best Answer

<apex:pageBlockSection columns="1" id="Test" >
 <apex:inputcheckbox value="{!test.Same_Address__c}" label="Same as Above">//removed selected=true
    <apex:actionSupport event="onchange" rerender="Test" />
</apex:inputcheckbox>
  <apex:outputPanel rendered="{!NOT(test.Same_Address__c)}"> 
    <apex:inputField value="{!test.Contact_Surname__c}" />//Use labels as previous answer from Marcel says
    <apex:inputField value="{!test.Contact_First_Name__c}" />
    <apex:inputField value="{!test.Pos__c}" />
    <apex:inputField value="{!test.Addr__c}" />
 </apex:outputPanel>
</apex:pageBlockSection>


public with sharing class test{

    public Test__c test{get;set;}

        public Test(ApexPages.StandardController stdCtrl) {
        this.test=(Test__c)stdCtrl.getRecord();
        this.test=true;//observe the change in this line.Added this for proper behaviour on load 
       }

   }

Here is the working code and I have added comments where necessary to the code to help you. Just remove selected=true at first place.

Related Topic