从顶点控制器类返回数组并在 Salesforce 中的 javascript 中使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6250672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Returning array from apex controller class and using in javascript in Salesforce
提问by Swati
I want to return an array from apex custom controller class in visualforce and use that array in JavaScript.
我想从 Visualforce 中的 apex 自定义控制器类返回一个数组,并在 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.
任何人都可以为我提供一些帮助。
回答by manubkk
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.
您必须动态构建 javascript,apex 控制器方法不会返回 javascript 对象。我会使用如下所示的visualforce重复标签来构建javascript数组。
var SFObjectArray = new Array();
<apex:repeat value="{!arrayItems}" var="arrayItem">
SFObjectArray.push('{!arrayItem}');
</apex:repeat>
回答by Tony H.
You could also use the javascript remoting feature which would return a javascript object/array for you:
您还可以使用 javascript 远程处理功能,它会为您返回一个 javascript 对象/数组:
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
更多关于这里http://www.salesforce.com/us/developer/docs/pages/Content/pages_js_remoting.htm