Accessing a selected row from h:dataTable

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(&quot;table1&quot;);
}
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 &quot;base&quot; 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;&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;
}

You May Also Like

3 Comments

  1. 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.

Leave a Reply to Khalled Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.