是否可以从 Visualforce Apex 标签传递参数?
我有一个函数 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将参数从页面传递到函数中,如下所示:
显然,在这种情况下参数的值是固定的。如果你想要一些动态的东西(即用户输入一些东西并将其传递给函数),我不是 100% 确定你会如何做到这一点,但我认为这是可能的。然而,解决方案已经为你发布了猫的皮肤,但我想我会跟进一个替代方案,以防它有任何用处。
You can pass in parameters from a page into a function like this:
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.
不,你不能将参数传递给这样的操作。
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!