7 Replies Latest reply on Mar 10, 2010 6:51 AM by brendanrichards

    How to get Multiple selected CheckBox's Value?

      Hi All,

      I am trying to get selected check box's value(multiple)


      JSF View.............

      <h:dataTable value="#{forward}" var="phone">
      <h:column>
      <h:selectBooleanCheckbox value="#{deleteSelections[phone]}" />
      </h:column>

      Maping in Action.........

      @Out(scope=ScopeType.CONVERSATION, required=false)
          Map<Forward, Boolean> deleteSelections;
      deleteSelections = new HashMap<Forward , Boolean>();

      And when i am trying to retrieve the value its getting Null for selected checkBox's....
      for (Forward phoneAddress : forward)
      {
                     
               System.out.println(deleteSelections.get(phoneAddress));     
      }

      Any Idea what's wrong with my code?
        • 1. Re: How to get Multiple selected CheckBox's Value?
          nickarls

          Not sure you can bind to a map like that


          I usually add a selected boolean attribute to the object in the datatable and hook the selectbox there. Then on the action, I iterate through the list to see which are selected and affected by the action.

          • 2. Re: How to get Multiple selected CheckBox's Value?

            Hi Nicklas,


            Thanks for your reply.


            I would like to try the way you have mentioned. Can you please magnify your thoughts(with any Example).


            • 3. Re: How to get Multiple selected CheckBox's Value?
              nickarls
              • 4. Re: How to get Multiple selected CheckBox's Value?
                Hi Nicklas,

                Thanks again, I am trying that solution but getting stuck for "list" in below mentioned reference  code...

                There are probably better/cleaner ways to do it but I have used a sort of

                  public class SelectedEntity {
                    private boolean selected = false;
                    private Entity entity;

                    public SelectedEntity(Entity entity) {
                      this.entity = entity;
                    }
                    ... setters / getters
                  }

                and then I return a List<SelectedEntity> to the datatable and bind the checkbox to var.selected and data to var.entity.entityproperty.

                then when I perform an action I loop

                for (SelectedEntity selectedEntity : list) {
                  if (selectedEntity.isSelected) {
                    doSomethingWith(selectedEntity.entity);
                  }
                }


                from where i will get that selected checkbox's list

                • 5. Re: How to get Multiple selected CheckBox's Value?
                  nickarls

                  I var referring to the list (whatever named) that backs the datatable. If you bind the table to


                  #{backingbean.theList}
                  



                  and there have a


                    private List<SelectedEntity> theList = new ArrayList<SelectedEntity>() // (+setters/getters
                  



                  then the iteration in the action would be


                  for (SelectedEntity selectedEntity : theList) {
                  

                  • 6. Re: How to get Multiple selected CheckBox's Value?

                    Hi Nicklas,


                    Thanks for your response :)


                    Remember, in mine 1st post i had asked for null value problem, i was getting that for multiple selection, that has resolved i had problem f: subview my logic was coming in between that block.


                    We actually don't need to hook another Boolean field for that.


                    And one more thing, do you know what actually that f: subview does, because of what i was getting Null values.


                    • 7. Re: How to get Multiple selected CheckBox's Value?
                      brendanrichards

                      Here's an alternative / simple way to do multiple checkboxes within a data table. With this method you dont need to bind your checkboxes to anything server side except the ids of the object you weant to select.
                      PHP coders will immediately recognise this...


                      1) put a plain old


                      <input type="checkbox" name="toDelete" value="#{item.id}">



                      tag in your table column,  binding the id of the record you want to select


                      2) In your action:


                      - get a HttpServletRequest Object:


                      HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();




                      - request.getParameterValues(toDelete) will return a String array of selected ids