We help IT Professionals succeed at work.
Get Started

Why is my p:selectManyCheckbox items do not get selected when press the "Select All" button

1,346 Views
Last Modified: 2014-06-17
I am willing to pay 500 points if someone tell me why on earth this thing is not working... very very frustrated....

Ok here is the scenario...
1. I have a "Select All" button followed by a list of check boxes.
I have two probelms...

1. When I Click the "Select All" button, no check box get selected... But I can see the event fires to the backer bean
2. If I select items one by one manually, then I go to the previos page by clicking the "Back" button on the browser, and then
    come back to the same page.... the SELECTED CHECK BOXES are now DE-SELECTED and the state client side state is lost.

Please see the code below... Could be a very simple thing I do not see...

EXPERTS PLEASE HELP...
I WILL GIVE YOU EVEN 1000 points if someone tell me the answer.... VERY FRUSTRATED.....


HERE  IS THE CLIENT CODE....

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:c="http://java.sun.com/jsp/jstl/core"
  xmlns:p="http://primefaces.org/ui"
  xmlns:fn="http://java.sun.com/jsp/jstl/functions"
  xmlns:cs="http://java.sun.com/jsf/composite/components"
  >
	
					
    <h:inputText id="userSelectedCheckBoxes" 
			             style="display:none" 
	
value="#{myBean.clientSideSelectedItems}" />
			             
    <p:remoteCommand name="updateServerSideList" 
	             actionListener="#{myBean.updateServerSideList}" 
	             process="@this,userSelectedCheckBoxes" />
						
    <script type="text/javascript">
			  // CDATA is required because of the displayState
var contains xml.
			  //<![CDATA[
              function processAndUpdateServerSide()
              {
                //alert("Hello World");
                var strUserSelectedCheckBoxes ="";
                for (var i = 0; i < 4;i++) {
                if
($("input[name='frmMain:briefList']:eq("+i+")").is(":checked")) 
                {
                  strUserSelectedCheckBoxes += "1";
                  //alert(strUserSelectedCheckBoxes);
                }
                else {
                	strUserSelectedCheckBoxes += "0";
                    }
                }
   
                //alert("Test : " + strUserSelectedCheckBoxes);
 
$('input[name$="userSelectedCheckBoxes"]').val(strUserSelectedCheckBoxes);
              
                updateServerSideList();                       
              }

			  //]]>					
     </script>
  
     <br />
 
 	
       <h:panelGrid id="aPnlGrid" style="margin:10px;"> <p:commandButton value="Select All" actionListener="#{myBean.selectAll()}" update="briefList"  >
		</p:commandButton> 
		<br />
 

        <p:selectManyCheckbox id="briefList" 
                              value="#{myBean.chosenCodes}" 
                              onchange="processAndUpdateServerSide();" 
                              layout="pageDirection" 
                              immediate="true"
                              var="chosenCodeIterator" >     
                                      
          <f:selectItems value="#{myBean.cbReportMeaning}"  
                         itemValue="#{myBean.itemValueSet}"/>   
          
        </p:selectManyCheckbox> 
        
		<br /><br />
	
	<p:commandButton value="Review Selected Sections"
update="reviewChosenList" oncomplete="selectBriefDlg.show()"/>
		<br></br>
		<p:commandButton value="DeSelect All"
action="#{myBean.deSelectAll()}" />
	</h:panelGrid>
	 

</ui:composition>
</html>

Open in new window


 HERE IS THE BACK BEAN

@ManagedBean( name="myBean") 
@SessionScoped
public class {
 
	private List<String> chosenCodes;
  
	private String clientSideSelectedItems;
	
	/** Corresponding to itemValue property in the f:selectedItem **/
	private List<String> itemValueSet;
	
	/** Corresponding to the titles for each checkbox on the client side
*/
	private List<String> meanings;
	
  
	
	public MyBean() {
		super();
	}
	
	@PostConstruct
	public void init() throws Exception {
		super.init();
 
            itemValueSet = new ArrayList<String>();
            itemValueSet.add("AA");
            itemValueSet.add("BB");
            itemValueSet.add("CC");
            itemValueSet.add("DD");
 
            meanings = new ArrayList<String>();
            meanings.add("AA Button);
            meanings.add("BB Button);
            meanings.add("CC Button);
            meanings.add("DD Button);

            chosenCodes = new ArrayList<String>(); 	 
	}
 
  
	
	/**
	 * @return the chosenCodes
	 */
	public List<String> getChosenCodes() {
		return chosenCodes;
	}

	/**
	 * @param chosenCodes the chosenCodes to set
	 */
	public void setChosenCodes(List<String> chosenCodes) {
		this.chosenCodes = chosenCodes;
	}
 
 	

	public void selectAll() {
		
		System.out.println("ENTERED SELECT ALL");
		
		/** Clean Previous Selections Stored */
		chosenCodes.clear();
		
		chosenCodes.addAll(getCareerBriefFullList());

		
		for (String str : chosenCodes)
		{
			System.out.print(str + ",");
		}
		System.out.println();
		
		persistData();  /** Auto save to Database */
	}

 
				
	public void onChange() {
		System.out.println("Checked In");
		System.out.println("chosenCodes Size : " +
chosenCodes.size());
		for (String str : chosenCodes)
		{
			System.out.println(str);
		}
			
		System.out.println("=================================="); 
	}	
		

		
	public void updateServerSideList()
	{
	    int length = clientSideSelectedItems.length();
	    chosenCodes.clear();
	 
    	    for (int nextItem=0 ; nextItem< length; nextItem++)
	    {
		if (clientSideSelectedItems.charAt(nextItem) == '1')
		{
			chosenCodes.add(itemValueSet.get(nextItem));
		}
	    }
			
			
	    for (String str : chosenCodes)
			System.out.println("Client chose : " + str);
	    }
		
	 
        }
	 
	public List<String> getItemValueSet() {
			return itemValueSet;
	}

	public void setItemValueSet(List<String> itemValueSet) {
			this.itemValueSet = itemValueSet;
	}

	public String getClientSideSelectedItems() {
			return clientSideSelectedItems;
	}

	public void setClientSideSelectedItems(String
clientSideSelectedItems) {
			this.clientSideSelectedItems =
clientSideSelectedItems;
	}
 
		 
	public List<String> getMeanings() {
			return meanings;
	}

	public void setMeanings(List<String> meanings) {
			this.meanings = meanings;
	}
 
}

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE