设置dataTable中链接上每一行的ID

时间:2010-10-26 19:36:49

标签: jsf datatable

我有一个页面列出了我的Post表中的所有内容,它看起来像这样:

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>TODO supply a title</title>
</head>
<body>
    <p>
        <h:dataTable id="grid1" value="#{postControle.lista}" var="post1">
            <h:column>
                <f:facet name="header"><h:outputText style="float: left; font-weight: bold;" value="Titulo"/></f:facet>
                #{post1.titulo}
            </h:column>
            <h:column>
                <f:facet name="header"><h:outputText style="float: left; font-weight: bold;" value="Texto"/></f:facet>
                #{post1.texto}
            </h:column>
            <h:column>
                <f:facet name="header"><h:outputText style="float: left; font-weight: bold;" value="Opções"/></f:facet>
                <a href="#{postControle.requestManager('alterar',post1.id)}">Alterar</a> |
                <a href="#{postControle.requestManager('consultar',post1.id)}">Consultar</a>
            </h:column>
        </h:dataTable>
    </p>
</body>

我想要的是,在每一行的末尾,有2个链接,一个用于更新,另一个用于显示操作获取该特定帖子的ID,但因为它是将其设置为每个id作为列表的最后一个resault 。我该如何解决这个问题?

编辑:继承人生成的HTML:

<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <title>TODO supply a title</title> 
</head> 
<body> 
    <p><table id="grid1"> 
            <thead> 
                <tr> 
                    <th scope="col"><span style="float: left; font-weight: bold;">Titulo</span></th> 
                    <th scope="col"><span style="float: left; font-weight: bold;">Texto</span></th> 
                    <th scope="col"><span style="float: left; font-weight: bold;">Opções</span></th> 
                </tr> 
            </thead> 
            <tbody> 
                <tr> 
                    <td> 
                        testeZ
                    </td> 
                    <td> 
                        testeZ
                    </td> 
                    <td><a href="./faces/alterar.xhtml">Alterar</a> |
                        <a href="./faces/consultar.xhtml">Consultar</a></td> 
                </tr> 
                <tr> 
                    <td> 
                        testeZ
                    </td> 
                    <td> 
                        testeZ
                    </td> 
                    <td><a href="./faces/alterar.xhtml">Alterar</a> |
                        <a href="./faces/consultar.xhtml">Consultar</a></td> 
                </tr> 
                <tr> 
                    <td> 
                        testeZ
                    </td> 
                    <td> 
                        testeZ
                    </td> 
                    <td><a href="./faces/alterar.xhtml">Alterar</a> |
                        <a href="./faces/consultar.xhtml">Consultar</a></td> 
                </tr> 
                <tr> 
                    <td> 
                        asdasdsa
                    </td> 
                    <td> 
                        asdsadsad
                    </td> 
                    <td><a href="./faces/alterar.xhtml">Alterar</a> |
                        <a href="./faces/consultar.xhtml">Consultar</a></td> 
                </tr> 
                <tr> 
                    <td> 
                        ddddd
                    </td> 
                    <td> 
                        dddd
                    </td> 
                    <td><a href="./faces/alterar.xhtml">Alterar</a> |
                        <a href="./faces/consultar.xhtml">Consultar</a></td> 
                </tr> 
                <tr> 
                    <td>                     
                    </td> 
                    <td>                     
                    </td> 
                    <td><a href="./faces/alterar.xhtml">Alterar</a> |
                        <a href="./faces/consultar.xhtml">Consultar</a></td> 
                </tr> 
                <tr> 
                    <td>                     
                    </td> 
                    <td>                    
                    </td> 
                    <td><a href="./faces/alterar.xhtml">Alterar</a> |
                        <a href="./faces/consultar.xhtml">Consultar</a></td> 
                </tr> 
                <tr> 
                    <td> 
                        dsakhgdsail
                    </td> 
                    <td> 
                        woaiuheaoiusrhjgsohrspuiohpousrhpiojnojho
                    </td> 
                    <td><a href="./faces/alterar.xhtml">Alterar</a> |
                        <a href="./faces/consultar.xhtml">Consultar</a></td> 
                </tr> 
            </tbody> 
        </table> 
    </p> 
</body> 

<head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>TODO supply a title</title> </head> <body> <p><table id="grid1"> <thead> <tr> <th scope="col"><span style="float: left; font-weight: bold;">Titulo</span></th> <th scope="col"><span style="float: left; font-weight: bold;">Texto</span></th> <th scope="col"><span style="float: left; font-weight: bold;">Opções</span></th> </tr> </thead> <tbody> <tr> <td> testeZ </td> <td> testeZ </td> <td><a href="./faces/alterar.xhtml">Alterar</a> | <a href="./faces/consultar.xhtml">Consultar</a></td> </tr> <tr> <td> testeZ </td> <td> testeZ </td> <td><a href="./faces/alterar.xhtml">Alterar</a> | <a href="./faces/consultar.xhtml">Consultar</a></td> </tr> <tr> <td> testeZ </td> <td> testeZ </td> <td><a href="./faces/alterar.xhtml">Alterar</a> | <a href="./faces/consultar.xhtml">Consultar</a></td> </tr> <tr> <td> asdasdsa </td> <td> asdsadsad </td> <td><a href="./faces/alterar.xhtml">Alterar</a> | <a href="./faces/consultar.xhtml">Consultar</a></td> </tr> <tr> <td> ddddd </td> <td> dddd </td> <td><a href="./faces/alterar.xhtml">Alterar</a> | <a href="./faces/consultar.xhtml">Consultar</a></td> </tr> <tr> <td> </td> <td> </td> <td><a href="./faces/alterar.xhtml">Alterar</a> | <a href="./faces/consultar.xhtml">Consultar</a></td> </tr> <tr> <td> </td> <td> </td> <td><a href="./faces/alterar.xhtml">Alterar</a> | <a href="./faces/consultar.xhtml">Consultar</a></td> </tr> <tr> <td> dsakhgdsail </td> <td> woaiuheaoiusrhjgsohrspuiohpousrhpiojnojho </td> <td><a href="./faces/alterar.xhtml">Alterar</a> | <a href="./faces/consultar.xhtml">Consultar</a></td> </tr> </tbody> </table> </p> </body>

1 个答案:

答案 0 :(得分:1)

我不确定你最后想做什么,但为什么不使用<h:commandLink>(会产生<a> HTML标签)而不是忘记将您的<h:datatable>嵌套在<h:form>)?

<h:form id="myForm">
    ...
    <h:dataTable id="grid1" value="#{postControle.lista}" var="post1">
        ...
        <h:column>
            <f:facet name="header"><h:outputText style="float: left; font-weight: bold;" value="Opções"/></f:facet>
            <h:commandLink action="#{postControle.alterar}" value="Alterar"/>
            <h:commandLink action="#{postControle.consultar}" value="Consultar"/>
        </h:column>
    </h:dataTable>

然后,在Java bean中定义两个动作(返回的String定义在执行方法后应用的导航大小写):

public String alterar() {
    ...
}

public String consultar() {
    ...
}

在每个操作方法中,您可以使用post1组件的getRowData()方法检索相应的元素(即HtmlDatatable):

public String alterar() {
    HtmlDatatable table = (HtmlDatatable) FacesContext.getCurrentInstance().getViewRoot().findComponent("myForm:id1");
    YourClass element = (YourClass) table.getRowData();
    ...