2014-05-18 28 views
0

當我點擊編輯鏈接時,我無法在豐富的彈出式面板中獲取所需的數據。任何人都可以幫助我解決它。無法在richfaces中的彈出式面板中獲取數據

當我嘗試編輯行詳細信息時,我無法第一次在各自的字段中獲取所需的數據,但出人意料地,當我刷新頁面並執行相同的過程時,我獲得了其中的數據各自的領域。

我想第一次獲得點擊編輯鏈接所需的數據。

那麼,有人可以幫我解決它。

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: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:t="http://myfaces.apache.org/tomahawk"> 
<h:head> 

</h:head> 
<h:body> 
    <h:form id="dboperatorform"> 
     <t:saveState value="#{dBOperator.empList}"></t:saveState> 
     <t:saveState value="#{dBOperator.edit}"></t:saveState> 
     <t:saveState value="#{dBOperator.rowNum}"></t:saveState> 
     <t:saveState value="#{dBOperator.employee}"></t:saveState> 
     <rich:panel style="width:1345px"> 
      <f:facet name="header"> 
       <h:outputText value="Search Panel" /> 
      </f:facet> 
      <h:panelGrid id="searchPanel" columns="6"> 
       <h:column> 
        <h:outputLabel value="First Name:" /> 
       </h:column> 
       <h:column> 
        <h:inputText value="#{dBOperator.first_Name}" /> 
       </h:column> 
       <h:column> 
        <h:outputLabel value="Last Name:" /> 
       </h:column> 
       <h:column> 
        <h:inputText value="#{dBOperator.last_name}" /> 
       </h:column> 
       <h:column> 
        <a4j:commandButton value="Search" action="#{dBOperator.search}" 
         render="richtable"> 

        </a4j:commandButton> 
       </h:column> 
       <h:column> 

       </h:column> 
      </h:panelGrid> 
     </rich:panel> 
     <br /> 
     <a4j:commandButton value="Add" action="#{dBOperator.editdetails}" 
      execute="@this" style="width:30px;height:30px;" 
      oncomplete="#{rich:component('popup1')}.show();" 
      render="richtable popup1"> 
      <f:setPropertyActionListener target="#{dBOperator.edit}" 
       value="false"></f:setPropertyActionListener> 
     </a4j:commandButton> 
     <p></p> 
     <rich:dataTable value="#{dBOperator.employees}" var="emp" 
      id="richtable" cellpadding="0" cellspacing="0" style="width:1349px" 
      reRender="richtable" iterationStatusVar="it" 
      noDataLabel="No Records Found!" rows="5"> 
      <rich:column> 
       <f:facet name="header">#</f:facet> 
       <h:outputText value="#{it.index}" /> 
      </rich:column> 
      <rich:column> 
       <f:facet name="header"> 
        <a4j:commandLink value="First Name" 
         action="#{dBOperator.sortByFirstName}" render="richtable"></a4j:commandLink> 
       </f:facet> 
       <h:outputText value="#{emp.first_Name}" /> 
      </rich:column> 
      <rich:column> 
       <f:facet name="header">Last Name</f:facet> 
       <h:outputText value="#{emp.last_name}" /> 
      </rich:column> 
      <rich:column> 
       <f:facet name="header">Edit</f:facet> 
       <a4j:commandButton image="images/edit.png" 
        action="#{dBOperator.editdetails}" execute="@this" 
        style="width:30px;height:30px;" 
        oncomplete="#{rich:component('popup1')}.show();" 
        render="richtable popup1"> 
        <f:setPropertyActionListener target="#{dBOperator.rowNum}" 
         value="#{it.index}"></f:setPropertyActionListener> 
        <f:setPropertyActionListener target="#{dBOperator.edit}" 
         value="true"></f:setPropertyActionListener> 
       </a4j:commandButton> 
      </rich:column> 
      <f:facet name="footer"> 
       <rich:dataScroller for="richtable" align="right" 
        status="ajaxProcessIcon" renderIfSinglePage="false" fastStep="5" 
        fastControls="auto" /> 
      </f:facet> 
     </rich:dataTable> 

     <rich:popupPanel id="popup1" minHeight="300" 
      minWidth="300"> 
      <t:saveState value="#{dBOperator.edit}"></t:saveState> 
      <t:saveState value="#{dBOperator.rowNum}"></t:saveState> 
      <f:facet name="header"> 
       <h:outputText value="Simple popup panel" /> 
      </f:facet> 
      <f:facet name="controls"> 
       <h:outputLink 
        onclick="#{rich:component('popup1')}.hide(); return false;" 
        reRender="richtable">X</h:outputLink> 
      </f:facet> 
      <rich:toolbar itemSeparator="line" width="100%" id="qawq"> 
       <rich:toolbarGroup location="left"> 
        <h:outputText 
         value="#{dBOperator.edit?'Employee Edit':'Employee Add'}" /> 
       </rich:toolbarGroup> 
      </rich:toolbar> 
      <h:panelGrid id="CancelGrid" columns="2" width="100%"> 
       <rich:column width="30%" style="border:none;"> 
        <h:outputText value="First Name" style="font-weight: bold;" /> 
        <font class="star" color="red">*</font> 
       </rich:column> 
       <rich:column width="70%" style="border:none;"> 
        <h:inputText value="#{dBOperator.employee.first_Name}" 
         style="width:100%" id="abbr" /> 
       </rich:column> 
       <rich:column width="30%" style="border:none;"> 
        <h:outputText value="Last Name" style="font-weight: bold;" /> 
        <font class="star" color="red">*</font> 
       </rich:column> 
       <rich:column width="70%" style="border:none;"> 
        <h:inputText value="#{dBOperator.employee.last_name}" 
         style="width:100%" id="abbr1" /> 
       </rich:column> 
      </h:panelGrid> 
      <a4j:commandButton value="#{dBOperator.edit?'Edit':'Save'}" 
       action="#{dBOperator.savedetails}" 
       oncomplete="#{rich:component('popup1')}.hide();" 
       onclick="validateFields();"></a4j:commandButton> 
     </rich:popupPanel> 
     <script type="text/javascript"> 
        function validateFields() { 
         alert('came inside validateFields'); 
         var fn=document.getElementById('dboperatorform:abbr'); 
         alert('fn '+fn.value); 
         var ln=document.getElementById('dboperatorform:abbr1'); 
         alert('ln '+ln.value); 
        } 

       </script> 
    </h:form> 
