2

I have a list of Property_Rate__c records in an Apex:repeat function. For every record in the list, a checkbox is created. The value attribute of each checkbox is the ID of the Property_Rate__c record. I am wondering: When one of the checkboxes is checked, how to extract the ID value into a variable for later use?

<apex:repeat value="{!property_rate_list}" var="propertyRate" id="propertyRateList">

    <input type="checkbox" value="{!propertyRate.Id}" class="dateCheckbox" onchange="verifyChecked($(this));" />

</apex:repeat>

verfiyChecked Function

$ = jQuery.noConflict()   
    function verifyChecked(checkbox)
    {
        $('.dateCheckbox:checked').prop('checked', false); // clear all checkboxes
        $(checkbox).prop('checked', !$(checkbox).prop('checked')); // toggle
        //changeDate($(checkbox).val());
    }
  • I think you need that inside javascript isn't it ? if yes you can easily do that using .prop method in javascript. – Himanshu Nov 9 '15 at 17:51
  • I've used javascript to toggle the checkboxes so that only one is selected at once. Can you also save the value into a variable using js? @Himanshu – Alex Brigham Nov 9 '15 at 17:53
  • you can modify your onchange function as onchange ="verifyChecked($(this),'{!propertyRate.Id}');" function and get value inside javascript. – Himanshu Nov 9 '15 at 17:57
  • So that's what I'm wondering how to do. How do I get the value from the checkbox using javascript and save that into an apex ID variable? @Himanshu – Alex Brigham Nov 9 '15 at 18:01
  • I assume with my last comment you can get propertyRate id in javascript and from javascript you can use apex:actionfunction to set variable in apex code. understood ? – Himanshu Nov 9 '15 at 18:04
2

We are going to use actionfucntion to pass value from visaulforce to controller.

I have defined inline comments. I hope it will help you to understand the code.

HTML update

<input type="checkbox" value="{!propertyRate.Id}" 
class="dateCheckbox" onchange="return verifyChecked($(this),'{!propertyRate.Id}');" />

Javascript update

function verifyChecked(checkbox,recordid)
{

    $('.dateCheckbox:checked').prop('checked', false); // clear all property 
     checkboxes
    $(checkbox).prop('checked', !$(checkbox).prop('checked')); // toggle
    //changeDate($(checkbox).val());
    //call action function.
    callmethod(recordid);
    return false;
}

Add following code before apex:form closing tag

 <!-- This code will call actionfunction and pass the value to controller -->
 <apex:actionfunction name="callmethod" action="{!setProperty}" status="updatestatus">
     <apex:param name="propertyid" value=""/>
</apex:actionfunction>
<apex:actionstatus id="updatestatus" starttext="updating.." stoptext=""/>

Controller method

public void setProperty()
{
     //read propertyid value from page.
     String propertyid = ApexPages.currentPage().getParameters().get('propertyid');

     //user this propertyId for your logic.
}
  • Thank you, that is broken down very clearly. I have one question though: When you use callmethod(recordId) in javascript, this calls the apex:actionfunction. Within the apex:param, the name is "propertyId", and the value is blank. How does the controller method end up getting the ID when it "gets" the name? Should it not get blank ("")? – Alex Brigham Nov 9 '15 at 18:40
  • no it will not be blank. when I call callmethod(recordid) it map the value with apex:param propertyid. :) this is the way it works :) – Himanshu Nov 9 '15 at 18:44
  • Interesting...You have been very helpful. Thank you! Would you have any idea why when I try System.debug(propertyId) at the end of the controller method, I am getting null instead of the ID? – Alex Brigham Nov 9 '15 at 18:57
  • try to alert recordid value prior to callmethod in js to make sure you are getting property id value. – Himanshu Nov 9 '15 at 19:01
  • Yep that is working fine! – Alex Brigham Nov 9 '15 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.