Dowemo


Question:

I've the below form:

<h:form>


    <h:dataTable value="#{bean.items}" var="item">


        <h:column>


            <h:selectBooleanCheckbox  value="#{item.enabled}" valueChangeListener="#{bean.onchangeEnabled}">


                <f:ajax event="change" />


            </h:selectBooleanCheckbox>


        </h:column>


        <h:column>#{item.name}</h:column>


    </h:dataTable>


</h:form>


I would like to get #{item} or at least #{item.name} in the value change listener method:

public void onchangeEnabled(ValueChangeEvent e) {


    // I would like to get #{item.name} here too.


}


How can I achieve this?


Best Answer:


First of all, the valueChangeListener is the wrong tool for the job. Use <f:ajax listener>. Second, event="change" is the wrong choice in case of checkboxes/radiobuttons because their physical value actually never changes. You should use event="click", but this is the default already, so you can just omit it.

All in all, the proper initial code should look like this:

<h:selectBooleanCheckbox value="#{item.enabled}">


    <f:ajax listener="#{bean.onchangeEnabled}" />


</h:selectBooleanCheckbox>


with

public void onchangeEnabled(AjaxBehaviorEvent event) { // Note: event argument is optional.


    // ...


}


Once fixed it like that, then you can easily make use of EL 2.2 capability to pass method arguments:

<h:selectBooleanCheckbox value="#{item.enabled}">


    <f:ajax listener="#{bean.onchangeEnabled(item)}" />


</h:selectBooleanCheckbox>


with

public void onchangeEnabled(Item item) {


    // ...


}


See also:




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs