0

I want to return an array from apex custom controller class in visualforce and use that array in JavaScript.

How I am doing:

Javascript:
   var SFObjectArray;
   function myJavascriptMethod()
   {
      SFObjectArray = myArrayItems();
   }


Apex:
<apex:actionFunction name="myArrayItems" 
                     action="{!myArrayItems}" 
                     status="mystatus" 
                     reRender="out"/>
</apex:actionFunction>

Controller:

public class MyController
{
  String[] arrayItems;

   public PageReference myArrayItems()
    {
       arrayItems = new String[]{'abc','def'};
        return null;
    }

    public String[] getItems()
     {
       return arrayItems ;
     }  
}

can anybody provide me some help.

1
  • can anybody provide me some help!!!!!!! Commented Jun 7, 2011 at 8:03

2 Answers 2

3

You will have to dynamically build your javascript, the apex controller method will not return you a javascript object. I would use the visualforce repeat tag something like the following to build the javascript array.

var SFObjectArray = new Array();
<apex:repeat value="{!arrayItems}" var="arrayItem">
SFObjectArray.push('{!arrayItem}');
</apex:repeat>
Sign up to request clarification or add additional context in comments.

Comments

0

You could also use the javascript remoting feature which would return a javascript object/array for you:

controllerLeadReviewToolSetup.asyncFunction(Parameter, function(result, event)
{
    if(event.status)
    {
        for(var i = 0; i < result.size; i++)
        {
           [result[iterator]].toString();

        }
    }
}, {escape:true});

More on this here http://www.salesforce.com/us/developer/docs/pages/Content/pages_js_remoting.htm

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.