You need to sign in to do that
Don't have an account?
AussieBattler
Toggle display based on Picklist selection
Hi. Perhaps I am over complicating this. I hope it is something simple I have missed.
Basically, I have a picklist on a page I have created and when the user changes the selection on the picklist I want to either show or hide parts of the screen (using ajax). My code below almost works (i.e.: the javascript function works but for some reason the variable I am storing the current state in appears to lose its value).
I have tried this a variety of ways but it always ends up with the same problem - I can see the value being assigned to the variable testval but when I want to render the section of the screen and make a call to get the testval value, the value has changed to being true all the time.
Can anybody assist?
Basically, I have a picklist on a page I have created and when the user changes the selection on the picklist I want to either show or hide parts of the screen (using ajax). My code below almost works (i.e.: the javascript function works but for some reason the variable I am storing the current state in appears to lose its value).
I have tried this a variety of ways but it always ends up with the same problem - I can see the value being assigned to the variable testval but when I want to render the section of the screen and make a call to get the testval value, the value has changed to being true all the time.
Can anybody assist?
Code:
<apex:page id="step4" controller="testpage" tabstyle="Enquiry__c">
<apex:form>
<script>
function currentval(id) {
var newval = document.getElementById(id).value;
switch (newval) {
case "1" :
var chgVal = '{!ToggleFalse}';
break;
default :
var chgVal = '{!ToggleTrue}';
break;
}
}
</script>
<apex:pageBlock title="test">
<apex:pageBlockSection title="test">
<apex:panelGrid columns="2" columnClasses="labelCol,dataCol">
<apex:outputLabel value="Picklist 1" for="picklist1"></apex:outputLabel>
<apex:inputField id="picklist1" value="{! testing.xyz__c}"></apex:inputField>
<apex:actionSupport event="onchange" onsubmit="currentval('{!$Component.picklist1}')" rerender="details"></apex:actionSupport>
</apex:panelGrid>
</apex:pageBlockSection>
<apex:outputPanel id="details">
<apex:pageBlockSection title="Details" rendered="{! hideshow}">
<apex:panelGrid columns="2" columnClasses="labelCol,dataCol">
<apex:outputLabel value="Name" for="personname"></apex:outputLabel>
<apex:inputField id="personname" value="{! testing.abcd__c}"></apex:inputField>
</apex:panelGrid>
</apex:pageBlockSection>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>
Code:
public class testpage {
testing123__c testing;
boolean testval = false;
public String getToggleTrue() {
testval = true;
return null;
}
public String getToggleFalse() {
testval = false;
return null;
}
public testing123__c gettesting() {
if (testing == null) testing = new testing123__c();
return testing;
}
public Boolean gethideshow() {
return testval;
}
}
So, you must put your getter/setter inside a component (
Therefore your actionSupport component should have an Action= and that controller action will be called to toggle the visibility flag. Then when the detail is re-rendered it can call the getter to determine the hideshow value.
Do you have any suggestions on this?
Many thanks for the response Ron.
i changed your code to work with the Account object since i didn't have your custom object in my developer org.
I hope that makes sense. Thanks again.
Here is the slightly amended code and the controller code remains the same as in your example.
<apex:form>
<apex:actionFunction name="myActionFunction" action="{!theActionIWantToInvoke}"/>
</apex:form>
<script>
myActionFunction();
</script>
Message Edited by dchasman on 12-10-2007 08:23 AM
In regards to your example, is that correct or should it be as amended below (based on the <apex:actionSupport> component (where the action attribute calls the function and event determines which event executes the function).
<apex:form>
<apex:actionFunction name="myActionFunction" action="{!theActionIWantToInvoke}"/>
</apex:form>
<script>
theActionIWantToInvoke();
</script>
apex:actionFunction does not perform any event wiring - it basically generates a javascript stub that makes it easy to invoke the action method from any block of javascript (event handlers or whatever - totally up to you).