将 STATIC/hardcoded 值从 visualforce 页面传递给控制器

Passing STATIC/hardcoded value to controller from visualforce page

我正在为仪表板创建 8 个 visualforce 页面 (VFP),由于每个 VFP 所需数据的相似性,我希望利用单个自定义控制器(而不是 8 个控制器)。

每个 VFP 之间的唯一区别是在控制器中找到的 SOQL 语句的 WHERE 子句。我的想法是让每个 VFP 将静态 (non-changing/hard-coded) 字符串传递给控制器​​,以便在控制器的 SOQL WHERE 子句中使用。这是我正在寻找的示例:

控制器:

public class SalesRev_UpFrontFees {
    public String StageVal {get;set;}
    public Integer salesVal = 0;
    public Integer intTCV;

    public SalesRev_UpFrontFees(){
        getData();
        }

    public void getData() {

    //Insert code to pull the hardcoded 'StageVal' from visualforce pages

    String SoqlString = 'SELECT SUM(Total_Subscription_Fees__c) RevSum\n' +
                  'FROM Opportunity\n' +
                  'WHERE CloseDate >= 2018-01-01 AND CloseDate <= 2018-12-31\n' +
                  'AND Opportunity.RecordType.Name = \'Enterprise Opportunity\'\n' +
                  'AND StageName = \':StageVal\'';

    AggregateResult[] groupedResults = Database.query(SoqlString);

    objTCV = groupedResults[0].get('RevSum');
    intTCV = Integer.valueOf(objTCV);
    salesVal = (intTCV  == null) ? 0 : intTCV ;   
   } 
    /*public void setStageVal(String sStageVal) {
        StageVal = sStageVal;        
        } */

    public Integer getsalesVal() {
        return intTCV;
        }
}

Visualforce 页面:

    <apex:page controller="SalesRev_UpFrontFees">

        <head>
            <apex:includeScript value="{!URLFOR($Resource.VFGauge, 'VFGauge/jQuery/jquery-1.10.2.min.js')}" /> 
            <apex:includeScript value="{!URLFOR($Resource.VFGauge, 'VFGauge/jQuery/jquery-ui-1.10.2.custom.min.js')}" /> 

            <apex:includeScript value="{!URLFOR($Resource.Raphael, 'Raphael/resources/js/raphael.2.1.0.min.js')}" /> 
            <apex:includeScript value="{!URLFOR($Resource.Raphael, 'Raphael/resources/js/justgage.1.0.1.js')}" />
            <apex:stylesheet value="{!URLFOR($Resource.Raphael, 'Raphael/styles/style.css')}"/>
        </head>

        <apex:form id="myForm">

            <!--- Insert way to feed the controller a string value, e.g. String StageVal = 'Closed Won'--->

            <body>
                <apex:pageBlock id="xxx"> 
                    <div id="gauge1" style="width:320px; height:256px"></div>
                    <script type="text/javascript"> 

                    var g1 = new JustGage({      
                            id: "gauge1",
                            value: {!salesVal},
                            min: 0,
                            max: 1000000000,
                            title: "WW Sales Revenue",
                            levelColorsGradient: false,
                            gaugeWidthScale: 0.4,
                            valueFontSize: "5px"
                            });
                    </script>
                    <a id="linear-gauge" href=""></a>
                </apex:pageBlock>
            </body>
        </apex:form>
    </apex:page>

我看过许多与此类似的帖子,但所有现有帖子要么 1) 处理从 VFP 到控件的传递动态值(通常通过 JS 脚本定义)VIA button/selectList/etc。或 2) 它们处理从控制器到 VFP 的变量传递;两者都不是我需要的。我最接近解决方案的方法是使用此来源:https://salesforce.stackexchange.com/questions/24666/how-to-pass-javascript-value-to-controller ...但是这个例子需要在 VFP 上使用一个按钮。

不用说,我是 visualforce 的新手,但在 Python 方面有大约 4 年的编码经验(这并没有像我希望的那样有帮助)。任何如何完成此操作的示例将不胜感激!!

尝试查看带有 < apex:param > 标签的 < apex:actionFunction > 标签 这应该可以让您到达您想去的地方。动作函数让您可以调用控制器方法,还可以让您将分配给 属性 的参数传递给您可以设置的值。

<apex:actionFunction action="{!someMethod}" name="someMethodName" rerender="ifNeeded">
    <apex:param name="stageParam" assignTo="{!StageVal}" value="Closed Won" />
</apex:actionFunction>

从 javascript 加载后调用

someMethodName('Closed Won')// you may be able to call without params since the value in the param is hard coded but not sure that would be something to test

更新

所以我创建了一个完整的示例页面,以便您可以更好地查看它的实际效果。

VF 页面

<apex:page controller="TestController" >
  <h1>Congratulations</h1>
      This is your new Page: Test
  <apex:form >
      <apex:actionFunction action="{!someMethod}" name="someMethodName" rerender="ifNeeded">
          <apex:param name="stageParam" assignTo="{!StageVal}" value="Closed Won" />
      </apex:actionFunction>
      <apex:outputPanel id="ifNeeded">{!StageVal} {!opps.size}

      </apex:outputPanel>
      <script>
          window.onload = function() {
               someMethodName();//or someMethodName('Closed Lost'); to replace default of param variable
          }
      </script>
   </apex:form>
</apex:page>

控制器

public class TestController {

    public String StageVal {get;set;}

    public List<Opportunity> opps {get;set;}

    public PageReference someMethod() {
        opps = [Select Name from Opportunity WHERE StageName = :StageVal];
        return null;
    }
}

发生了什么事... actionfunction 标签声明了一个 js 函数,该函数将对控制器进行异步调用,params 标签中的参数按照它们出现的顺序列出。这会导致部分 postpack 到服务器端controller调用action函数标签的action属性指定的方法,actionfunction标签的name属性中的js方法名。作为回发属性的一部分,更新因此您的参数可以在控制器中使用。