1 Reply Latest reply on Dec 29, 2010 2:43 AM by Ilya Sorokoumov

    a4j:command button: backing bean property not updated in modalpanel

    Wilhelm FY Newbie

      Hi,

       

      I have a page with rich:datatable, with on each row a button to edit current row. The idea is that when user click, a modalpopup appears to let user change values and ercord them in backing bean. Everything works fine when I post from a simple panel, but when i put the code in a modal panel, the backing bean properties are not updated.

      Can someone help ?

       

      Here's the page

       

      <?xml version='1.0' encoding='UTF-8' ?>
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <ui:composition
          xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:a4j="http://richfaces.org/a4j"
          xmlns:rich="http://richfaces.org/rich"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:ui="http://java.sun.com/jsf/facelets"
          xmlns:c="http://java.sun.com/jsp/jstl/core">
          <style>
              .active-row {
                  background-color: #FFEBDA;
              }
          </style>
          <script type="text/javascript">
              var row;
          </script>
          <rich:message/>
          <rich:panel id="tablePanel">
              <h:form>
                  <rich:dataTable
                      value="#{MainBean.users}"
                      onRowMouseOver="this.style.backgroundColor='#B5CEFD'"
                      onRowMouseOut="this.style.backgroundColor='#{org.richfaces.SKIN.tableBackgroundColor}'"
                      onRowClick="this.style.backgroundColor='#F1F1F1'"
                      var="user" rows="20" rowKeyVar="row"
                      id="table"
                      >
                      <f:facet name="header">
                          <h:outputText value="Utilisateurs" />
                      </f:facet>
                      <rich:column>
                          <f:facet name="header">
                              <h:outputText value="login" />
                          </f:facet>
                          <h:outputText value="#{user.email}" id="login" />
                      </rich:column>
                      <rich:column>
                          <f:facet name="header">
                              <h:outputText value="Prénom" />
                          </f:facet>
                          <h:outputText value="#{user.firstname}" id="firstname" />
                      </rich:column>
                      <rich:column>
                          <f:facet name="header">
                              <h:outputText value="Nom" />
                          </f:facet>
                          <h:outputText value="#{user.lastname}" id="lastname" />
                      </rich:column>
                      <rich:column>
                          <f:facet name="header">
                              <h:outputText value="Admin." />
                          </f:facet>
                          <h:outputText value="#{user.admin}" id="admin"/>
                      </rich:column>
                      <rich:column>
                          <f:facet name="header">
                              Actions
                          </f:facet>
                          <a4j:commandLink
                              id="editlink"
                              ajaxSingle="true"
                              action="#{MainBean.setEditMode}"
                              oncomplete="#{rich:component('editCreateUser')}.show()"
                              reRender="editCreateUserForm" >
                              <h:graphicImage  style="border:0;" url="/images/icons/edit.gif" />
                          </a4j:commandLink>
                          <rich:toolTip for="editlink" value="Edit" />
      
                      </rich:column>
                      <f:facet name="footer">
                          <rich:datascroller renderIfSinglePage="false" maxPages="5" />
                      </f:facet>
                  </rich:dataTable>
              </h:form>
              <a4j:commandButton id="newbtn"
                                 reRender="editCreateUserForm"
                                 value="Nouveau"
                                 ajaxSingle="true"
                                 oncomplete="#{rich:component('editCreateUser')}.show()"
                                 action="#{MainBean.setInsertMode}" style="width:85px" />
      
          </rich:panel>
          <rich:modalPanel id="editCreateUser">
              <f:facet name="header">Informations générales</f:facet>
              <a4j:form id="editCreateUserForm" >
                  <a4j:region>
                      <h:panelGrid columns="2"  width="350" >
                          <h:outputText value="Login" />
                          <h:inputText value="#{MainBean.selectedUser.email}" disabled="#{MainBean.editMode}"/>
                          <h:outputText value="Prénom" />
                          <h:inputText value="#{MainBean.selectedUser.firstname}" />
                          <h:outputText value="Nom" />
                          <h:inputText value="#{MainBean.selectedUser.lastname}" />
                          <h:outputText value="Admin" />
                          <h:selectBooleanCheckbox value="#{MainBean.selectedUser.admin}" />
                      </h:panelGrid>
                      <h:panelGrid id="crud_panel" columns="3">
                          <a4j:commandButton id="validbtn"
                                             disabled="#{MainBean.editMode}" ajaxSingle="true" reRender="tablePanel,editCreateUserForm" value="Créer"
                                             actionListener="#{MainBean.storeUser}" style="width:85px"/>
                          <a4j:commandButton id="editbtn"
                                             disabled="#{!MainBean.editMode}" ajaxSingle="true" reRender="tablePanel,editCreateUserForm,editbtn" value="Modifier"
                                             actionListener="#{MainBean.editUser}" style="width:85px"/>
                          <rich:componentControl for="editCreateUser" attachTo="validbtn,editbtn" operation="hide" event="onclick"/>
                      </h:panelGrid>
                  </a4j:region>
              </a4j:form>
          </rich:modalPanel>
      </ui:composition>
      

       

      And the backing bean:

       

      @ManagedBean(name="MainBean")
      @SessionScoped
      public class MainBean {
              private User currentUser;
          private boolean init;
          //private List<User> users;
      
      
          @ManagedProperty(value="#{userService}")
          private UserService service;
      
          private User selectedUser;
      
          private DataModel<User> userDataModel;
      
          private boolean editMode;
      
          public User getCurrentUser() {
              return currentUser;
          }
      
          public void setCurrentUser(User currentUser) {
              this.currentUser = currentUser;
          }
      
          public UserService getService() {
              return service;
          }
      
          public void setService(UserService service) {
              this.service = service;
          }
      
          public User getSelectedUser() {
              return selectedUser;
          }
      
          public void setSelectedUser(User selectedUser) {
              this.selectedUser = selectedUser;
          }
      
          public DataModel<User> getDataModel() {
              return userDataModel;
          }
      
          public void setDataModel(DataModel<User> dataModel) {
              this.userDataModel = dataModel;
          }
      
          public boolean getEditMode() {
              return editMode;
          }
      
          public void setEditMode() {
              this.selectedUser = (User) userDataModel.getRowData();
              editMode = true;
          }
      
      
          public MainBean() {
              HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
              currentUser = (User) session.getAttribute("currentUser");
              setInsertMode();
              init = false;
          }
      
          private void updateDataModel() {
              userDataModel = new ListDataModel<User>();
              List<User> users = service.getUserDao().getAll();
              userDataModel.setWrappedData(users);
          }
      
          public DataModel<User>  getUsers() {
              if (!init) {
                  updateDataModel();
                  init = true;
              }
              return userDataModel;
          }
      
          public void editUser() {
              service.updateUser(selectedUser);
          }
          public void storeUser() {
              service.createUser(
                      selectedUser.getEmail(),
                      selectedUser.getPassword(),
                      selectedUser.getFirstname(),
                      selectedUser.getLastname(),
                      selectedUser.isAdmin()
                      );
              updateDataModel();
          }
      
         
          public void setInsertMode(){
              this.selectedUser = new User();
              editMode = false;
          }
      
      }
      
      
      

       

      Thanks !