向Ajax事件监听器发送附加参数

向Ajax事件监听器发送附加参数

本文介绍了向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 =.../> 。从 :

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



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:

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事件监听器发送附加参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 14:22