简体   繁体   中英

Ajax + JSF 2 times clicking after the first click

I have a dataTable with a column named DELETE (it is a link) which has an listener . When I click it the first time (click 1) it deletes the row (as expected), but when I try it with another row after that nothing happened (click 2). In fact wherever I click next nothing happen. I should click another time (click 3) to get it work. I don't want that.

Attention: The "delete()" method in my_user is not reached after "click 2".

Here is the code for the column:

<h:column>
    <f:facet name="header">#{lng.del}</f:facet>
    <h:form>
        <h:commandLink action="#">
            <f:ajax event="click" listener="#{my_user.delete}" render="@all" />
            <h:graphicImage name="delete.png" library="images" styleClass="tableIcon" />
        </h:commandLink>
    </h:form>
</h:column> 

You've multiple forms inside the table. When you re-render another form from inside a form by ajax, then its view state will get lost and hence the 1st click will fail. This click however takes care that the form gets the view state back, so the 2nd click works.

Technically you need to re-render only the content of the other form, but this isn't possible in this particular use case. Better put the <h:form> outside the <h:dataTable> so that you have a single form with a shared view state.

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

If your page contains another forms as well, I'd suggest to render only the current form instead of all, otherwise any actions on those forms will fail as well.

<f:ajax event="click" listener="#{my_user.delete}" render="@form" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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