0

I have custom controller and I pass data from VF to controller and it works. However, when I try to use this variable in controller at VF, I cannot.

Why is this happening? VF Page

<apex:form>
            <apex:actionFunction name="setCenter" action="{!setParam}" reRender="resultPanel" status="myStatus">
                <apex:param name="firstparam" assignTo="{!currentCenter}" value=""/>
            </apex:actionFunction>
        </apex:form>
        <script> 
            $(document).ready(function(){
                $(".btn_none").click(function(){
                    setCenter($(this).attr("title"));
                    alert('{!currentCenter}');  // Cannot get Value.      
            });
        </script>

Controller

public String currentCenter {get; set;}
public PageReference setParam(){
    PageReference pg;
    System.debug('setParam()' + currentCenter); // Debug get value successfully.

    return null;
}

Add.

<div class="btn_none" id="center" title="NYC">
     NYC
</div>
5
  • 1
    When you click this button .btn_none. Did you rerender the JS section? Dec 5, 2018 at 9:13
  • I cannot understand what you mean. Its, in document.ready and there is no other code If you have any clue, please share with me Dec 5, 2018 at 9:21
  • $(".btn_none").click on click event you are displaying alert. So you must have a button. Wher is resultPanel? Dec 5, 2018 at 9:27
  • I updated question, and there is no resultPanel. I didn't use reRender attr. Dec 5, 2018 at 9:32
  • You need to use rerender here to get the updated value. Check the answer. Dec 5, 2018 at 9:38

1 Answer 1

1

You need to rerender the form

you need to use oncomplete="onCompleteHandler()" event of action function. onCompleteHandler is a JS function inside you can have alert statement alert('{!currentCenter}')

like

<apex:actionFunction name="setCenter" action="{!setParam}" reRender="myform" status="myStatus" oncomplete="onCompleteHandler()">
        <apex:param name="firstparam" assignTo="{!currentCenter}" value=""/>
    </apex:actionFunction>

Put your script in the form. Give some id to form, and rerender that form by putting its id in reRender attribute of action function. See Below Code

    <apex:form id="myform">
    <!-- ... -->

    <apex:actionFunction name="setCenter" action="{!setParam}" reRender="myform" status="myStatus">
        <apex:param name="firstparam" assignTo="{!currentCenter}" value=""/>
    </apex:actionFunction>

Also If you have put your script in any outputpanel you can rerender that specifically by giving id of that particular outputpanel. In that case no need to rerender whole form.

9
  • Thanks Rahul. I tried what you suggested and still it doesn't work. I just use form for warping actionFunction Only. I don't need any rendering change. I just want to see value alert is poped well from Controller. For doing this, I need to consider reRender attr? Dec 5, 2018 at 9:33
  • yes, try putting script into the form Dec 5, 2018 at 9:35
  • Wonderful! it works! Could you please explain the reason that script need to be loacted in form? + when I ckick button at the first time, it doesn't show me value, but I try again, it works. Why is this happening? Dec 5, 2018 at 9:46
  • Because It refresh part of page in particular form/outputpanel, and you get updated values from controller. Also one thing I forgot instead of this you can use oncomple event of action function, in this case you dont need to double click on the button Dec 5, 2018 at 9:50
  • check updated answer! Dec 5, 2018 at 9:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .