tags:

views:

767

answers:

2

Hello!

I'm having a hard time with a selectManyCheckbox. Basically what I am doing is loading a List of Categories in a selectManyCheckbox type controller (have done this either with a List or with a List with convertEntity). My problem is with the selected elements (value="#{cardListProvider.categoriesHolder.selectedCategories}"). After some reading I understand it also has to be a List, but what kind? And how can I set the selected categories? I'm not saving them in DB but I need to run some action in the bean with them!

Here's what I have:

    <h:selectManyCheckbox id="supportCategoryCardFilter"
value="#{cardListProvider.categoriesHolder.selectedCategories}" styleClass="greyText" required="false" >

             <s:selectItems var="filterList" value="#{cardListProvider.categoriesList}" label="#{filterList.label}" />

             <a:support id="supportCategoryCardFilter2" event="onchange"
reRender="someHolder, categoriesPanel" eventsQueue="onchange" action="#{cardListProvider.findCards(cardListProvider.categoriesHolder.selectedCategories)}" />

    </h:selectManyCheckbox>

I have wasted several hours with this... Can anyone help me? Thank you

+1  A: 

You can bind to a String[] array like so:

public class CheckSelector {
  private String[] chosen;

  public String[] getChosen() { return chosen; }
  public void setChosen(String[] chosen) { this.chosen = chosen; }

  public SelectItem[] getChoices() {
    return new SelectItem[] { new SelectItem("1"), new SelectItem("2"),
        new SelectItem("3") };
  }
}

The value of the selectManyCheckbox should be bound to chosen. Alternatively, you can use a List:

public class CheckSelector {
  private List<String> chosen;

  public List<String> getChosen() { return chosen; }

  public void setChosen(List<String> chosen) { this.chosen = chosen; }

  public List<SelectItem> getChoices() {
    return Arrays.asList(new SelectItem("1"), new SelectItem("2"),
        new SelectItem("3"));
  }
}

The exact rules for value support are listed in the javadoc:

  • If the component has an attached Converter, use it.
  • If not, look for a ValueExpression for value (if any). The ValueExpression must point to something that is:
    • An array of primitives (such as int[]). Look up the registered by-class Converter for this primitive type.
    • An array of objects (such as Integer[] or String[]). Look up the registered by-class Converter for the underlying element type.
    • A java.util.List. Assume that the element type is java.lang.String, so no conversion is required.
  • If for any reason a Converter cannot be found, assume the type to be a String array.
McDowell
Thank you for your help!The setter and getter you suggested is similar to what I am doing. I've tried it as a List<String> and tried other types as well but for some reason when I debug it, the list always comes empty... Can't figure this one out..
GuilhermeA
Have you added a `<h:messages />` control to the view to see if any controls are failing conversion/validation?
McDowell
I got it to work. Honestly I cannot tell what the exact problem was! Thank you for your help! One more question please: the "chosen" is a List<String>; is it possible to use a List<Category> instead (possibly with a converter)? I think it would be better because being a List<String>, I am getting the id's of the selected categories in the list and then have to do operations to get the real Category. Thank you once again.
GuilhermeA
Yes, that should be possible by implementing a custom Converter: http://java.sun.com/javaee/5/docs/api/javax/faces/convert/Converter.html But you'll just be moving the lookup operation to another part of your application - the browser is always going to send the server strings.
McDowell
Thank you once again. I'll look that up! :)
GuilhermeA
A: 

I see you are using Seam so no need to use Strings or any primitive type, you can bind directly to List. You just have to add another tag inside your selectManyCheckbox component which is and it will automatically do everything.

Better than doing all by yourself, check Seam documentation

http://docs.jboss.org/seam/2.2.0.GA/reference/en-US/html/controls.html#d0e28378

kpolice