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

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

问题描述

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

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.

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

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.

模板:

<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>

控制器:

public void viewItem(SelectEvent event) {
  // ...
}
    
public void viewItem(String viewUrl) {
  // ...
}

我可以向 bean 添加额外的属性,但由于它是通用的,并且提供模型项并不会污染它.

I can add additional properties to the bean but since it is generic and providing model items doesn't feel right to pollute it.

有什么解决办法吗?

推荐答案

您可以在数据表中设置一个属性并在您的选择侦听器中读取它.为此,请使用 .来自 文档:

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:

必须嵌套在 UIComponent 自定义操作中.

Constraints

Must be nested inside a UIComponent custom action.

找到最近的父 UIComponent 自定义操作实例 (...).如果关联的组件已经有一个组件具有该名称的属性,则不执行任何操作.否则,对参数 value 调用 isLiteralText() 方法.如果它返回 true,将值存储在组件的属性 Map 中,以上面派生的名称命名.如果返回false,则存储组件的 ValueExpression 映射中的 ValueExpression 以上面派生的名称命名.

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>

听众:

public void viewItem(SelectEvent event) {
  String test = (String) event.getComponent().getAttributes().get("test");
  // ...
}

这篇关于向 Ajax 事件侦听器发送附加参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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