You need to sign in to do that
Don't have an account?
Luca Benedettini 25
Passing an array from Javascript to Apex
Hi,
I have a little problem but I'm not able to find a solution.I'm trying to pass an array variable "d" from javascript to a variable "Value" in the apex controller. Here my code:
javascript:
visualforce:
apex controller:
I really don't understand what's going wrong. Why Value doesn't contain the array in d? Help me please
I have a little problem but I'm not able to find a solution.I'm trying to pass an array variable "d" from javascript to a variable "Value" in the apex controller. Here my code:
javascript:
function setVal(param){ param =param.replace("[",""); param =param.replace("]",""); var mylist = new Array() mylist = param.split(','); alert(mylist); var d =new Array(); for( var i = 0; i < mylist.length; i++ ){ var appoggio = mylist[i].trim(); var strAppoggio = document.getElementById( appoggio ).innerHTML; strAppoggio = strAppoggio.substring(strAppoggio.lastIndexOf("*") + 1, strAppoggio.lastIndexOf("+")); d.push(strAppoggio); } afConMethod(d.join()); alert('dToString: ' + dToString); }
visualforce:
<apex:commandButton value="Get" onclick="setVal('{!firstrow}');" reRender="output"/> <apex:actionFunction name="afConMethod" action="{!MyConMethod}" > <apex:param value="" name="theValue" assignTo="{!Value}" />
apex controller:
public List<String> values {get;set;} private String Value = 'no'; public void setValue(String n) { Value = n; System.debug('Set Passed in Value: '+ Value); } public String getValue() { System.debug('Get Passed in Value: '+ Value); return Value; } public void MyConMethod() { system.debug('Start'); convertStrToList(Value); } public void convertStrToList(String str) { String[] listToReturn = new List<String>(); if(String.isNotBlank(str)) { for(String eachStr : str.split(',')) { listToReturn.add(eachStr); } } system.debug(listToReturn); }The debug logs shows the message: Passed in value: null
I really don't understand what's going wrong. Why Value doesn't contain the array in d? Help me please
Just pass the string to apex and parse the string as list in apex, as your doing in function in vf page.
The variable you declared as
public List<String> values {get;set;}
but your assigning to "Value"
<apex:param value="" name="theValue" assignTo="{!Value}" />
It should be :
<apex:param value="" name="theValue" assignTo="{!values}" />
Mark this as best answer if this helps
All Answers
Just pass the string to apex and parse the string as list in apex, as your doing in function in vf page.
The variable you declared as
public List<String> values {get;set;}
but your assigning to "Value"
<apex:param value="" name="theValue" assignTo="{!Value}" />
It should be :
<apex:param value="" name="theValue" assignTo="{!values}" />
Mark this as best answer if this helps