如何将值从可视化力量组件中的脚本传递到其控制器

How to pass values from Script in visualforce component to its controller

本文关键字:脚本 控制器 组件 力量 可视化      更新时间:2023-09-26

我有一个 visualforce 组件,里面写了一些脚本,我直接想将一些值传递给控制器。

<script>
function uploadComplete(evt) {
  var city = 'Shimla';
  var location = 'kkllkk'
  **i want to pass city and location in IWantToDebug method**
  function IWantToDebug() {
         MyController.IWantToDebug(city,location, function(result, event) {
    // Set the inputField value to the result in here
   });
} </script>

我的顶点控制器类方法就像。

 public void IWantToDebug(String city , String location) {
      System.debug('======================= ' + data);
 }

您可以使用 actionfunction 将值传递给控制器

将以下内容添加到组件的 HTML 部分:

<apex:actionFunction action="{!IWantToDebug}" name="IWantToDebugJavascriptSide" rerender="someComponentIdToRender">
    <apex:param name="city " value=""/>
    <apex:param name="location" value=""/>
</apex:actionFunction>

将你的 JavaScript 更改为类似的东西

    <script>
       function uploadComplete(evt) {
          var city = 'Shimla';
          var location = 'kkllkk'
          **i want to pass city and location in IWantToDebug method**
          function IWantToDebug() {
          IWantToDebugJavascriptSide(city,location);
      });
   </script>

并将您的控制器更改为类似

public PageReference IWantToDebug() {
     String city , String location;
     if (Apexpages.currentPage().getParameters().containsKey('city')){
       city = Apexpages.currentPage().getParameters().get('city'));
     }
     if (Apexpages.currentPage().getParameters().containsKey('location')){
        location= Apexpages.currentPage().getParameters().get('location'));
     }
     System.debug('======================= ' + city + ' ' +location);
     return null;
  }

有关如何使用操作函数的更多参考,请访问https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionFunction.htm

谢谢