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
Rick MacGuiganRick MacGuigan 

Rerender & actionSupport problem with section collapsing

This code renders the Other_Policy_term__c visible if the user chooses 'other' from a picklist called Policy_Term__c . However, I have a script function that collapses the page block section on load. This actionSupport event causes the pageblock section to collapse . How can I update the field leaving the section expanded ? 

<apex:page standardController="Auto_Audit_Sample_Policy__c" readOnly="false" > 
<apex:variable var="a" value="{!Auto_Audit_Sample_Policy__c}" /> 
<apex:variable var="PolicyTerm" value="{!a.Policy_Term__c}" />

<apex:form id="form1">
<apex:pageBlock id="block1" title="Auto Policy" >
    <apex:outputPanel id="ajaxrequest" >
    <apex:pageBlockSection columns="1" id="section1" title="Policy Information" rendered="{!PolicyInformation=true}">

        <apex:inputField value="{!a.Policy_Term__c}" >
            <apex:actionSupport event="onchange" reRender="section1" />
        </apex:inputField>
        <apex:inputField id="fieldtest" value="{!a.Other_Policy_term__c}" rendered="{!PolicyTerm="other"}" />
 
    <script>twistSection(document.getElementById("{!$Component.section1}").childNodes[0].childNodes[0]); </script>
</apex:pageBlockSection>
</apex:outputPanel> 
</apex:pageBlock>
</apex:form>
</apex:page>
Best Answer chosen by Rick MacGuigan
cmoylecmoyle
Your script is contained within the pageBlockSection that is getting rerendered. This script is going to be run every time you change the Policy_Term__c field. If its just toggling the collapse on that section then you'll see it toggle every time this field is changed. If you want the script only to be run when the page is loaded, then you need to pull the script outside your pageBlockSection like so:
 
<apex:page standardController="Auto_Audit_Sample_Policy__c" readOnly="false" > 
<apex:variable var="a" value="{!Auto_Audit_Sample_Policy__c}" /> 
<apex:variable var="PolicyTerm" value="{!a.Policy_Term__c}" />

	<apex:form id="form1">
		<apex:pageBlock id="block1" title="Auto Policy" >
			<apex:outputPanel id="ajaxrequest" >
				<apex:pageBlockSection columns="1" id="section1" title="Policy Information" rendered="{!PolicyInformation=true}">
					<apex:inputField value="{!a.Policy_Term__c}" >
						<apex:actionSupport event="onchange" reRender="section1" />
					</apex:inputField>
					<apex:inputField id="fieldtest" value="{!a.Other_Policy_term__c}" rendered="{!PolicyTerm="other"}" />
				</apex:pageBlockSection>
			
				<script>twistSection(document.getElementById("{!$Component.section1}").childNodes[0].childNodes[0]); </script>
			</apex:outputPanel> 
		</apex:pageBlock>
	</apex:form>
</apex:page>

 

All Answers

cmoylecmoyle
Your script is contained within the pageBlockSection that is getting rerendered. This script is going to be run every time you change the Policy_Term__c field. If its just toggling the collapse on that section then you'll see it toggle every time this field is changed. If you want the script only to be run when the page is loaded, then you need to pull the script outside your pageBlockSection like so:
 
<apex:page standardController="Auto_Audit_Sample_Policy__c" readOnly="false" > 
<apex:variable var="a" value="{!Auto_Audit_Sample_Policy__c}" /> 
<apex:variable var="PolicyTerm" value="{!a.Policy_Term__c}" />

	<apex:form id="form1">
		<apex:pageBlock id="block1" title="Auto Policy" >
			<apex:outputPanel id="ajaxrequest" >
				<apex:pageBlockSection columns="1" id="section1" title="Policy Information" rendered="{!PolicyInformation=true}">
					<apex:inputField value="{!a.Policy_Term__c}" >
						<apex:actionSupport event="onchange" reRender="section1" />
					</apex:inputField>
					<apex:inputField id="fieldtest" value="{!a.Other_Policy_term__c}" rendered="{!PolicyTerm="other"}" />
				</apex:pageBlockSection>
			
				<script>twistSection(document.getElementById("{!$Component.section1}").childNodes[0].childNodes[0]); </script>
			</apex:outputPanel> 
		</apex:pageBlock>
	</apex:form>
</apex:page>

 
This was selected as the best answer
Rick MacGuiganRick MacGuigan
Great.... works like I need it to . Too easy. Although it does make a round trip to the server (controller.) If I have 25 fields in this pageblock where a few are tied to an action can I just specify an actionReion around just those inputfields ? Thinking this would improve returns from the server by sending over only a subset of data to be updated ?
Rick MacGuiganRick MacGuigan

Is there a way around loosing the field label when wrapping the actionRegion around the inputField ?

<apex:page standardController="Auto_Audit_Sample_Policy__c" readOnly="false" >
<apex:variable var="a" value="{!Auto_Audit_Sample_Policy__c}" />
<apex:variable var="PolicyTerm" value="{!a.Policy_Term__c}" />

<apex:form id="form1">
<apex:pageBlock id="block1" title="Auto Policy" >
 <apex:outputPanel id="ajaxrequest" >
 <apex:pageBlockSection columns="1" id="section1" title="Policy Information" rendered="{!PolicyInformation=true}">

<apex:actionRegion >
  <apex:inputField value="{!a.Policy_Term__c}" >
   <apex:actionSupport event="onchange" reRender="section1" />
  </apex:inputField>
  <apex:inputField id="fieldtest" value="{!a.Other_Policy_term__c}" rendered="{!PolicyTerm="other"}" />
</apex:actionRegion >

</apex:pageBlockSection>

 <script>twistSection(document.getElementById("{!$Component.section1}").childNodes[0].childNodes[0]); </script>

</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>