0

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Chandan Sarma
  • 308
  • 2
  • 5
  • 19
  • 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 Sep 15 '14 at 11:24
  • I will post as an answer ok, to close this question, can be? – Cold Sep 15 '14 at 11:27
  • not please help me to solve it as i am not finding solution – Chandan Sarma Sep 15 '14 at 12:38

2 Answers2

0

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()

Alexey Semenyuk
  • 3,263
  • 2
  • 32
  • 36
  • My problem is i need a the checkboxes. if is check its should show true otherwise false – Chandan Sarma Sep 13 '14 at 12:04
  • What's problem? You have examinationFormBean.selectedExaminationParts and examinationFormBean.examinationParts lists. So you can return some other object not only examinationPart model, if you want, for this need to realize converter with getAsObject and getAsString methods http://stackoverflow.com/questions/19883771/how-to-use-selectmanycheckbox-with-two-arraylist-primefaces. – Alexey Semenyuk Sep 13 '14 at 12:45
0

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
   }
}
...
Cold
  • 787
  • 8
  • 23