My Question is related to jsf.
I am trying to make a jsf dataTable with radio buttons.
For e.g. In the first row, there are 5 columns: The first column has a question and rest 4 are radio buttons.: I want the user to select only one radio button out of the 4 in a single row and repeat the same process for all the rows.
I have read the following link where a javascript code is given for selecting one row using radio-buttons: How to group Radio Buttons in h:datatable jsf2.0 and http://www.javabeat.net/how-to-use-hselectoneradio-inside-hdatatable-in-jsf/
But my requirement is to select one of the radio-button in a row
I got some clue here: http://www.javaworld.com/article/2077687/enterprise-java/group-radio-buttons-inside-a-jsf-datatable-component.html but I would rather prefer a javascript code to do this activity rather than going for a custom tag, if possible.Or if there is another way to do the same in pure jsf.
Edit:
Code in jsp:
<h:dataTable var="row" value="#{rateYourBillerBean.questionsList}">
<h:column>
<f:facet name="header">
<h:outputText value="Question" />
</f:facet>
<h:outputText value="#{row.question}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Poor" />
</f:facet>
<h:selectOneRadio styleClass="rad" onclick="uncheckOthers(this)" value="#{row.poor}">
<f:selectItem itemValue="1" />
</h:selectOneRadio>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Average" />
</f:facet>
<h:selectOneRadio styleClass="rad" onclick="uncheckOthers(this)" value="#{row.average}">
<f:selectItem itemValue="2" />
</h:selectOneRadio>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Good" />
</f:facet>
<h:selectOneRadio styleClass="rad" onclick="uncheckOthers(this)" value="#{row.good}">
<f:selectItem itemValue="3" />
</h:selectOneRadio>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Excellent" />
</f:facet>
<h:selectOneRadio styleClass="rad" onclick="uncheckOthers(this)" value="#{row.excellent}">
<f:selectItem itemValue="4" />
</h:selectOneRadio>
</h:column>
</h:dataTable>
JS:
<script>
function uncheckOthers(radio) {
var name = radio.name.substring(radio.name.lastIndexOf(':'));
var elements = radio.form.elements;
for (var i = 0; i < elements.length; i++) {
if (elements[i].name.substring(elements[i].name.lastIndexOf(':')) == name) {
elements[i].checked = false;
}
}
radio.checked = true;
}
</script>
The problem is that each radio button in the consecutive rows have the same value for radio.name.substring(radio.name.lastIndexOf(':'))
but the radio buttons in the same row doesn't have the same name as I checked in "View Source" of browser. So how to overcome that.