Search code examples
jsfdatatableradio-buttonsurveytomahawk

JSF: display dynamic values within dataTable or panelGrid


I want to create my own jsf tag to display a table grid for my project about online surveys. In the first row, there should be a title and a dynamic count of images (smileys). After first row, there should be questions and the same count of selectOneRadios like images. The result should be a table with a question column and dynamic columns for possible survey answers.

I think I need three loops. The first to display the images in the table header, the second to list all questions and the third loop to list all possible answers (or selectOneRadio) to each question. I tried to use the h:dataTable because this could loop over my questions but what's about the other dynamic data?

note: Because of our cms it's necessary that I only use jsf 1.2 components.

thanks for your help
yves beutler


Solution

  • If I got you right you want something like this:

    In the JSP:

    <h:dataTable value="#{myBean.questions}"
                       var="question">
            <h:column>
              <f:facet name="header" >
                <h:outputText value="Question"/>
              </f:facet>
              <h:outputText value="#{question.title}"/>
            </h:column>
            <h:column>
              <f:facet name="header" >
                <!-- smilies go here -->
              </f:facet>
              <h:selectOneRadio>
                <f:selectItems value="#{question.options}"/>
              </h:selectOneRadio>
            </h:column>
          </h:dataTable>
    

    In the Controller you would return a list of questions:

    public List<Question> getQuestions(){
        List<Question> questions = new ArrayList<Question>();
        questions.add(new Question("How did you like this?"));
        questions.add(new Question("How did you like that?"));
        return questions;
      }
    

    and your question class may look something like this:

    public class Question{
    
        private final String title;
    
        public Question(String title){
          this.title = title;
        }
    
        public String getTitle(){
          return title;
        }
    
        public List<SelectItem> getOptions(){
          List<SelectItem> items = new ArrayList<SelectItem>();
          items.add(new SelectItem("1", "Very much"));
          items.add(new SelectItem("2", "okay"));
          items.add(new SelectItem("3", "not that good"));
          items.add(new SelectItem("4", "bad"));
    
          return items;
        }
      }