function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sflearnsflearn 

actionSupport selectOptions

my controller is pulling a list of app names(which are picklist values on a custom field) and displaying it on the page. Now the picklist values show as checkboxes fine but when I select Cool App 1 check box on the page, the input field doesn't show.....perhaps I need a getter or setter on the selectedApp in the controller?

 

 

    public List<SelectOption> getApps(){
        if(apps== null){
            apps= new List<SelectOption>();
            Schema.DescribeFieldResult appField = Schema.sObjectType.Lead.fields.Applications__c;
            Schema.PicklistEntry [] values = appField.getPickListValues();
            for(Schema.PicklistEntry value: values){
                apps.add(new SelectOption(value.getValue(), value.getLabel()));
            }
        }
        return apps;    
    }

              <apex:panelGrid columns="2">
                    <apex:outputLabel for="apps"><span class="required">* </span>My Apps:</apex:outputLabel>
                    <apex:outputPanel >
                        <apex:actionRegion >
                        <apex:selectCheckBoxes value="{!selectedApps}" required="true">
                            <apex:selectOptions value="{!apps}"/>
                             <apex:actionSupport event="onchange" rerender="appTypeDiv"/>
                        </apex:selectCheckBoxes>
                       </apex:actionRegion>
                    </apex:outputPanel>
                </apex:panelGrid>

 

 

                <apex:panelGrid columns="2">   

                <!-- input field code for Cool App2-->

                <!-- input field code for Cool App3-->

                    <apex:outputPanel id="appTypeDiv" rendered="{!apps= 'Cool App 1'}">
                     <apex:outputLabel for="appType"><span class="required">* </span> App Type:</apex:outputLabel>
                     <apex:inputField value="{!Lead.App_Type__c}" id="appType" required="true"/>
                    </apex:outputPanel>

                  </apex:panelGrid>
   

souvik9086souvik9086

You can do like this. do the rendered and rerender in separate outputpanel as it will not work in the same.

 

<apex:panelGrid columns="2">
                    <apex:outputLabel for="apps"><span class="required">* </span>My Apps:</apex:outputLabel>
                    <apex:outputPanel >
                        <apex:actionRegion >
                        <apex:selectCheckBoxes value="{!selectedApps}" required="true">
                            <apex:selectOptions value="{!apps}"/>
                             <apex:actionSupport event="onchange" rerender="appTypeDiv"/>
                        </apex:selectCheckBoxes>
                       </apex:actionRegion>
                    </apex:outputPanel>
                </apex:panelGrid>

 

 

                <apex:panelGrid columns="2">   

                <!-- input field code for Cool App2-->

                <!-- input field code for Cool App3-->

<apex:outputPanel id="appTypeDiv" >                   

<apex:outputPanel rendered="{!products = 'Cool App 1'}">
                     <apex:outputLabel for="appType"><span class="required">* </span> App Type:</apex:outputLabel>
                     <apex:inputField value="{!Lead.App_Type__c}" id="appType" required="true"/>
                    </apex:outputPanel>

</apex:outputPanel>

                  </apex:panelGrid>

 

And also in the controller make selectedApp {get;set;}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

Avidev9Avidev9

Well just adding few info why its not working!

In VF you cannot rerender a component which is not visible(rendered ="false") in page. Initially "appTypeDiv" is not visible because the condition doesnt match, SO to make this component visible we will have to rerender the parent component(which is visible). In our case its the "panelGrid"

 

So the code should be

 

<apex:actionRegion >
                        <apex:selectCheckBoxes value="{!selectedApps}" required="true">
                            <apex:selectOptions value="{!apps}"/>
                             <apex:actionSupport event="onchange" rerender="appTypePanel"/>
                        </apex:selectCheckBoxes>
                       </apex:actionRegion>

 

 

 

<apex:panelGrid columns="2" id="appTypePanel">   
                <!-- input field code for Cool App2-->
                <!-- input field code for Cool App3-->
                    <apex:outputPanel id="appTypeDiv" rendered="{!products = 'Cool App 1'}">
                     <apex:outputLabel for="appType"><span class="required">* </span> App Type:</apex:outputLabel>
                     <apex:inputField value="{!Lead.App_Type__c}" id="appType" required="true"/>
                    </apex:outputPanel>
                  </apex:panelGrid>

 

sflearnsflearn

Ok ..my code didn't work but there is just too much to show here so here is a similar example that is same conceot from the sfdc docs but how come my <apex:outputText doesn't display after I select US checkbox?  i think if i can get this to work i can do the same type of logic to my other code

 

public class sampleCon {
    public String[] countries{get;set;}
    public PageReference test() {
        return null;
    }

    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('US','US'));
        options.add(new SelectOption('CANADA','Canada'));
        options.add(new SelectOption('MEXICO','Mexico'));

        return options;
    }

}

 

 

<apex:page controller="sampleCon">
    <apex:form >
    <apex:actionRegion >
        <apex:selectCheckboxes value="{!countries}">
            <apex:selectOptions value="{!items}"/>
             <apex:actionSupport event="onchange"  reRender="myDiv"/>
        </apex:selectCheckboxes><br/>
    </apex:actionRegion>
        </apex:form>
        <apex:outputPanel id="myDiv">
            <apex:outputPanel rendered="{!countries= 'US'}">
               <apex:outputText value="The unformatted time right now is: {!NOW()}" />
            </apex:outputPanel>
        </apex:outputPanel>      
</apex:page>

sflearnsflearn

I wil try this but the thing is, i only want to show or hide this field below if  one of the checkboxes is selected but the 2 other fields should always display. In that case, how should i do it since th panel grid is still there

                     <apex:inputField value="{!Lead.App_Type__c}" id="appType" required="true"/>
sflearnsflearn

also can you see the code in the country example..that one will be easier

sflearnsflearn

Perhaps because if i select the checkbox it returns a boolean and not a string?

Avidev9Avidev9

Well wrongly used the operator here

 

  <apex:outputPanel rendered="{!countries = 'US'}">

 It should be

 

  <apex:outputPanel rendered="{!countries == 'US'}">

 

sflearnsflearn

still didn't work - here is the exact code - u can paste it into your browser. When US is selected it should show the output text automatically and whern it is unselected, it should hide the output text.  If any other countries are selected then it should still show the output text as log as US is still selected

 

<apex:page controller="sampleCon">
    <apex:form >
    <apex:actionRegion >
        <apex:selectCheckboxes value="{!countries}">
            <apex:selectOptions value="{!items}"/>
             <apex:actionSupport event="onchange"  reRender="myDiv"/>
        </apex:selectCheckboxes><br/>
    </apex:actionRegion>
        </apex:form>
        <apex:outputPanel id="myDiv">
            <apex:outputPanel rendered="{!countries== 'US'}">
               <apex:outputText value="The unformatted time right now is: {!NOW()}" />
            </apex:outputPanel>
        </apex:outputPanel>      
</apex:page>

 

 

public class sampleCon {
    public String[] countries{get;set;}
    public PageReference test() {
        return null;
    }

    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('US','US'));
        options.add(new SelectOption('CANADA','Canada'));
        options.add(new SelectOption('MEXICO','Mexico'));

        return options;
    }

}

Avidev9Avidev9

Countries should be string and not an array

 

 
public class sampleCon {
    public String countries{get;set;}
    public PageReference test() {
        return null;
    }

    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('US','US'));
        options.add(new SelectOption('CANADA','Canada'));
        options.add(new SelectOption('MEXICO','Mexico'));

        return options;
    }

}