如何通过ajax将迭代参数传递给backing bean方法 [英] How to pass an iterated parameter via ajax to a backing bean method

查看:68
本文介绍了如何通过ajax将迭代参数传递给backing bean方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个数据表,如下所示:

So, I have a dataTable that looks like this:

                <h:form>
        <h:dataTable value="#{backingBean.employeeLineItems}" var="emp">
            <h:column>
                <f:facet name="header">First</f:facet>
                #{emp.lastname}
            </h:column>
            <h:column>              
                <f:facet name="header">Last</f:facet>
                #{emp.firstname}
            </h:column>
            <h:column>
                <f:facet name="header">Actions</f:facet>
                <h:commandButton value="View Details"> 
                    <f:ajax execute="#{setCurrentEmployeeId(emp.id)}" render="employeeDetails"/> 
                </h:commandButton>
            </h:column>
        </h:dataTable>
                    <h:outputText value="#{backingBean.employeeDetails}" id="employeeDetails"/>
                </h:form>

对于数据表的每一行,都有一个我想要的按钮,当单击该按钮时,它会用ajax将employeeLineItem id的值更改为一个方法,该方法在后备bean中设置该id,然后呈现id为 employeeDetails的outputText标记(当然,getEmployeeDetails方法将使用employeeLineItem id从数据库中获取正确的员工详细信息对象)

For each row of the datatable, there is a button that I want to, when clicked, ajax the employeeLineItem id value over to a method that sets that id in the backing bean, and then renders the outputText tag with id "employeeDetails" (The getEmployeeDetails method would use the employeeLineItem id to get the right employee details object from the database, of course)

我的解决方案似乎无效,有人知道我在做什么吗?

My solution doesn't seem to be working, does anyone know what I'm doing wrong?

推荐答案


<h:commandButton value="View Details"> 
   <f:ajax execute="#{setCurrentEmployeeId(emp.id)}" render="employeeDetails"/> 
</h:commandButton>


这是错误的。 < f:ajax> execute 属性应指向以空格分隔的组件客户端ID集合可以在服务器端进行提交和处理(与您指定 render 属性的方式相同,该属性带有以空格分隔的组件客户端ID集合,这些组件ID将在之后更新/重新呈现ajax请求)。在您的特定情况下,它应该是数据表或表单的ID,或者只是 @form 来通用地引用父表单。

This is wrong. The execute attribute of <f:ajax> should point to a space separated collection of component client IDs which are to be submitted and processed in the server side (the same way as you specify the render attribute with a space separated collection of component client IDs which are to be updated/re-rendered after the ajax request). In your particular case, it should have been the ID of the datatable or the form, or just @form to generically refer the parent form.

其中传递行ID的操作方法应在< h:commandButton>的 action 属性中定义。 代替。因此,应该这样做:

The action method wherein you pass the row ID should be definied in the action attribute of the <h:commandButton> instead. So, this should do:

<h:commandButton value="View Details" action="#{backingBean.setCurrentEmployeeId(emp.id)}">
    <f:ajax execute="@form" render="employeeDetails"/> 
</h:commandButton>

(请注意,我在action方法中修复了丢失的托管bean名称)

顺便问一下,您是否知道还可以将整个对象作为参数传递给EL?

By the way, are you aware that you can also just pass whole objects along as arguments in EL?

<h:commandButton value="View Details" action="#{backingBean.setCurrentEmployee(emp)}">
    <f:ajax execute="@form" render="employeeDetails"/> 
</h:commandButton>

这样,您无需从数据库中重新加载员工。

This way you don't need to reload the employee from the DB.

这篇关于如何通过ajax将迭代参数传递给backing bean方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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