</h:body> 
</html> 

Bean類代碼

package com.richfaces.db; 

import java.lang.management.ManagementFactory; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 

import javax.faces.context.FacesContext; 
import javax.faces.event.ActionEvent; 
import javax.management.MBeanServer; 
import javax.swing.SortOrder; 

import org.hibernate.Query; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; 

import com.comparator.NameComparator; 
import com.comparator.NameCompartor1; 
import com.richfaces.Employee; 

public class DBOperator { 
    String id; 
    String first_Name; 
    String last_name; 
    String fName; 
    String lName; 
    Employee employee; 
    int rowNum; 
    boolean edit; 
    Boolean search=Boolean.FALSE; 
    Boolean status = Boolean.FALSE; 
    List<Employee> list = new ArrayList<Employee>(); 
    List<Employee> empList = new ArrayList<Employee>(); 

    public boolean isEdit() { 
     return edit; 
    } 

    public void setEdit(boolean edit) { 
     this.edit = edit; 
    } 

    public Employee getEmployee() { 
     return employee; 
    } 

    public void setEmployee(Employee employee) { 
     this.employee = employee; 
    } 

    public int getRowNum() { 
     return rowNum; 
    } 

    public void setRowNum(int rowNum) { 
     this.rowNum = rowNum; 
    } 

    public String getFirst_Name() { 
     return first_Name; 
    } 

