开发者

Set ID of each line on a link in a dataTable

I have a page that lists all from my Post table, it looks like this:

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

What i want is that, at the end of each line, 2 links, one for update and other for show actions get the id of that specific post, but as it is its setting every id as the last resault of the list. How can i fix that?

Edit: Heres the HTML generated:

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


I am not sure of what you want to do at the end, but why don't you use a <h:commandLink> (that will generate a <a> HTML tag) instead (don't forget to nest your <h:datatable> in a <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>

Then, you define two actions in your Java bean (the String returned defines the navigation case to apply after the execution of the method):

public String alterar() {
    ...
}

public String consultar() {
    ...
}

And in each action method, you can retrieve the corresponding element (i.e. post1) using the getRowData() method of the HtmlDatatable component:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