How to group Radio Buttons in h:datatable jsf2.0

Java Ka Baby picture Java Ka Baby · Aug 22, 2011 · Viewed 7k times · Source

I have a column of radio buttons in h:datatable in JSF2 but I can not find a way to group them. Meaning all of them can be selected at the same time where as whole point of radio button is so that only one can be selected at any given time. I am sure there will be standard way of doing it.

I am using myfaces. Can use richfaces if really needed to.

Can any one help with this.

Answer

BalusC picture BalusC · Aug 22, 2011

When using standard JSF <h:selectOneRadio> component inside a <h:dataTable> you'll need to bring a shot of JavaScript into the game which unchecks all other radio buttons in the same column when one is checked.

<h:column>
    <h:selectOneRadio onclick="uncheckOthers(this);">
</h:column>

with

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