0 Replies Latest reply on Feb 17, 2012 7:15 AM by darkhawk

    DataTable Problem

    darkhawk Newbie

      I want to add datas to datable but i have a problem that it doesnt add data into new row, it inserts first row.

      When i click submit button, it inserts datas into datatable on second.xhtml page but if i click back button and again insert datas with submit button, it won't insert into second row.

       

      form.xhtml

       

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

      <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:ui="http://java.sun.com/jsf/facelets"

                xmlns:a4j="http://richfaces.org/a4j"

                xmlns:rich="http://richfaces.org/rich">

      <head>

      <title>Ana Sayfa</title>

      </head>

      <body bgcolor="#c8d8f8">

                <rich:panel>

                          <h:form id="form" prependId="false">

                                    <h:panelGrid columns="1" width="100%">

                                              <b><h:outputText value="User Registration" /> </b>

                                              <br />

                                              <b><h:outputText value="First Name" /> </b>

                                              <h:inputText value="#{formBean.firstName}" />

       

       

                                              <b><h:outputText value="Last Name" /> </b>

                                              <h:inputText value="#{formBean.lastName}" />

       

       

                                              <b><h:outputText value="Email" /> </b>

                                              <h:inputText value="#{formBean.email}" />

                                              <br />

                                              <b><h:outputText value="Birthday" /> </b>

       

       

                                              <h:panelGrid id="panel" columns="2" columnClasses="ecol1, ecol2">

                                                        <a4j:outputPanel id="calendar" layout="block">

                                                                  <rich:calendar value="#{calendarBean.selectedDate}"

                                                                            locale="#{calendarBean.locale}" popup="#{calendarBean.popup}"

                                                                            datePattern="#{calendarBean.pattern}" cellWidth="24px"

                                                                            cellHeight="22px" style="width:200px" />

                                                        </a4j:outputPanel>

                                              </h:panelGrid>

       

       

                                              <b><h:outputText value="What languages do you program in?" /> </b>

                                              <br />

                                              <h:selectManyCheckbox value="#{formBean.languages}">

                                                        <f:selectItem itemValue="JAVA" itemLabel="JAVA" />

                                                        <f:selectItem itemValue="C#" itemLabel="C#" />

                                                        <f:selectItem itemValue="PHP" itemLabel="PHP" />

                                              </h:selectManyCheckbox>

                                              <b><h:outputText

                                                                  value="How often can we notify you regarding your interests?" />

                                              </b>

                                              <h:selectOneRadio value="#{formBean.notify}">

                                                        <f:selectItem itemLabel="Weekly" itemValue="Weekly" />

                                                        <f:selectItem itemLabel="Monthly" itemValue="Monthly" />

                                                        <f:selectItem itemLabel="Quarterly" itemValue="Quarterly" />

                                              </h:selectOneRadio>

                                              <h:outputText value="Country" />

                                              <rich:comboBox suggestionValues="#{formBean.contries}"

                                                        directInputSuggestions="true" defaultLabel="Enter Contry"

                                                        itemSelectedClass="formBean.country"

                                                        valueChangeListener="#{formBean.valueChanged}" />

       

       

                                              <br />

                                              <h:commandButton value="Submit" action="GO_TARGET" />

                                              <br />

       

                                    </h:panelGrid>

       

                          </h:form>

                </rich:panel>

      </body>

      </html>

       

      second.xhtml

       

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

      <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:ui="http://java.sun.com/jsf/facelets"

                xmlns:a4j="http://richfaces.org/a4j"

                xmlns:rich="http://richfaces.org/rich">

      <head>

      <title>Informations</title>

      </head>

      <body bgcolor="#c8d8f8">

                <f:view>

                          <rich:panel>

                                                        <rich:dataTable value="#{formBean.dataList}" var="dataItem"

                                                                  rowClasses="row1,

                              row2" id="taskList"

                                                                  rows="4" columnClasses="50,100,100,100"

                                                                  onRowMouseOver="this.style.backgroundColor='#B5F3FB'"

                                                                  onRowMouseOut="this.style.backgroundColor='#{a4jSkin.rowBackgroundColor}'"

                                                                  width="350">

       

       

                                                                  <f:facet name="caption">

                                                                            <h:outputText value="Table" />

                                                                  </f:facet>

       

       

                                                                  <f:facet name="header">

                                                                            <h:outputText value="Accounts Database" />

                                                                  </f:facet>

       

       

                                                                  <rich:column colspan="4">

                                                                            <h:outputText value="User:" />

                                                                  </rich:column>

       

       

                                                                  <rich:column>

                                                                            <f:facet name="header">First Name</f:facet>

                                                                            <h:outputText value="#{dataItem.firstName}" />

                                                                  </rich:column>

       

       

                                                                  <rich:column>

                                                                            <f:facet name="header">Last Name</f:facet>

                                                                            <h:outputText value="#{dataItem.lastName}" />

                                                                  </rich:column>

       

       

                                                                  <rich:column>

                                                                            <f:facet name="header">Email</f:facet>

                                                                            <h:outputText value="#{dataItem.email}" />

                                                                  </rich:column>

       

       

                                                                  <rich:column>

                                                                            <f:facet name="header">Languages</f:facet>

                                                                            <h:outputText value="#{dataItem.languages}" />

                                                                  </rich:column>

       

                                                                  <rich:column>

                                                                            <f:facet name="header">Country</f:facet>

                                                                            <h:outputText value="#{dataItem.country}" />

                                                                  </rich:column>

       

                                                                  <rich:column>

                                                                            <f:facet name="header">Notify</f:facet>

                                                                            <h:outputText value="#{dataItem.notify}" />

                                                                  </rich:column>

       

                                                                  <rich:column>

                                                                            <f:facet name="header">Birthdate</f:facet>

                                                                            <h:outputText value="#{dataItem.birthday}" />

                                                                  </rich:column>

       

                                                                  <f:facet name="footer">

                                                                            <h:outputText value="Accounts" />

                                                                  </f:facet>

       

                                                        </rich:dataTable>

       

                                                        <h:commandButton value="BACK" action="GO_FORM" />

       

                          </rich:panel>

                </f:view>

      </body>

      </html>

       

      FormBean.java

       

      package demo;

       

       

      import java.util.ArrayList;

      import java.util.List;

       

       

      import javax.faces.event.ValueChangeEvent;

       

       

      public class FormBean{

                  private String firstName;

                  private String lastName;

                  private String email;

                  private List<String> languages;

                  private List<String> contries;

                  private String country;

                  private String notify;

                  Accounts a = new Accounts();

                  private ArrayList<Accounts> dataList=new ArrayList<Accounts>();

                 

                  public ArrayList<Accounts> getDataList() {

                            loadDatalist();

                            return dataList;

                }

                 

                  public void loadDatalist(){

                   

                            CalendarBean c = new CalendarBean();

                  

                            a.setFirstName(firstName);

                            a.setLastName(lastName);

                            a.setEmail(email);

                            a.setCountry(country);

                            a.setNotify(notify);

                            a.setBirthday(c.getSelectedDate());

                            a.setLanguages(languages);

                           

                            dataList.add(a);

                  }

       

      public FormBean() {

                          this.firstName = firstName;

                          this.lastName = lastName;

                          this.email = email;

                          contries = new ArrayList<String>();

                          contries.add("Turkey");

                          contries.add("Spain");

                          contries.add("United Kingdom");

                          this.notify = notify;

                }

       

      getters and setters....

      -------------------------------------------------------

       

      Accounts.java

       

      package demo;

       

      import java.util.Date;

      import java.util.List;

       

      public class Accounts {

                  private String firstName;

                  private String lastName;

                  private String email;

                  private List<String> languages;

                  private List<String> contries;

                  private String country;

                  private String notify;

                  private Date birthday;

       

      getters and setters....

       

      -----------------------------------------------

       

      my faces-config.xml;

       

      <managed-bean>

                          <managed-bean-name>formBean</managed-bean-name>

                          <managed-bean-class>demo.FormBean</managed-bean-class>

                          <managed-bean-scope>session</managed-bean-scope>

                </managed-bean>

       

      <managed-bean>

                          <managed-bean-name>accounts</managed-bean-name>

                          <managed-bean-class>demo.Accounts</managed-bean-class>

                          <managed-bean-scope>request</managed-bean-scope>

                </managed-bean>