2 Replies Latest reply on Dec 12, 2006 7:25 AM by lara

    How to send a parameter with checkbox in JSF in order to get

    lara

      I have an object of type Group. Each group has an ID, name & description. I want to list the groups and add a checkbox next to each one. In the form, I have a button Add, when it is clicked, I should be able to get first all the groups selected. I want to know how can I send the ID of the checked group with the <h:selectBooleanCheckbox> because I need to have the ID in the bean in order to perform some actions. Is that possinle?

      The code in the JSF looks as follow:

      <h:dataTable var="group" value="#{groupList}" rendered="#{groupList.rowCount>0}">
      
       <h:column>
       <f:facet name="header">
       <h:outputText/>
       </f:facet>
       <h:selectBooleanCheckbox
       value="#{addExecutorToGroup.groupsSelected}">
       </h:selectBooleanCheckbox>
       </h:column>
      
       <h:column>
       <f:facet name="header">
       <h:outputText value="#{msg.name}"/>
       </f:facet>
       <h:outputText value="#{group.name}" />
       </h:column>
      
       <h:column>
       <f:facet name="header">
       <h:outputText value="#{msg.description}" />
       </f:facet>
       <h:outputText value="#{group.description}" />
       </h:column>
      
       </h:dataTable>
      


      Note that value="#{groupList}" is the List of groups and groupsSelected is a boolean variable which has it setter and getter defined in the bean.
      What I did for now is to save the list of boolean variable into an array and for example, if the second index contains a value true, I check the second element in the groupList and gets its ID from the bean. This doesn't work when I add sorting and things will be confused that is why I want to send the ID from the JSF page. Can anyone propose any solution?


      Any help is appreciated!
      Thanks in advance.
      Regards

        • 1. Re: How to send a parameter with checkbox in JSF in order to

          I guess you want to implement multiple selections in a datatable. I experienced this issue a while ago and could only work around this problem with the following approach. Its requires a bit of work but it works fine ....

          The idea is to have an object as a view with the boolean variable representing the selection status....

          As such i have a Generic View

          
          public abstract class GenericView<T> {
          
           private boolean selected=new Boolean(false);
          
           private T persistentInstance;
          
           public boolean isSelected() {
           return selected;
           }
          
           public void setSelected(boolean isSelected) {
           this.selected = isSelected;
           }
          
           public T getPersistentInstance() {
           return persistentInstance;
           }
          
           public void setPersistentInstance(T persitentInstance) {
           this.persistentInstance = persitentInstance;
           }
          }
          


          I then create a view specific for the object i want to use
          public class UserSelectionView extends GenericView<User> {
          
          }
          

          And a Mapper class which will map the view to the object and vice versa as required :
          public abstract class UserViewMapper {
          
           public static User MapViewToUser(UserSelectionView view){
           return view.getPersistentInstance();
           }
          
           public static UserSelectionView MapUserToView(User user){
          
           UserSelectionView view = new UserSelectionView();
          
           view.setPersistentInstance(user);
           view.setSelected(false);
          
           return view;
           }
          
          
          }
          
          




          In my SFSB, i then use the view as data model and a list of the object

          @DataModel
           private List<UserSelectionView> userViewListing = new LinkedList<UserSelectionView>();
          
          
          @Factory("userViewListing")
           public void ListUserSelectionViews(){
          
           List<User> userList = userDAO.selectAll();
          
           for(User user:userList){
           userViewListing.add(UserViewMapper.MapUserToView(user));
           }
          
          }
          
          
          public void deleteSelectedUser(){
          
          for(UserSelectionView userView:userViewListing){
           if(userView.isSelected()){
           userDAO.remove(UserViewMapper.MapViewToUser(userView));
           }
          }
          
          }
          



          Let me know how it goes ...

          Cheers,
          Jankee Yogesh
          Software Developer
          http://www.m-itc.net/

          • 2. Re: How to send a parameter with checkbox in JSF in order to
            lara

            Thank you very much for replying!

            I am trying your approach but I am having problems and nothing is listed in the page, seems the list is always empty. Can you please show me your JSF page? Should I keep the <h:selectBooleanCheckbox> and let its value call the setSelected method in the Generic view class?

            I will be very thankful if you can explain more what you suggested because I am reallly stuck.

            Regards