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

时间:2018-05-10 18:48:54

标签: salesforce visualforce

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

每个VFP之间的唯一区别是控制器中找到的SOQL语句的WHERE子句。我的想法是让每个VFP将一个静态(非更改/硬编码)字符串传递给控制器​​,以便在控制器的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按钮/ selectList /等。或者2)他们处理从控制器传递变量到VFP;这些都不是我需要的。我最接近解决方案是使用此来源:https://salesforce.stackexchange.com/questions/24666/how-to-pass-javascript-value-to-controller ...但此示例需要在VFP上使用按钮。

不言而喻,我是visualforce的新手,但在Python中拥有约4年的编码经验(这并没有像我希望的那样有帮助)。任何如何实现这一点的例子都将非常感激!!

1 个答案:

答案 0 :(得分:2)

尝试查看&lt; apex:actionFunction&gt;标签与&lt; apex:param&gt;标签应该让你到达你想去的地方。 Action函数允许您调用控制器方法,还允许您传递分配给属性的params,其值可以设置。

<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按照它们出现的顺序列在param标记中。这会导致部分postpack到serverside控制器调用action函数标记的action属性指定的方法,actionfunction标记的name属性中的js方法的名称。由于回发属性的一部分已更新,因此您的参数可用于控制器。