0

I have a loop with a bunch of h:selectBooleanCheckboxs inside a ui:repeat and I want to be able to check / uncheck all of them with one click. Following this this is how I try to do it:

<h:selectBooleanCheckbox value="#{bean.selectAll}" valueChangeListener="#{bean.selectAllCustomers}">
    <f:ajax execute="@form" render="entireLoop"/>
</h:selectBooleanCheckbox>

The loop:

<h:panelGroup id="entireLoop">  
  <ui:repeat var="customer" varStatus="status" value="#{bean.customers}">

    <tr> 
        <td>
    <h:selectBooleanCheckbox value="#{bean.customersMap[customer]}"/>
        </td>
      <td><h:commandLink value="#{customer.userID}" action="#{navBean.gotoEditCustomer(customer)}"/></td>
      <td>#{bean.getContact(user)}</td>
      <td>#{customer.companyName}</td>
      <td>#{customer.email}</td>
      <td>#{customer.phone}</td>
    </tr>       
  </ui:repeat>  
</h:panelGroup>

The code in the bean:

public void selectAllCustomers() {
    Iterator<User> keys = customersMap.keySet().iterator();
    while(keys.hasNext()) {
        User user = keys.next();
        customersMap.put(user, selectAll);
    }
}

When I click to check the "select all" checkbox, I have two problems:

  1. Nothing happens. Only when I click on somewhere else in the page the method is called. How can I get it to go directly?

  2. The entire loop disappears from the page. Why? (I tried to get the ajax to render only the selectBooleanCheckbox by giving it an id, or a name, but I'm getting an error saying this is not a naming component.)

Community
  • 1
  • 1
Herzog
  • 923
  • 1
  • 14
  • 28

1 Answers1

1

Instead of using valueChangeListener, your method will work if you use the listener attribute of the <f:ajax> tag. It would be something like this:

<h:selectBooleanCheckbox value="#{bean.selectAll}">
    <f:ajax listener="#{bean.selectAllCustomers}" render="entireLoop"/>
</h:selectBooleanCheckbox>

Besides, you don't need to submit the whole form with execute="@form". You only need to submit selectAll property and re-render the entireLoop.

If you really want to use valueChangeListener, you must note that your method will be triggered before the new value is applied in the selectAll property. As a result, your entireLoop will not be updated until the next request. To use valueChangeListener, your listener method should look like this:

public void selectAllCustomers(ValueChangeEvent e) {
    boolean newSelectAll = (Boolean) e.getNewValue();
    Iterator<Users> keys = customersMap.keySet().iterator();
    while(keys.hasNext()) {
        Users user = keys.next();
        customersMap.put(user, newSelectAll);
    }
}
Mr.J4mes
  • 9,168
  • 9
  • 48
  • 90
  • Thanks for your response, Mr.J4mes. But when I follow your advice I get the following error: clientError: Error MyFaces ERROR: Affected Class:myfaces._impl.xhrCore._AjaxResponse. Affected Method:processResponse Error Name:myfaces._impl._util._Dom.findByTagNames: fragment must be set – Herzog Jan 05 '12 at 07:07
  • btw. I changed my html table to h:dataTable and the silly disappearing act whenever I checked the box went away. The listener is still not doing it until I click on another place on the page. I guess a valueChangedListener is waiting for a "confirmation" that the value changed -- for example when the user clicks somewhere else -- before doing its work? – Herzog Jan 05 '12 at 13:59
  • @user1037343: :P Sry I missed your earlier comment. That error looks weird to me :). As I mentioned, have you tried out `e.getNewValue();`? Your `valueChangeListener` is triggered before your `selectAll` property get updated. As a result, you will not see what you want until next request. – Mr.J4mes Jan 05 '12 at 14:03
  • Yes, I'm using your suggestion in the method. But it still doesn't work. – Herzog Jan 05 '12 at 19:28
  • OK, I checked it out of eclipse and this works as expected in Firefox and Chrome, but not in IE. Any idea how to solve this cross browser problem? – Herzog Jan 08 '12 at 17:29
  • @user1037343 hmmm.. most of the work are done on the server-side and result is returned to client's browser. I think you should check if your IE's JavaScipt is enabled properly. – Mr.J4mes Jan 08 '12 at 18:51