f:ajax inside ui:repeat

蓝咒 提交于 2019-12-24 22:44:03

问题


I need to show a list of auction items. When a user clicks on a Bid button next to each item, I'd like to have ajax open a bid form right under this auction item. So I'm going with a ui:repeat and a f:ajax as shown below, but when I go to the page all the auction items have the bid component open next to them. And clicking any of the buttons doesn't do anything. This is the code (with the bid form simplified to just an outputText:)

<h:form>
<table border="1">
<ui:repeat var="parcel" varStatus="status" value="#{auctionsViewBean.parcels}">
<tr>
  <td><h:commandButton value="Bid" action="nothing">
      <f:ajax render="bidView"/>
  </h:commandButton></td>
  <td>#{status.index + 1}</td>
  <td>#{parcel.a}</td>
  <td>#{parcel.b}</td>
  <td>#{parcel.c}</td>
</tr>    
<tr><td><h:outputText id="bidView" value="#{auctionsViewBean.showBidViewForParcel(parcel)}">Some text</h:outputText></td></tr>

</ui:repeat> 
</table>
</h:form>

What am I doing wrong? And how can I specify only the bid component related to the clicked auction item?


回答1:


If I understand you correctly, you want to initially hide the bidView until the button is pressed? You can do it by giving it a rendered attribute and put it in another component which can be referenced by <f:ajax>. You only need to rearrange the action method and checking logic.

<h:commandButton value="Bid" action="#{auctionsViewBean.addBidView(parcel)}">
<f:ajax render="bidView" />
...
<h:panelGroup id="bidView">
    <h:panelGroup rendered="#{auctionsViewBean.showBidView(parcel)}">
        ...
    </h:panelGroup>
</h:panelGroup>

with something like this:

public void addBidView(Parcel parcel) {
    bidViews.put(parcel, new BidView());
}

public boolean isShowBidView(Parcel parcel) {
    return bidViews.containsKey(parcel);
}


来源:https://stackoverflow.com/questions/8201634/fajax-inside-uirepeat

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!