5 Replies Latest reply on Apr 11, 2011 4:00 AM by ilya_shaikovsky

    updating rich:dataTable contents

    techbrainless

      Hi All,

      I  have created(using jsf1.2 and richfaces) a page which list the students , when the page opens it lists all the students ,

       

      For the purpose of search  I have tried to use filterBy but the problem with it , it allows fitlering based on first character , that is why  I have added to the page inputtext to search for a particular student name (even so if the typed character is at the middle of the name),

       

      the problem is that when i search for a student i got the desired result for search but the datatable contents will not be updated accroding to the search result

       

       

      any way to fix this issue ?

       

       

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

          xmlns:h="http://java.sun.com/jsf/html"

          xmlns:f="http://java.sun.com/jsf/core"

          xmlns:c="http://java.sun.com/jstl/core"

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

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

       

      <f:loadBundle basename="resources" var="msg" />

       

      <ui:composition template="/templates/common.xhtml">

       

          <ui:define name="pageTitle">#{msg.studentsManagement}</ui:define>

       

          <ui:define name="pageHeader">#{msg.studentsManagement}</ui:define>

       

          <ui:define name="body">

       

              <h:form>

                  <rich:panel id="studentsPanel" header="#{msg.studentsList}" rendered="true">

       

                      <table align="center">

                          <tr>

                              <td><h:outputLabel value="#{msg.studentName}"

                                  for="studentName" /></td>

                              <td><h:inputText id="studentName"

                                  value="#{studentInfoListBean.searchedStudentName}" /></td>

                              <td><a4j:commandLink

                                  actionListener="#{studentInfoListBean.search}"

                                  reRender="studentsPanel,studentsList,errMessage,pan,delete,edit" value="#{msg.search}" /></td>

                          </tr>

       

                          <tr>

                              <td colspan="3" align="center"><h:outputLabel

                                  value="#{studentInfoListBean.errorMessage}" id="errMessage"

                                  style="color:red;" /></td>

       

                          </tr>

                      </table>

       

       

                      <rich:dataTable binding="#{studentInfoListBean.table}"

                          align="center" id="studentsList" rows="10"

                          value="#{studentInfoListBean.studentsInfo}"

                          var="student" rendered="true">

       

                          <rich:column sortBy="#{student.studentName}">

                              <f:facet name="header">

                                  <h:outputText value="#{msg.studentName}" />

                              </f:facet>

                              <h:outputText value="#{student.studentName}" />

                          </rich:column>

       

                          <rich:column>

                              <f:facet name="header">

                                  <h:outputText value="#{msg.edit}" />

                              </f:facet>

                              <h:graphicImage value="/images/edit.gif" />

                              <a4j:commandLink immediate="true"

                                  actionListener="#{studentInfoListBean.editStudentInfo}"

                                  value="#{msg.edit}"

                                  rendered="#{userInfo.userAccessRights.editStudentInfo}">

                                  <f:setPropertyActionListener value="#{student}"

                                      target="#{requestScope.selectedStudentInfo}" id="edit"/>

                              </a4j:commandLink>

       

                          </rich:column>

       

       

                          <rich:column>

                              <f:facet name="header">

                                  <h:outputText value="#{msg.delete}" />

                              </f:facet>

       

                              <h:graphicImage value="/images/delete.gif" />

                              <a4j:commandLink reRender="studentsPanel"

                                  actionListener="#{studentInfoListBean.deleteStudentInfo}"

                                  value="#{msg.delete}"

                                  rendered="#{userInfo.userAccessRights.deleteStudentInfo}" id="delete"></a4j:commandLink>

                          </rich:column>

       

       

                      </rich:dataTable>

                      <rich:datascroller align="center" for="studentsList" maxPages="20" />

       

                      <h:graphicImage value="/images/create.gif" />

                      <a4j:commandLink

                          actionListener="#{studentInfoListBean.addStudentInfo}"

                          value="#{msg.addNewStudent}"

                          rendered="#{userInfo.userAccessRights.addStudentInfo}"></a4j:commandLink>

       

                      <span>&nsbp;&nsbp;&nsbp;</span>

                  </rich:panel>

              </h:form>

              <a4j:include id="pan" viewId="#{dialogManager.currentDialogURI}" rendered="true"></a4j:include>

          </ui:define>

      </ui:composition>

      </html>

       

       

      StudentInfoListBean.java

      import com.student.api.StudentInfo;

      import com.student.backoffice.api.exception.StudentInfoException;

      import com.student.backoffice.api.managers.IStudentInfoManager;

      import com.student.backoffice.utilities.Log;

       

      import org.richfaces.component.UIDataTable;

       

      import java.util.List;

       

      import javax.faces.event.ActionEvent;

       

       

       

      public class StudentInfoListBean extends BaseBean {

          private IStudentInfoManager studentInfoManager;

          private UIDataTable table;

          private String errorMessage;

          private String clearErrorMessage;

          private DialogManager dialogManager;

       

          private String searchedStudentName;

          private List<StudentInfo> studentsInfo;

          /**

          * Creates a new UserInfoListBean object.

          */

          public StudentInfoListBean() {

              Log.getLogger().info("#####StudentInfoListBean Constructor");

          }

       

          public UIDataTable getTable() {

              return this.table;

          }

       

          public void setTable(UIDataTable table) {

              this.table = table;

          }

       

          public String getErrorMessage() {

              return this.errorMessage;

          }

       

          public void setErrorMessage(String errorMessage) {

              this.errorMessage = errorMessage;

          }

       

          public String getClearErrorMessage() {

              return this.clearErrorMessage;

          }

       

          public void setClearErrorMessage(String clearErrorMessage) {

              this.clearErrorMessage = clearErrorMessage;

          }

       

          /**

           * DOCUMENT ME!

           *

           * @param students DOCUMENT ME!

           */

       

          /**

           * DOCUMENT ME!

           *

           * @param ae DOCUMENT ME!

           */

       

          /**

           * DOCUMENT ME!

           *

           * @param ae DOCUMENT ME!

           */

          public List<StudentInfo> getStudentsInfo() {

              List<StudentInfo> studentsInfo = null;

       

              try {

                  if ((this.searchedStudentName != null)

                          && (this.searchedStudentName.trim().length() > 0))

                      studentsInfo = this.studentInfoManager

                              .getStudentsInfo(this.searchedStudentName);

                  else

                      studentsInfo = this.studentInfoManager.getStudentsInfo();

              } catch (StudentInfoException e) {

                  e.printStackTrace();

              }

       

              Log.getLogger(this.getClass())

                 .debug("The students size is " + studentsInfo.size());

       

              return studentsInfo;

          }

       

          /**

           * @return the studentInfoManager

           */

          public IStudentInfoManager getStudentInfoManager() {

              return studentInfoManager;

          }

       

          /**

           * @param studentInfoManager the studentInfoManager to set

           */

          public void setStudentInfoManager(IStudentInfoManager studentInfoManager) {

              this.studentInfoManager = studentInfoManager;

          }

       

          /**

           * @return the dialogManager

           */

          public DialogManager getDialogManager() {

              return dialogManager;

          }

       

          /**

           * @param dialogManager the dialogManager to set

           */

          public void setDialogManager(DialogManager dialogManager) {

              this.dialogManager = dialogManager;

          }

       

          public void addStudentInfo(ActionEvent ae) {

              this.dialogManager.openDialog(ae, "addStudentInfo");

          }

       

          /**

           * DOCUMENT ME!

           *

           * @param ae DOCUMENT ME!

           */

          public void deleteStudentInfo(ActionEvent ae) {

              try {

                  StudentInfo studentInfo = (StudentInfo) this.table.getRowData();

                  this.studentInfoManager.deleteStudentInfo(studentInfo);

              } catch (StudentInfoException e) {

                  Log.getLogger(this.getClass())

                     .error("Unable to to delete the student info. Reason [ " +

                      e.getMessage() + " ]");

                  e.printStackTrace();

              }

          }

       

          /**

           * DOCUMENT ME!

           *

           * @param ae DOCUMENT ME!

           */

          public void editStudentInfo(ActionEvent ae) {

              StudentInfo studentInfo = (StudentInfo) this.table.getRowData();

              Log.getLogger().debug("#####selected Student id :"+studentInfo.getStudentId()+" and name :"+studentInfo.getStudentName());

              this.dialogManager.openDialog(ae, "editStudentInfo");

          }

       

          public void search(ActionEvent ae) {

              this.studentsInfo = getStudentsInfo();

          }

       

          public String getSearchedStudentName() {

              return searchedStudentName;

          }

       

          public void setSearchedStudentName(String searchedStudentName) {

              this.searchedStudentName = searchedStudentName;

          }

       

          public void setStudentsInfo(List<StudentInfo> studentsInfo) {

              this.studentsInfo = studentsInfo;

          }

       

      }

        • 1. updating rich:dataTable contents
          ilya_shaikovsky

          check a4j:log information about the responce and performed updates.

           

          B.t.w. not related to the problem but just to mention - it was possible to use something like http://livedemo.exadel.com/richfaces-demo/richfaces/filteringFeature.jsf?tab=ex-usage&cid=660556

          • 2. Re: updating rich:dataTable contents
            techbrainless

            Thanks for your reply ,

             

            I have tried to use the filtering feature , but am still getting the same thing , the rich table data is not updating here is the code :

             

            some part of java code

             

            public boolean filterUsers(Object current) {

                    Log.getLogger().info("#####inside filterUsers ");

                    UserInfo currentUser = (UserInfo)current;

                    Log.getLogger().info("#####inside filterUsers searchedUserName : "+this.searchedUserName+" , userInfo username :"+currentUser.getUsername());

                    if (this.searchedUserName!=null){

                        if (this.searchedUserName.length()==0) {

                            return true;

                        }

                        if (currentUser.getUsername().toLowerCase().contains((this.searchedUserName.toLowerCase()))) {

                            return true;

                        }else {

                            return false;

                        }

                    }else

                        return true;

                }

             

             

             

            some part of xhtml file :

             

                            <rich:panel id="accountsPanel" header="#{msg.usersList}">

             

                                <rich:dataTable binding="#{userInfoListBean.table}" align="center"

                                    id="accountsList" rows="10" value="#{userInfoListBean.usersInfo}"

                                    var="user">

             

                                    <rich:column sortBy="#{user.username}" filterMethod="#{userInfoListBean.filterUsers}">

                                        <f:facet name="header">

                                            <h:outputText value="#{msg.username}" />

                                            <h:inputText value="#{userInfoListBean.searchedUserName}"

                                                id="input">

                                                <a4j:support event="onkeyup"

                                                    reRender="accountsList,accountsPanel,errMessage,ds"

                                                    ignoreDupResponses="true" requestDelay="700" />

                                            </h:inputText>

                                        </f:facet>

                                        <h:outputText value="#{user.username}" />

                                    </rich:column>

                                    <rich:column>

                                        <f:facet name="header">

                                            <h:outputText value="#{msg.delete}" />

                                        </f:facet>

             

                                        <h:graphicImage value="/images/delete.gif" />

                                        <a4j:commandLink reRender="accountsPanel"

                                            actionListener="#{userInfoListBean.deleteUserInfo}"

                                            value="#{msg.delete}"

                                            rendered="#{userInfo.userAccessRights.deleteUserInfo}"></a4j:commandLink>

                                    </rich:column>

             

             

                                </rich:dataTable>

                                <rich:datascroller align="center" for="accountsList" maxPages="20" id="ds"/>

             

             

            actually it will the filteration will be correct but when i am trying to delete the one of the filtered records , it will delete another record not same as i have selected (it seems that datatable contains the previous non filtered data)

            • 3. updating rich:dataTable contents
              ilya_shaikovsky

              at first

              <ui:composition xmlns="http://www.w3.org/1999/xhtml"

                    xmlns:ui="http://java.sun.com/jsf/facelets"

                    xmlns:h="http://java.sun.com/jsf/html"

                    xmlns:f="http://java.sun.com/jsf/core"

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

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

                        <h:form>

                                  <rich:dataTable value="#{capitalsBean.capitals}" var="cap" rows="20" reRender="ds" id="simpletable" binding="#{capitalsBean.table}">

                                            <f:facet name="header">

                                                      <rich:columnGroup>

                                                                <rich:column colspan="2" >

                                                                          <h:outputText value="Filtering Example"/>

                                                                </rich:column>

                                                                <rich:column breakBefore="true">

                                                                          <h:outputText value="State Name"/>

                                                                </rich:column>

                                                                <rich:column>

                                                                          <h:outputText value="State Capital"/>

                                                                </rich:column>

                                                      </rich:columnGroup>

                                            </f:facet>

                                            <rich:column filterBy="#{cap.state}" filterEvent="onkeyup" filterValue="#{capitalsBean.currentStateFilterValue}">

                                                      <h:outputText value="#{cap.state}"/>

                                            </rich:column>

                                            <rich:column filterBy="#{cap.name}" filterEvent="onkeyup" filterValue="#{capitalsBean.currentNameFilterValue}">

                                                      <h:outputText value="#{cap.name}"/>

                                            </rich:column>

                                            <rich:column>

                                                      <a4j:commandLink value="delete" action="#{capitalsBean.delete}" reRender="simpletable"/>

                                            </rich:column>

                                            <f:facet name="footer">

                                                      <rich:datascroller id="ds" renderIfSinglePage="false"></rich:datascroller>

                                            </f:facet>

                                  </rich:dataTable>

                                  <a4j:commandButton action="#{capitalsBean.resetFilter}" value="Reset Current Filtering" reRender="simpletable" ajaxSingle="true" limitToList="true"/>

                        </h:form>

                                  <a4j:log></a4j:log>

              </ui:composition>

               

              and

               

                  private UIDataTable table;

                 

                  public void delete(){

                      capitals.remove(this.table.getRowData());

                  }

               

               

                  public UIDataTable getTable() {

                      return table;

                  }

                 

                  public void setTable(UIDataTable table) {

                      this.table = table;

                  }

               

              works just fine for me.

               

              But b.t.w. using binding is not recommended for that case especially if the bean are session scoped. that sample [java code]uses much better way with wrapping to simple model which allow to get the items by just rowKeys without storing the UI component instance.

              • 4. updating rich:dataTable contents
                techbrainless

                Thanks  Ilya Shaikovsky for your reply  ,

                 

                but your search is for the first characters but my search i am searching for any character even so if it is at the middle of the word that is why i have used filterMethod

                • 5. updating rich:dataTable contents
                  ilya_shaikovsky

                  it doesn't matter filterBy or filterExpression used.