Conditional Statement In Visualforce

conditionalif statementsalesforcevisualforce

I'm trying to display records on a page (not a form) via VisualForce based on a checkbox value.

For example, if checkbox "Active_c" is checked, I would like to display five subsequent fields. If checkbox "Active_c" is not checked, I would like to display nothing.

The only example I can find are display text in the output, however, they don't support displaying multiple field output. For example:

{! IF ( CONTAINS('salesforce.com','force.com'), 'Yep', 'Nah') }

Anyone have experience with a conditional?

Best Answer

Apex Controller:

public YourObject__c YourObject { get; set; }

public YourClass(){
    YourObject = [ Select Active__c, Field1__c, Field2__c From YourObject__c Limit 1 ];
}

Visualforce Page:

<apex:actionFunction name="showHideFields" reRender="myFields" />

<apex:inputField value="{!YourObject.Active_c}" onChange="showHideFields()"/>

<apex:outputPanel id="myFields">
    <apex:outputPanel rendered="{!YourObject.Active_c}">
        <apex:outputField value="YourObject.Field1__c" />
        <apex:outputField value="YourObject.Field2__c" />
    </apex:outputPanel>
</apex:outputPanel>

Another example. Without reRendering, just checking the value of the checkbox on the runtime:

<apex:outputPanel>
    <apex:outputField value="YourObject.Field1__c" rendered="{!YourObject.Active_c}" />
    <apex:outputField value="YourObject.Field2__c" rendered="{!YourObject.Active_c}" />
</apex:outputPanel>
Related Topic