0
votes

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

1 Answers

2
votes

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);
}