You need to sign in to do that
Don't have an account?
Jon Keener
Message Edited by Jon Keener on 12-12-2007 11:14 AM
Use of the actionSupport tag with the "onchange" event
I've been running into some issues with the "onchange" event triggering so I've been working on a simplified version of a form based on another thread (Toggle display based on Picklist selection ) in the Visualforce forum. I have run into a number of issues and questions surrounding the code below.
General Description: The code, as it is right now, does not work as intended.
The goal of this code is that:
When the LeadSource dropdown is changed to "Web", the "Details" outputPanel should become visible. Also, when an account is added, the "Details2" outputPanel should appear. if the LeadSource is changed to something other than "Web", the "Details" outputPanel should disappear, and if the account is removed, the "Details2" outputPanel should disappear.
1. Currently, if both InputFields are on the form, neither of the actionSupports appear to be firing correctly. If the LeadSource is changed to "Web", the "Details" outputPanel does not appear.
2. If I comment out the section for Test 2, between the comments <!-- Test 2 - Using Account --> and <!-- End of Test 2 - Using Account field --> The LeadSource picklist works correctly in Firefox.
3. When number 2 is done, and things are working correctly in Firefox, they do not work in IE6. (I haven't tried it in IE7 yet)
4. I have not gotten Test 2 to work successfully, even when commenting out Test 1. My specific concern about Test 2 is when does the "onchange" event fire for a Lookup Field? I am hoping that it is when the field value changes, and not when I leave to field. In my real code, I am trying to enable/disable command buttons based on this being populated or not populated.
Thanks for any assistance!
Jon Keener
The Page is:
Code:
<apex:page id="step4" controller="testpage" tabstyle="Contact"> <apex:form> <apex:pageBlock title="test"> <!-- Test 1 - Using Picklist --> <apex:pageBlockSection title="test 1"> <apex:panelGrid columns="2" columnClasses="labelCol,dataCol"> <apex:outputLabel value="Picklist 1" for="picklist1" /> <apex:outputPanel> <apex:actionSupport event="onchange" action="{!ToggleTrue}" rerender="details" /> <apex:inputField id="picklist1" value="{!testing.LeadSource}" /> </apex:outputPanel> </apex:panelGrid> </apex:pageBlockSection> <!-- End of Test 1 - Using Picklist --> <!-- Test 2 - Using Account --> <apex:pageBlockSection title="test 2"> <apex:panelGrid columns="2" columnClasses="labelCol,dataCol"> <apex:outputLabel value="Parent Account" for="Acct" /> <apex:outputPanel> <apex:actionSupport event="onchange" action="{!ToggleTrue2}" rerender="details2" /> <apex:inputField id="Acct" value="{!testing.Account}" /> </apex:outputPanel> </apex:panelGrid> </apex:pageBlockSection> <!-- End of Test 2 - Using Account field --> <apex:outputPanel id="details"> <apex:pageBlockSection title="Details" rendered="{!hideshow}"> <apex:panelGrid columns="2" columnClasses="labelCol,dataCol"> <apex:outputLabel value="Name" for="personname" /> <apex:inputField id="personname" value="{!testing.Ownerid}" required="false" /> </apex:panelGrid> </apex:pageBlockSection> </apex:outputPanel> <apex:outputPanel id="details2"> <apex:pageBlockSection title="Details 2" rendered="{!hideshow2}"> <apex:panelGrid columns="2" columnClasses="labelCol,dataCol"> <apex:outputLabel value="Email" for="Email" /> <apex:inputField id="Email" value="{!testing.Email}" required="false" /> </apex:panelGrid> </apex:pageBlockSection> </apex:outputPanel> </apex:pageBlock> </apex:form> </apex:page>
The Controller Code is:
Code:
public class testpage { Contact testing; boolean testval = false; boolean testval2 = false; public String ToggleTrue() { if ( testing.LeadSource =='Web') { testval = true; } else { testval = false; } return null; } public Contact gettesting() { if (testing == null) testing = new Contact(); return testing; } public Boolean gethideshow() { return testval; } public String ToggleTrue2() { if ( testing.Account != null) { testval2 = true; } else { testval2 = false; } return null; } public Boolean gethideshow2() { return testval2; } }
Message Edited by Jon Keener on 12-12-2007 11:14 AM
- <apex:actionSupport> is a child helper component that needs to be a direct child of the component you are trying to hook which means your placement below is wiring up the onchange event handler for the <apex:outputPanel> and not the <apex:inputField> which is not what you want. BTW this "works" in FF because of differences in default event handling/bubbling but it is still not going to be what you want.
- <apex:inputField> in Winter '08 has very limited support for <apex:actionSupport> - only onblur can currently be wired up (yes I know how weird/arbitrary that seems :-). We have already fixed that in Spring' 08 and will also be introducing <apex:actionFunction> that will let you really take total control of things.
Message Edited by dchasman on 12-12-2007 11:37 AM
I moved the actionSupports below the InputFields and changed the event to onblur. Overall, I'm seeing similar results as I had previously.
1. If both Test 1 and Test 2 are uncommented, neither Test 1 or Test 2 work correctly in Firefox or IE.
2. If Test 2 is commented out, Test 1 works correctly (slightly different due to using onchange vs. onblur) in Firefox. Test 1 still does not work correctly in IE6.
3. Still not able to get Test 2 to work at all in IE or Firefox, with or without Test 1 commented out. (This could be an issue in my controller code, if it's firing, but the code is pretty simple, so at the moment, I'm assuming it is not firing)
If you think I should wait for Spring 08 release for this, let me know. What I'm trying to enable in my real app is the following:
This wizard will replace the "New" functionality, and may start with any of the three lookup fields on top autopopulated (based on parameters passed in). If the Account is populated, the "Use Account Address" button is enabled. If the Contact is populated, then the "Use Contact Address is populated.". Otherwise, the user can click "Manually Enter Address" to bring up and edit form for the address fields, vs. the display fields shown in the screenshot above.
Everything works fine, except for capturing the onchange events on Account and Contact. When either of these change, I need to make sure the buttons reflect the correct "disabled" flag, and also I need to make sure I refresh my internal account and contact object variables with a new query of data so that if a user changes the account or contact, the variables are updated so that the user clicking the buttons gets the correct address. (I probably could perform a refresh of the account and contact object variables in the button press)
If I can't get these to work at the moment, I may just make the fields read only and force users to start at the correct point so data is populated correctly, and if they don't start from the right place, then they will need to restart the wizard.
Also, I just tried out test 1 on Spring '08 (internal test system) using onchange and its working nicely there.
Message Edited by dchasman on 12-12-2007 12:19 PM
Message Edited by Jon Keener on 12-12-2007 12:31 PM
<!-- Test 2 - Using Account -->
<apex:pageBlockSection title="test 2">
<apex:inputField id="Acct" value="{!testing.Accountid}">
<apex:actionSupport event="onblur" action="{!ToggleTrue2}" rerender="details2" />
</apex:inputField>
</apex:pageBlockSection>
<!-- End of Test 2 - Using Account field -->
HI dchasman,
I am trying to hide pageBlockSection in my VisualForce page based on the value selected by the user, an I have added the actionSupport to refresh the pageBlockSection so that based on the values the BlockSection hides and unhide.
Whenever I change the value in the field the pageBlockSections gets refreshed but it does't hide and unhide because the field value doesn't change. is that I am doing some thing wrong in setting/getting the current value on my VF page?
<apex:page StandardController="Lead" extensions="LeadPageBeforeConvert" tabstyle="lead" >
<apex:sectionHeader title="Convert Lead"subtitle="{!lead.owner.name}"/>
Leads can be converted to Accounts, Contacts, Opportunities, and followup tasks.
<br>You should only convert a lead once you have identified it as qualified.</br>
<br>After this lead has been converted, it can no longer be viewed or edited as a lead, but can be viewed in lead reports.</br>
<script type="text/javascript">
function hideMPAN(input, Compid) {
if(input){
document.getElementById(Compid).style.display="block";
}
else {
document.getElementById(Compid).style.display="none";
}
}
</script>
<apex:form >
<apex:pageBlock title="Bussiness Advisory Services Lead" id="pageBlockItm" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton action="{!Save}" value="Save"/>
<apex:commandButton action="{!SaveNew}" value="SaveNew"/>
<apex:commandButton action="{!Cancel}" value="Cancel"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Lead Information" columns="3">
</apex:pageBlockSection>
<apex:pageBlockSection title="Lead Details" columns="3">
</apex:pageBlockSection>
<apex:pageBlockSection title="Current Spend vs Renewal Spend(Electricity)" columns="3" rendered="{!OR(LeadRec.Related_Utility__c == 'Gas & Electricity', LeadRec.Related_Utility__c == 'Electricity')}">
</apex:pageBlock>
<apex:outputPanel id="detail">
<apex:detail subject="{!$CurrentPage.parameters.theList}" relatedList="true" title="true"/>
</apex:outputPanel>
</apex:form>
</apex:page>
public class LeadPageBeforeConvert {
public String LeadName {get; set;}
if(LeadRec.Site_Same_As_Bussiness_Address__c == true){
}
public List<SelectOption> getRelatedUtilities() {
/* Loop through the feature_category__c records creating a selectOption
public String getLeadName() {
}
if(LeadId == null){
}
public PageReference save(){
PageReference pageRef = new PageReference('/' + LeadRec.Id);
public PageReference saveNew(){
try {
public void LeadValidations(){
if(LeadRec.Reaction__c == 'Alternative Broker' && LeadRec.Alternative_Broker_Name__c ==NULL){
if(LeadRec.RecordTypeId == '012200000008r2P' && !((LeadRec.Annual_Spend__c == '£0 - £1000') ||
public PageReference convert() {
return ConvertPage;
}
/* Construct the standard controller for quote. */
/* Switch to runtime context */
/* Construct the quoteExt class */
/* Switch back to test context */
/* Verify the navigation outcome is as expected */
}
}