Note that there are some explanatory texts on larger screens.

plurals
  1. POStore values from multiple select many checkboxes to a single object
    text
    copied!<p>Suppose I have to render multiple checkboxes inside a data table as shown below:</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:c="http://java.sun.com/jstl/core" &gt; &lt;h:head&gt; &lt;!-- &lt;h:outputStylesheet library="css" name="table-style.css" /&gt; --&gt; &lt;/h:head&gt; &lt;h:body&gt; &lt;h1 align="center"&gt;Generate Exam&lt;/h1&gt; &lt;hr/&gt; &lt;h:form id="form"&gt; &lt;h:panelGrid columns="2" width="100%"&gt; &lt;h:panelGroup layout="block" style="text-align:center"&gt; &lt;h3&gt;Level&lt;/h3&gt; &lt;h:selectOneMenu value="#{examBean.candidateLevel}"&gt; &lt;f:selectItem itemValue="SR" itemLabel="Senior Level" /&gt; &lt;f:selectItem itemValue="MD" itemLabel="Mid Level" /&gt; &lt;f:selectItem itemValue="JR" itemLabel="Junior Level" /&gt; &lt;/h:selectOneMenu&gt; &lt;/h:panelGroup&gt; &lt;h:panelGroup layout="block" style="text-align:center"&gt; &lt;h3&gt;Candidate&lt;/h3&gt; &lt;h:selectOneMenu value="#{examBean.selectedCandidateId}"&gt; &lt;f:selectItems value="#{examBean.candidateList}" var="candidate" itemLabel="#{candidate.name}" itemValue="#{candidate.id}"/&gt; &lt;/h:selectOneMenu&gt; &lt;/h:panelGroup&gt; &lt;/h:panelGrid&gt; &lt;hr/&gt; &lt;h:dataTable value="#{examBean.parentCategoryList}" var="cat" binding="#{table}"&gt; &lt;h:column&gt; &lt;h:outputText value="#{cat.name}"/&gt; &lt;h:commandButton id="button" type="button" value="+" onclick="expand('#{table.rowIndex}');"/&gt; &lt;br/&gt; &lt;h:selectManyCheckbox id="checkbox" value="#{examBean.selectedCategoryList}" style="display:none"&gt; &lt;f:selectItems value="#{cat.subCategoryList}" var="sub" itemLabel="#{sub.name}" itemValue="#{sub.id}"/&gt; &lt;/h:selectManyCheckbox&gt; &lt;/h:column&gt; &lt;/h:dataTable&gt; &lt;hr/&gt; &lt;h:panelGrid columns="1" width="100%" layout="block" style="text-align:center"&gt; &lt;h:panelGroup style="text-align:center"&gt; &lt;h:commandButton value="Generate Exam" action="#{examBean.generateExam()}" /&gt; &lt;h:commandButton value="Reset" type="reset" /&gt; &lt;/h:panelGroup&gt; &lt;/h:panelGrid&gt; &lt;/h:form&gt; &lt;script type="text/javascript"&gt; function expand(val) { var button = "form:j_idt18:" + val + ":button"; var checkbox = "form:j_idt18:" + val + ":checkbox"; var buttonElement = document.getElementById(button); var checkboxElement = document.getElementById(checkbox); if(checkboxElement.style.display == 'none') { checkboxElement.style.display = 'block'; buttonElement.value = '-'; } else { checkboxElement.style.display = 'none'; buttonElement.value = '+'; } } &lt;/script&gt; &lt;/h:body&gt; &lt;/html&gt; </code></pre> <p>The <strong>value</strong> attribute in all the checkboxes are the same - <code>#{examBean.selectedCategoryList}</code>. <code>selectedCategoryList</code> is declared as a <code>List&lt;String&gt;</code>property in my bean as shown below:</p> <pre><code>package com.gtp.iqp.presentation.managedBeans; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.gtp.iqp.business.bo.Candidate; import com.gtp.iqp.business.bo.Category; import com.gtp.iqp.business.delegate.CandidateDelegate; import com.gtp.iqp.business.delegate.CategoryDelegate; import com.gtp.iqp.business.delegate.ExamDelegate; import com.gtp.iqp.presentation.dto.CandidateSubCategoryList; @SuppressWarnings("serial") @Component public class ExamBean extends BaseManagedBean { @Autowired private CategoryDelegate categoryDelegate; @Autowired private CandidateDelegate candidateDelegate; @Autowired private ExamDelegate examDelegate; private List&lt;Category&gt; parentCategoryList; private List&lt;String&gt; selectedCategoryList; private List&lt;Candidate&gt; candidateList; private String selectedCandidateId; private String candidateLevel; public ExamDelegate getExamDelegate() { return examDelegate; } public void setExamDelegate(ExamDelegate examDelegate) { this.examDelegate = examDelegate; } public String getSelectedCandidateId() { return selectedCandidateId; } public void setSelectedCandidateId(String selectedCandidateId) { this.selectedCandidateId = selectedCandidateId; } public List&lt;Candidate&gt; getCandidateList() { this.candidateList = candidateDelegate.getAllCandidates(); System.out.println("Cands: " + candidateList.size()); return candidateList; } public void setCandidateList(List&lt;Candidate&gt; candidateList) { this.candidateList = candidateList; } public CandidateDelegate getCandidateDelegate() { return candidateDelegate; } public void setCandidateDelegate(CandidateDelegate candidateDelegate) { this.candidateDelegate = candidateDelegate; } public String getCandidateLevel() { return candidateLevel; } public void setCandidateLevel(String candidateLevel) { this.candidateLevel = candidateLevel; } public CategoryDelegate getCategoryDelegate() { return categoryDelegate; } public void setCategoryDelegate(CategoryDelegate categoryDelegate) { this.categoryDelegate = categoryDelegate; } public List&lt;Category&gt; getParentCategoryList() { selectedCategoryList = new ArrayList&lt;String&gt;(); parentCategoryList = categoryDelegate.getParentCategories(); return parentCategoryList; } public void setParentCategoryList(List&lt;Category&gt; parentCategoryList) { this.parentCategoryList = parentCategoryList; } public List&lt;String&gt; getSelectedCategoryList() { return selectedCategoryList; } public void setSelectedCategoryList(List&lt;String&gt; selectedCategoryList) { this.selectedCategoryList = selectedCategoryList; } public void generateExam() { System.out.println("Chosen categories:"); for(String cat: selectedCategoryList) { System.out.println(cat); } System.out.println(); System.out.println("Chosen level: " + candidateLevel); System.out.println(); System.out.println("Chosen candidate: " + selectedCandidateId); //examDelegate.generateExam(selectedCategoryList, candidateLevel, selectedCandidateId); } } </code></pre> <p>The problem I've is that once the form is submitted I only see the values from the last checkbox in my bean property <code>selectedCategoryList</code>. Can someone please tell me how do I collect the values from all the checkboxes? I'm forced to give different Ids for each checkbox because I want to be able to collapse each one of them separately on a button click. I tried changing the bean property <code>selectedCategoryList</code> to be a <code>List&lt;List&lt;String&gt;&gt;</code>. And then on my xhtml page I tried to set the value attribute of my checkbox to be something like </p> <pre><code>&lt;h:selectManyCheckbox id="checkbox_#{status.index}" value="#{examBean.selectedCategoryList[status.index]}" style="display:none"&gt; </code></pre> <p>It didn't really work, I got an array out of bounds exception for 0. The list was initialized, so not sure how I was getting an array out of bounds exception.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload