是否可以从 Visualforce Apex 标签传递参数?

发布于 2024-09-30 17:20:20 字数 928 浏览 2 评论 0原文

我有一个函数 searchWorkByName,它将“key”作为参数并使用 SQOL 来检索数据。

在 Visualforce 方面,我有一个调用 searchWorkByName 的链接,但希望能够传递诸如字符“a”

示例之类的参数(这会引发错误)

<apex:commandLink value="search!" action="{!searchWorkByName('aaa')}" />

是否可以这样做,如果不是,还有什么替代方案?

顶级级别

public class SearchWorkTest { 

    public PageReference searchWorkByName(String key) {

            //find record of work names starting from provided key character
            workNames = [select name from work__c where work__c.name like 'key%'];
            return Page.searchResult;   
     }
}

视觉力

<apex:page standardController="work__c" extenstions="SearchWorkTest">
  <!-- Is it possible to pass argument like 'foo' ? -->
  <apex:commandLink value="search!" action="{!searchWorkByName}" />
</apex:page>

I have a function searchWorkByName that takes "key" as an argument and use SQOL to retrieve the data.

In visualforce side, I have a link that calls searchWorkByName but would like to be able to pass argument such as character 'a'

example, (this throws an error)

<apex:commandLink value="search!" action="{!searchWorkByName('aaa')}" />

Is it possible to do so if not what is the alternatives?

apex class

public class SearchWorkTest { 

    public PageReference searchWorkByName(String key) {

            //find record of work names starting from provided key character
            workNames = [select name from work__c where work__c.name like 'key%'];
            return Page.searchResult;   
     }
}

visualforce

<apex:page standardController="work__c" extenstions="SearchWorkTest">
  <!-- Is it possible to pass argument like 'foo' ? -->
  <apex:commandLink value="search!" action="{!searchWorkByName}" />
</apex:page>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可可 2024-10-07 17:20:20

您可以将参数从页面传递到函数中,如下所示:

<apex:commandLink value="search!" action="{!searchWorkByName}">
  <apex:param name="key" value="val"/>
</apex:commandLink>

显然,在这种情况下参数的值是固定的。如果你想要一些动态的东西(即用户输入一些东西并将其传递给函数),我不是 100% 确定你会如何做到这一点,但我认为这是可能的。然而,解决方案已经为你发布了猫的皮肤,但我想我会跟进一个替代方案,以防它有任何用处。

You can pass in parameters from a page into a function like this:

<apex:commandLink value="search!" action="{!searchWorkByName}">
  <apex:param name="key" value="val"/>
</apex:commandLink>

Obviously, the value of the parameter in this case is fixed. If you want something dynamic (i.e. user types something and that is passed to the function), I'm not 100% sure how you'd do that, but I think it might be possible. However, the solution already posted skins the cat for you, but I thought I'd follow up with an alternative in case it's any use.

剑心龙吟 2024-10-07 17:20:20

不,你不能将参数传递给这样的操作。

1 个选项是使该变量成为普通表单字段,用户可以输入文本/从下拉列表中选择/其他内容 - 如果您在 Apex 中对变量使用相同的名称(并使其由 setters/getters 公开可见),这将起作用没有问题。请查看我的答案:如何将 Salesforce 与 Google 地图集成? 开始。

第二个选项 - 如果此搜索必须以某种方式以编程方式完成,而无需用户单击任何内容,例如,如果数据来自页面本身(即在 标记中读取)...你可以制作一个小的帮助页面&控制器并将它们称为组件。向组件传递数据没有问题。检查 的文档。但我认为第一个答案对您最有用。

祝你好运!

No, you cannot pass arguments to actions like that.

1 option is to make this variable a normal form field that user can type text/select from dropdown/whatever - if you'll use same name for a variable in Apex (and make it publicly visible by setters/getters), this will work without problems. Check out my answer at How do I integrate Salesforce with Google Maps? to get started.

Second option - if this search must be somehow done programatically without user having to click anything, if the data for example comes from page itself (i.e. is read in <apex:repeat> tag)... you could make a small helper page & controller and call them as components. There is no problem with passing data to components. Check documentation for <apex:component> and <apex:componentBody>. But I think first answer os most useful for you.

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文