0
votes

I am new to primefaces and i have a problem to save my primefaces SelectManyCheckbox value to database. I am using hibernate and mysql. The sample code are give as below

My xhtml pages code is:

   <h:outputText value="#{msg['elicense.examinationform.personal.classofcertificates']}"/> 
        <p:selectManyCheckbox id="grid" value="#{examinationFormBean.selectedClass}" layout="grid" columns="1">
            <f:selectItems value="#{examinationFormBean.examinationPart}"var="className" itemLabel="#{className.name}" itemValue="#{className}" />
      </p:selectManyCheckbox>

My bean is:

  private String[] selectedClass;
  private List<CertificateClass> examinationPart=new ArrayList<CertificateClass>();
  getter()
  setter()

The method where I want to save my checkbox is:

        private void saveExaminationDetails()
          {
             examDetails.setElementaryPrinciples();  //bolean field
             examDetails.setLightinig()
             //no of setter
           }

I am not able to find out how I will set the selected and not selected checkbox value on the method

2
If i'm not wrong, the sele p:selectManyCheckbox save the select values on a String Collection(List, ArrayList... etc). And you just need to save each elemnt on the Collection. - Cold
I will post as an answer ok, to close this question, can be? - Cold
not please help me to solve it as i am not finding solution - Chandan Sarma

2 Answers

0
votes

Look at primefaces showcases: http://primefaces-rocks.appspot.com/ui/selectManyCheckbox.jsf

Selected values from examinationFormBean.examinationPart should setting in p:selectManyCheckbox attribute value and then you can used this selected list in bean method. For your example should be something:

    <p:selectManyCheckbox id="grid" value="#{examinationFormBean.selectedExaminationParts}" layout="grid" columns="1">
                <f:selectItems value="#{examinationFormBean.examinationParts}" var="className" itemLabel="#{className.name}" itemValue="#{className}" /> 
</p:selectManyCheckbox>

And then you can use selectedExaminationParts in your saveExaminationDetails()

0
votes

The p:selectManyCheckbox select values are biding a String Collection(List, ArrayList... etc) on managed bean. You just need to save each String existent on the Collection.

I will give you an example showing how you can do that:

Example:

...
@Named(value = "myBean")
@SessionScoped
public class InscricaoBean implements Serializable {
...
private List<String> selectedElemnts = new ArrayList();

//selectedElements get and set
...

On JSF you have something like:

...
<h:outputText value="#{msg['elicense.examinationform.personal.classofcertificates']}"/> 
        <p:selectManyCheckbox id="grid" value="#{examinationFormBean.selectedElemnts}"...>
            <f:selectItems value="#{examinationFormBean.examinationPart}"var="className"   
            itemLabel="#{className.name}" itemValue="#{className}" />
      </p:selectManyCheckbox>
...

On save method:

...
private void saveExaminationDetails()
{
   for (String nameAux: selectedElemnts )
   {
       //you save the data here
   }
}
...