向Ajax事件监听器发送附加参数 [英] Send additional parameter to Ajax event listener
问题描述
但是,由于我使用通用类型作为模型,我想在我的通用数据控制控制器中另外指定什么是第二个参数的视图。
不幸的是可以在两个听众方法之间进行选择,一种使用事件参数来帮助识别对象,第二种方法使您有机会发送免费参数,但缺少事件。
模板:
< p:dataTable值=#{aObj.objList}var =item.... selectionMode =single>
< p:ajax事件=rowSelectlistener =#{aObj.viewItem}/>
< p:ajax event =rowSelectlistener =#{aObj.viewItem('myItemView?_id =')}/>
...
< / p:dataTable>
控制器:
public void viewItem(SelectEvent event){
// ...
}
public void viewItem(String viewUrl){
// ...
}
我可以添加其他属性bean,但是由于它是通用的,并且提供的模型项目不会被污染。
有没有解决方法?
您可以在数据表中设置一个属性,并在选择的侦听器中读取它。为此,请使用< f:attribute name =...value =.../>
。从文档 :
限制
必须嵌套在
UIComponent
自定义操作。
描述
找到最近的父母
UIComponent
自定义动作实例(...)。如果关联组件已经具有一个组件
属性,该组件具有该名称,则不执行任何操作。否则,在参数值
上调用isLiteralText()
方法。如果
返回true
,则将该值存储在组件的属性Map中,并以上述方式导出。如果它返回false
,在组件的ValueExpression中存储
code>在上面导出的名称下的地图。
$ code> ValueExpression
所以,采取你在评论中设置的属性,你应该使用它像:
XHTML:
< p:dataTable value =#{aObj.objList}var =item.... selectionMode =single>
< f:attribute name =testvalue =abc/>
< p:ajax event =rowSelectlistener =#{aObj.viewItem}/>
...
< / p:dataTable>
聆听者:
code> public void viewItem(SelectEvent event){
String test =(String)event.getComponent()。getAttributes()。get(test);
// ...
}
I am having a ajax listener who should redirect to item view page. However since I am using generic type as model I would like to specify additionally in my common datatable controller what is the view with a second parameter.
Unfortunately one can choose between two listener approaches one using event parameter which helps identifying the object and the second one gives you the opportunity to send free param but lacks the event.
template:
<p:dataTable value="#{aObj.objList}" var="item" .... selectionMode="single">
<p:ajax event="rowSelect" listener="#{aObj.viewItem}" />
<p:ajax event="rowSelect" listener="#{aObj.viewItem('myItemView?_id=')}" />
...
</p:dataTable>
controller:
public void viewItem(SelectEvent event) {
// ...
}
public void viewItem(String viewUrl) {
// ...
}
I can add additional properties to the bean but since it is generic and providing model items doesn't feel right to pollute it.
Is there any workaround?
You could set an attribute in your data table and read it in your select listener. To do so, use <f:attribute name="..." value="..."/>
. From the documentation:
Constraints
Must be nested inside a
UIComponent
custom action.Description
Locate the closest parent
UIComponent
custom action instance (...). If the associated component already has a component attribute with that name, take no action. Otherwise, call theisLiteralText()
method on the argumentvalue
. If it returnstrue
, store the value in the component’s attribute Map under the name derived above. If it returnsfalse
, store theValueExpression
in the component’sValueExpression
Map under the name derived above.
So, taking the attribute you tried to set in your comment, you should use it like:
XHTML:
<p:dataTable value="#{aObj.objList}" var="item" .... selectionMode="single">
<f:attribute name="test" value="abc" />
<p:ajax event="rowSelect" listener="#{aObj.viewItem}" />
...
</p:dataTable>
Listener:
public void viewItem(SelectEvent event) {
String test = (String) event.getComponent().getAttributes().get("test");
// ...
}
这篇关于向Ajax事件监听器发送附加参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!