简体   繁体   中英

JSF dataTable with radio buttons

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?

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.

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>

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