简体   繁体   English

如何添加具有特定ID的按钮,并为dataTable中的每一行调用以该ID作为参数的javabean方法?

[英]How do I add buttons with specific id and invoke javabean method with that id as argument for every row in a dataTable?

I created a generic xhtml page in my java web app that gets all the users from a database table and shows them in a datatable, but I want to add buttons next to every row (view, edit, delete) that have unique ids and which invoke methods in the java bean passing their ids as arguments. 我在Java Web应用程序中创建了一个通用的xhtml页面,该页面从数据库表中获取所有用户并将其显示在数据表中,但是我想在具有唯一ID的每行(查看,编辑,删除)旁边添加按钮,并且调用java bean中的方法,将其id作为参数传递。

I'm new at this it seems incredibly simple, but nothing I tried works. 我是新来的,这似乎非常简单,但我尝试过的任何方法都没有。

<h:form>
        <h:dataTable value="#{functions.employees}" var="employee"
                        rowClasses="oddTableRow, evenTableRow"
                        headerClass="tableHeader"
                        styleClass="table">
            <h:column>
                <f:facet name="header">id</f:facet>
                #{employee.id}
            </h:column>

            <h:column>
                <f:facet name="header">First Name</f:facet>
                #{employee.firstName}
            </h:column>

            <h:column>
                <f:facet name="header">Last Name</f:facet>
                #{employee.lastName}
            </h:column>

            <h:column>
                <f:facet name="header">Adress</f:facet>
                #{employee.adress}
            </h:column>

            <h:column>
                <f:facet name="header">Phone</f:facet>
                #{employee.phone}
            </h:column>

            <h:column>
                <f:facet name="header">Email</f:facet>
                #{employee.email}
            </h:column>

            <h:column>
                <h:commandButton value="View" action="viewPage.xhtml">
                    <f: actionListener binding ="#{Functions.setId(id)}"
                </h:commandButton>
            </h:column>

            <h:column>
                      <h:button value="Edit" outcome = "editPage.xhtml"/>
            </h:column>

            <h:column>
                      <h:button value="Delete" outcome = "#{Functions.delete(id)}"/>
            </h:column>

        </h:dataTable>
    </h:form>

How about you use PrimeFaces ? 您如何使用PrimeFaces? its components make a lot of work easier, for example in your case you can use the <p:dataTable> refer to their showcase for a concrete example. 它的组件使工作变得更加容易,例如,在您的情况下,您可以使用<p:dataTable>参阅其展示以获取具体示例。
If you want to do it with just JSF then refer to this , that question and its answer should solve your problem. 如果您只想使用JSF进行操作,请参考this ,该问题及其答案将解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM