向Ajax事件监听器发送附加参数 [英] Send additional parameter to Ajax event listener

查看:186
本文介绍了向Ajax事件监听器发送附加参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应该重定向到项目视图页面的ajax监听器。
但是,由于我使用通用类型作为模型,我想在我的通用数据控制控制器中另外指定什么是第二个参数的视图。



不幸的是可以在两个听众方法之间进行选择,一种使用事件参数来帮助识别对象,第二种方法使您有机会发送免费参数,但缺少事件。



模板

 < 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> ValueExpression
code>在上面导出的名称下的地图。


所以,采取你在评论中设置的属性,你应该使用它像:



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 the isLiteralText() method on the argument value. If it returns true, store the value in the component’s attribute Map under the name derived above. If it returns false, store the ValueExpression in the component’s ValueExpression 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