+ Start a Discussion
thomas_sthomas_s 

Problem calling javascript function in inputCheckBox's onchange with params

Hi,

 

I'd like to call a script function with the checkbox and the ID of the current record as parameters in a pageBlockTable in an inputCheckBox's onchange event.

 

If I use 'normal' parameters, my function is getting called, but if I fill in '{!con.Id}' as second parameter, a SUBMIT call is generated and my function is not called.

 

Next thing I'd like to know is how I can write back the changed checkbox value to the db. I guees I have to write a supporting save() function in my controller extension.

 

Can someone help me?

 

Here's an excerpt from my code:

 

<script>
function rerenderMap(cb, id)
{
// ... do something with cb.checked on record with id
}
</script>

<apex:pageBlock title="Kontakte" id="table">
  <apex:form >
     <apex:pageBlockTable value="{!account.Contacts}" var="con">
       <apex:column value="{!con.Name}"/>
       <apex:column value="{!con.MailingStreet}"/>
       <apex:column value="{!con.MailingCity}"/>
       <apex:column value="{!con.Phone}"/>
       <apex:column headerValue="{!$ObjectType.contact.fields.Visit_planned__c.label}">
         <apex:inputCheckbox value="{!con.Visit_planned__c}" onchange="rerenderMap(this, '{!con.Id}')">
           <apex:actionSupport event="onchange" rerender="table"/>
         </apex:inputCheckbox>
       </apex:column>
    </apex:pageBlockTable>
  </apex:form>
</apex:pageBlock>

 

Thanks,

 

Thomas

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

 

Replace your event onchange with onclick ,it will work

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

Replace your event onchange with onclick ,it will work

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
thomas_sthomas_s

Hi Ankit,

 

cool, that works. But why? Is it because onchange has the actionSupport tag?

 

Regarding the second part of my question: I wrote a save function in my extension, how can I call it in the onclick or onchange handler?

 

Thomas

 

Gunners_23Gunners_23

For that define a ActionFunction and incase of a event such as onClick call ActionFunction and in ActionFunction you can call

 

any method in your controller or extensions

thomas_sthomas_s

I did this.

At the end of my OnClick handler I added the call to my save() ActionFunction:

What happens is this:

<apex:actionFunction name="save" action="{!save}" rerender="table"/>

<apex:column value="{!con.Visit_planned__c}"/>
<apex:column headerValue="{!$ObjectType.contact.fields.Visit_planned__c.label}">
   <apex:inputCheckbox value="{!con.Visit_planned__c}" onclick="rerenderMap(this, '{!con.Id}')">
      <apex:actionSupport event="onchange" rerender="table"/>
   </apex:inputCheckbox>
</apex:column>

 

I uncheck my checkbox, my JS OnClick handler is called and display things correctly (in a map). When the save function is called at the end of ma handler, the checkbox is rechecked again!

To debug the situation I added a column with the value of my flag like this:

If I comment out my save function and I uncheck my checkbox, the corresponding column is changed some time later (about 1 sec).

If I reenable the save function this is almost not visible, only the checkbox is rechecked after 1 sec.

 

Can it be that my save function is called before the postback is completed and the old value is rewritten to the db?

 

Thomas