开发者

Returning array from apex controller class and using in javascript in Salesforce

开发者 https://www.devze.com 2023-03-10 22:06 出处:网络
I want to return an array from apex custom controller class in visua开发者_JAVA百科lforce and use that array in JavaScript.

I want to return an array from apex custom controller class in visua开发者_JAVA百科lforce 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.


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>


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号