简体   繁体   English

从Visualforce页面将STATIC /硬编码值传递给控制器

[英]Passing STATIC/hardcoded value to controller from visualforce page

I'm creating 8 visualforce pages (VFPs) for a dashboard and because of the similarity of the data needed for each VFP, I'm hoping to leverage a single custom controller (rather than 8 controllers). 我正在为仪表板创建8个visualforce页面(VFP),并且由于每个VFP所需数据的相似性,我希望利用单个自定义控制器(而不是8个控制器)。

The only difference between each VFP will be the WHERE clause of a SOQL statement found in the controller. 每个VFP之间的唯一区别将是在控制器中找到的SOQL语句的WHERE子句。 My thought is to have each VFP pass a static (non-changing/hard-coded) string to the controller to use as in the controller's SOQL WHERE clause. 我的想法是让每个VFP将静态(不变的/硬编码的)字符串传递给控制器​​,以用于控制器的SOQL WHERE子句中。 Here's an example of what I'm looking for: 这是我要寻找的示例:

Controller: 控制器:

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 Page: 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>

I've looked at dozens of posts similar to this, but all existing posts either 1) deal with passing dynamic values (usually defined via JS script) from the VFP to the control VIA a button/selectList/etc. 我看过数十篇与此类似的文章,但是所有现有的文章要么1)处理将动态值(通常通过JS脚本定义)从VFP传递到按钮/ selectList / etc等控件。 or 2) they deal with passing variables from the controller to the VFP; 或2)他们处理从控制器到VFP的传递变量; neither of which are what I need. 我都不需要。 The closest I got to a solution was by using this source: https://salesforce.stackexchange.com/questions/24666/how-to-pass-javascript-value-to-controller ... but this example required the use of a button on the VFP. 我最接近解决方案的地方是使用以下来源: https : //salesforce.stackexchange.com/questions/24666/how-to-pass-javascript-value-to-controller ...但是此示例要求使用VFP上的一个按钮。

Goes without saying, I'm new to visualforce but have ~4 years of coding experience in Python (which hasn't helped as much as I was hoping). 毋庸置疑,我是Visualforce的新手,但是拥有大约4年的Python编码经验(这并没有达到我的期望)。 Any example of how to accomplish this would be greatly appreciated!! 如何做到这一点的任何例子将不胜感激!

Try looking at the < apex:actionFunction > tag with a < apex:param > tag That should get you where you want to go. 尝试用<apex:param>标记查看<apex:actionFunction>标记,这应该可以使您到达想要的位置。 Action functions let you call a controller method an also lets you pass params assigned to a property with a value that you can set. 动作函数使您可以调用控制器方法,还可以传递分配给具有可设置值的属性的参数。

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

from javascript after load make a call to 加载后从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

Update 更新资料

So I create a fully working sample page so you could better see it in action. 因此,我创建了一个可以正常运行的示例页面,以便您更好地看到它的实际效果。

VF Page 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>

Controller 控制者

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;
    }
}

What's happening ... the actionfunction tag declares a js function that would make an async call to the controller, with params listed in the param tag in the order that they appear in. That causes a partial postpack to the serverside controller calling the method designated by the action property of the action function tag, the name of the js method in the name property of the actionfunction tag. 发生了什么... actionfunction标记声明了一个js函数,该函数将对控制器进行异步调用,并以param标记中出现的顺序列出其参数。这会导致部分服务器后包装到服务器端控制器,从而调用指定的方法通过action function标签的action属性,在actionfunction标签的name属性中使用js方法的名称。 As part of a postback properties are updated hence your param becomes available for use in the controller. 作为回发属性的一部分,更新后,您的参数就可以在控制器中使用了。

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

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