jsf - How to use h:selectOneRadio in h:dataTable to select single row? -
i have list of pages displayed in table. each page has property homepage , want in datatable have column of radio buttons binded on property, , user can check 1 value. how can value on server side?
i saw examples following: http://jforum.icesoft.org/jforum/posts/list/14157.page, know best practice in such case.
as per jsf spec issue 329 have implemented jsf 2.3. new group
attribute able group radio buttons in repeater component.
<h:datatable value="#{bean.items}" var="item"> <h:column> <h:selectoneradio group="foo" value="#{bean.selecteditem}"> <f:selectitem itemvalue="#{item}" /> </h:selectoneradio> </h:column> </h:datatable>
it available per mojarra 2.3.0-m07.
before jsf 2.3, not trivial standard jsf <h:selectoneradio>
. basically, radio button in every row needs grouped each other using same input name
other radio buttons unchecked whenever select one. not been grouped, have own name
, other radio buttons never unchecked.
component libraries primefaces have solved providing special attribute or column component. see this showcase example uses <p:column selectionmode="single">
generate single selection column. selected value referenced selection
attribute of <p:datatable>
. if you're using component library , has such component you, should use it.
in standard jsf <h:datatable>
<h:selectoneradio>
you'd need bring in javascript workaround follows unchecks other radio buttons in same column:
<h:datatable value="#{bean.items}" var="item"> <h:column> <h:selectoneradio valuechangelistener="#{bean.setselecteditem}" onclick="datatableselectoneradio(this);"> <f:selectitem itemvalue="null" /> </h:selectoneradio> </h:column> ... </h:datatable>
with
public void setselecteditem(valuechangeevent event) { facescontext context = facescontext.getcurrentinstance(); selecteditem = context.getapplication().evaluateexpressionget(context, "#{item}", item.class); }
and
function datatableselectoneradio(radio) { var radioid = radio.name.substring(radio.name.lastindexof(':')); (var = 0; < radio.form.elements.length; i++) { var element = radio.form.elements[i]; if (element.name.substring(element.name.lastindexof(':')) == radioid) { element.checked = false; } } radio.checked = true; }
Comments
Post a Comment