    public String getId() { 
     return id; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

    public void setFirst_Name(String first_Name) { 
     this.first_Name = first_Name; 
    } 

    public String getLast_name() { 
     return last_name; 
    } 

    public void setLast_name(String last_name) { 
     this.last_name = last_name; 
    } 

    public List<Employee> getEmployees() { 
     System.out.println("hghhghhhhh"); 
     String sql2=""; 
     Query query = null; 
     String sql1 = "from Employee as emp where id is not null "; 
     Configuration cfg = new Configuration(); 
     cfg.configure(); 
     SessionFactory sf = cfg.buildSessionFactory(); 
     Session sess = sf.openSession(); 
     sess.beginTransaction(); 
     if(status==false && search==false) { 
      System.out.println("Normal Query +++++++++++++++++++++++ " + sql1); 
      String appendedQuery = createCriteriaQuery(sql1); 
      System.out.println("Appended Query ++++++++++++++++++++++++ " 
        + appendedQuery); 
      query = sess.createQuery(appendedQuery); 
     }else if(status==true && search==true) { 
      System.out.println("came inside when search and status is true"); 
      String appendedQuery = createCriteriaQuery(sql1); 
      System.out.println("Appended Query ++++++++++++++++++++++++ " 
        + appendedQuery); 
      query = sess.createQuery(appendedQuery); 
     }else if(status==false && search==true) { 
      System.out.println("came inside when search is true and status is false"); 
      String appendedQuery = createCriteriaQuery(sql1); 
      System.out.println("Appended Query ++++++++++++++++++++++++ " 
        + appendedQuery); 
      query = sess.createQuery(appendedQuery); 
     }else if(status==true && search==false) { 
      System.out.println("came inside when search is false and status is true"); 
      String appendedQuery = createCriteriaQuery(sql1); 
      System.out.println("Appended Query ++++++++++++++++++++++++ " 
        + appendedQuery); 
      query = sess.createQuery(appendedQuery); 
     } 
     list = query.list(); 
     /*if (status) { 
      Collections.sort(list, new NameComparator()); 
     } else { 
      Collections.sort(list, new NameCompartor1()); 
     }*/ 
     System.out.println(list.size()); 
     this.empList = list; 
     return list; 
    } 

    public String createCriteriaQuery(String query) { 
     StringBuffer sb = new StringBuffer(query); 
     if (getFirst_Name() != null && !(getFirst_Name().equals(""))) { 
      sb.append(" and emp.first_Name='" + first_Name + "'"); 
     } 
     if (getLast_name() != null && !(getLast_name().equals(""))) { 
      sb.append(" and emp.last_name='" + last_name + "'"); 
     } 
     if(this.status) { 
      sb.append(" order by emp.first_Name desc"); 
     }else { 
      sb.append(" order by emp.first_Name"); 
     } 
     System.out.println(sb.toString()); 
     return sb.toString(); 
    } 

    public void editdetails() { 
     if (edit) { 
      System.out.println("rownum is " + rowNum); 
      System.out.println(empList.size()); 
      employee = (Employee) empList.get((rowNum)); 
      System.out.println(employee.getFirst_Name() + "\t" 
        + employee.getLast_name() + "\t" + employee.getId()); 
      System.out.println("end of edit details method"); 
     } else { 
      employee = new Employee(); 
     } 

    } 

    public List<Employee> getEmpList() { 
     return empList; 
    } 

    public void setEmpList(List<Employee> empList) { 
     this.empList = empList; 
    } 

    public void savedetails() { 
     System.out.println("came into savedetails"); 
     Configuration cfg = new Configuration(); 
     cfg.configure(); 
     SessionFactory sf = cfg.buildSessionFactory(); 
     Session sess = sf.openSession(); 
     if(this.edit) { 
      System.out.println("EMPLOYEE DETAILS in IF "+getEmployee().getFirst_Name()+"\t"+getEmployee().getLast_name()); 
      System.out.println("came inside if when edit is true"); 
      sess.update(employee); 
     }else { 
      System.out.println("came inside else when edit is false"); 
      System.out.println("EMPLOYEE DETAILS in ELSE "+getEmployee().getFirst_Name()+"\t"+getEmployee().getLast_name()); 
      sess.save(employee); 
     } 
    } 

    public void search() { 
     System.out.println("came into search method"); 
     search = true; 
    } 

    public void refreshData(ActionEvent ae) { 
     System.out.println("came into refreshdata"); 
     getEmployees(); 
    } 

    public void sortByFirstName() { 
     System.out.println("came inside sortByFirstName"); 
     if (!status) { 
      status = Boolean.TRUE; 
     } else { 
      status = Boolean.FALSE; 
     } 
     //status=Boolean.TRUE; 
     getEmployees(); 

    } 


    public String getfName() { 
     return fName; 
    } 

    public void setfName(String fName) { 
     this.fName = fName; 
    } 

    public String getlName() { 
     return lName; 
    } 

    public void setlName(String lName) { 
     this.lName = lName; 
    } 
} 

My Entity class code as follows: 



    package com.richfaces; 

import java.io.Serializable; 

public class Employee implements Serializable { 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    String id; 
    String first_Name; 
    String last_name; 


    public String getFirst_Name() { 
     return first_Name; 
    } 
    public String getId() { 
     return id; 
    } 
    public void setId(String id) { 
     this.id = id; 
    } 
    public void setFirst_Name(String first_Name) { 
     this.first_Name = first_Name; 
    } 
    public String getLast_name() { 
     return last_name; 
    } 
    public void setLast_name(String last_name) { 
     this.last_name = last_name; 
    } 

} 
+0

刪除'返回false;''從oncomplete' attribut編輯'commandButton'。它應該是'oncomplete =「#{rich:component('popup1')}。show();」' –

+0

@ Vasil,沒有運氣。即使我沒有獲取數據,我的第一次點擊編輯button.its顯示空白文本框爲每個領域。 – VSK

+0

你在'a4j:commandButton'中有錯誤的屬性'reRender'。它在RichFaces 4.x中不受支持,應該是'render'。參見[doc](http://docs.jboss.org/richfaces/latest_4_X/vdldoc/)。將其更改爲'render =「richtable,popup1」' –

回答

0

變化a4j:commandButton屬性

<a4j:commandButton value="Edit" action="#{dBOperator.editdetails}" execute="@this" 
    oncomplete="#{rich:component('popup1')}.show();" 
    render="richtable, popup1"> 
     <f:setPropertyActionListener target="#{dBOperator.rowNum}" value="#{it.index}" /> 
</a4j:commandButton> 
+0

當我試圖保存在彈出式面板中輸入的詳細信息時,我無法插入/更新任何details.can你請幫助我。我已經更新我的代碼上面。 – VSK

+0

創建新問題比已經回答的更改要好。在'h:panelGrid'的彈出窗口中,移除'rich:column'並使用'h:inputText'字段(使用'h:outputText'或'h:outputLabel')並將相關數據放在inputText的'value'屬性中。看[示例](http://stackoverflow.com/questions/21149141/richfaces-show-details-in-popup-commandbutton-action-and-popuppanel/21149308#21149308)。 –

+0

,我創建了新的question.so,請看看這篇文章。[鏈接] [http://stackoverflow.com/questions/23960227/not-able-to-save-update-details-from-richfaces-popup -面板] – VSK