1

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Caau Dung Ngok
  • 109
  • 3
  • 15
  • You can pass it like here http://stackoverflow.com/a/10398023/617373 or here http://stackoverflow.com/a/8385723/617373 or iterate over the `list` and grab the names of *enabled* objects... – Daniel Jan 14 '13 at 09:16

2 Answers2

7

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Could you elaborate on why event="change" is the wrong choice? He is interested in a value change and not in a mouse click (would event="click" fire when changing the value with the keyboard? – Martin Höller Nov 20 '14 at 16:05
0

For selectBooleanCheckbox it only reacts on the event click and the form should be posted.

so add this to the checkbox

valueChangeListener="#{mybean.myfunction}" onchange="submit();"

it should get fired !

Benjamin Fuentes
  • 674
  • 9
  • 22