Avatar of prain
prain
Flag for United States of America asked on

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

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

Java EEJavaProgramming Languages-Other

Avatar of undefined
Last Comment
Valeri

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Valeri

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Your help has saved me hundreds of hours of internet surfing.
fblack61