Javascript to Select One Radio Button
function selectOne(form, button) { turnOffRadioForForm(form); button.checked = true; } function turnOffRadioForForm(form) { for (var i = 0; i < form.elements.length; i++) { form.elements[i].checked = false; } }
JSF Code of h:dataTable Table on JSP Page
We will use valueChangeListener for this, as when user check the radio button we will get the binded value of bean. It is one column. NOTE: Please remove all spaces after : sign. In f and h tags.
<h:column> <f:facet name=”header”> <f:verbatim>Select</f:verbatim> </f:facet> <h:selectOneRadio id="selectRadio" value="#{depositMoneyPageBean.creditCardDataBean.cardId}" valueChangeListener="#{depositMoneyPageBean.handleRadioValueChange}" onclick="selectOne(this.form , this)" > <f:selectItem itemValue="#{creditCardData.cardId}" itemLabel=" " /> </h:selectOneRadio> </h:column>
Page Bean Code
This is the page bean code, and here is the valueChangeListener method.
public void handleRadioValueChange(ValueChangeEvent valueChangedEvent){ try { // here we got the selected row of dataTable this.creditCardDataBean = (CreditCardDataBean)getTable1().getRowData(); selectedCardId = creditCardDataBean.getCardId(); }catch (Exception e){ e.printStackTrace(); } } public HtmlDataTable getTable1(){ if (table1 == null) { table1 = (HtmlDataTable) findComponentInRoot("table1"); } return table1; } public static UIComponent findComponentInRoot(String id) { UIComponent ret = null; FacesContext context = FacesContext.getCurrentInstance(); if (context != null) { UIComponent root = context.getViewRoot(); ret = findComponent(root, id); } return ret; } public static UIComponent findComponent(UIComponent base, String id) { // Is the "base" component itself the match we are looking for? if (id.equals(base.getId())) { return base; } // Search through our facets and children UIComponent kid = null; UIComponent result = null; Iterator kids = base.getFacetsAndChildren(); while (kids.hasNext() &amp;&amp; (result == null)) { kid = (UIComponent) kids.next(); if (id.equals(kid.getId())) { result = kid; break; } result = findComponent(kid, id); if (result != null) { break; } } return result; }
where findComponent method is decleared?
Dear Khalled;
It may be I missed some methods to mention. I will surely mention it if I able to find out the code from my machine. Well thanks for pointing.
Code is updated, findComponent is there.
hope it works.