Reputation: 11454
I want to create a web page using JSF with the following table:
Item 1 | ( ) good ( ) ok ( ) bad
Item 2 | ( ) good ( ) ok ( ) bad
Item 3 | ( ) good ( ) ok ( ) bad
[…]
After a radio button has been checked, I immediately want to submit and process the rating. The current code for the dataTable looks as follows:
<h:dataTable value="#{bean.list}" var="item">
<h:column>
<h:outputText value="#{item.title}"/>
</h:column>
<h:column>
<h:form>
<h:selectOneRadio id="rate" value="#{bean.rate}">
<f:selectItem itemValue="1" itemLabel="good" />
<f:selectItem itemValue="0" itemLabel="ok" />
<f:selectItem itemValue="-1" itemLabel="bad" />
<f:ajax event="click" execute="rate" />
</h:selectOneRadio>
</h:form>
</h:column>
</h:dataTable>
The problem I am now facing: I need to determine, which item was actually rated. Can somebody give me a hint how I can achieve this?
Upvotes: 0
Views: 6367
Reputation: 305
Adding "rate" property to the Item class will help you determine easily which Item is rated.
<h:selectOneRadio id="rate" value="#{bean.rate}"
<f:selectItem itemValue="1" itemLabel="good" />
<f:selectItem itemValue="0" itemLabel="ok" />
<f:selectItem itemValue="-1" itemLabel="bad" />
<f:ajax event="click" execute="rate" />
</h:selectOneRadio>
Change the above code to
<h:selectOneRadio id="rate" value="#{item.rate}">
<f:selectItem itemValue="1" itemLabel="good" />
<f:selectItem itemValue="0" itemLabel="ok" />
<f:selectItem itemValue="-1" itemLabel="bad" />
<f:ajax event="click" execute="rate" />
</h:selectOneRadio>
Upvotes: 0
Reputation: 3573
You can choose among several techniques. For example, you could use a Map
in your bean
<h:selectOneRadio value="#{bean.ratePerItem[item]}"
Map<ItemClass, Integer> ratePerItem;
That way you must walk through the map looking for values. A simpler approach for your needs might be using a h:inputHidden
which stores the item.
Or, maybe better, f:setPropertyActionListener
. It depends on what you exactly want.
Upvotes: 1