如何通过 ajax 将迭代参数传递给支持 bean 方法

发布于 2024-12-13 07:45:37 字数 1217 浏览 1 评论 0 原文

因此,我有一个如下所示的 dataTable:

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

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

我的解决方案似乎不起作用,有谁知道我做错了什么?

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>

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

救赎№ 2024-12-20 07:45:37
 
   ; 

这是错误的。 execute 属性应该指向一个空格分隔的组件客户端 ID 集合,这些 ID 将在服务器端提交和处理(同样的方式因为您使用空格分隔的组件客户端 ID 集合指定 render 属性,这些 ID 将在 ajax 请求后更新/重新呈现)。在您的特定情况下,它应该是数据表或表单的 ID,或者只是 @form 以一般引用父表单。

您传递行 ID 的操作方法应在 action 属性中定义。所以,应该这样做:

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

(请注意,我修复了操作方法中缺少的托管 bean 名称)

顺便说一句,您是否知道也可以将整个对象作为 EL 中的参数传递?

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

这样您就不需要从数据库重新加载员工。

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

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.

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>

(note that I fixed the missing managed bean name in the action method)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文