繁体   English   中英

在visualforce中从javascript调用控制器方法

[英]Call a controller method from javascript in visualforce

如何在没有任何onclick事件发生的情况下从VisualForce页面调用控制器方法? 我的VisualForce页面就像

<apex:page standardController="Account"> 

    <script>
     /* Here i need to call a controller class with out any onclick event. It should load by itselef */

    </script>

</apex:page>

我的控制器类就像

public class OrderPadController {
    /* this will be the constructor */

    public PageReference openPage() {
         PageReference newpage = new Pagereference('/apex'+'/pagetoopen'+'?aid='+a.id); 
         openorderpadembed.setRedirect(false); 
         return openorderpadembed;      
    }

从我的VisualForce页面,我需要调用openPage()方法。

请帮帮我

您可以通过以下方式为Apex控制器使用JavaScript Remoting

<apex:page standardController="Account" extensions="OrderPadController">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script>
      window.$j = jQuery.noConflict();
      $j( document ).ready(function() {
        OrderPadController.openPage(function(result, event){

            console.log(result);
            window.open(result,"_self");

        });
      });

    </script>
</apex:page>
public class OrderPadController {
    //

    @remoteAction
    public  PageReference openPage() {
        PageReference newpage = NEW Pagereference('/apex' + '/pagetoopen' + '?aid=' + a.id);
        openorderpadembed.setRedirect(false);
        return openorderpadembed;
    }
}
<apex:page standardController="Account" extensions="OrderPadController" 
    action="{!openPage}">
</apex:page>

一旦页面加载, action参数将调用您想要的方法。 这是一个危险的属性,如果您要通过Salesforce的安全审核流程,将会向您指出。 被调用的动作可以操纵数据库值(例如,OrderPadController构造函数不能),并且哲学是访问页面的行为不应该有任何副作用。

在您的特定场景中,即使没有任何Apex代码,您也可以执行此操作:

<apex:page standardController="Account" 
    action="/apex/Gantt?aid={!$CurrentPage.parameters.id}">
    Look Mom, no hands!
</apex:page>

正如帕维尔所提到的 - 请详细说明您的要求。 我怀疑你甚至不需要这个visualforce页面充当重定向,一个简单的自定义链接或按钮(type = url而不是Visualforce或Javascript)可以做到这一点......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM