2013-04-01 44 views
2

我有兩個選項卡。 使用嚮導組件,當我按下'Next'按鈕(從第一個選項卡)時,我需要填充一個列表(使用SelectItem對象),然後在第二個選項卡中的SelectOneMenu標籤中顯示這些SelectItems。由於我很快就完成了在第一個標籤上輸入數據,然後按下「下一步」按鈕,此SelectOneMenu標籤(在第二個標籤上)中沒有任何內容。嚮導組件PrimeFaces更新其他變量

基本上,我需要更新下一個選項卡以及當前選項卡的處理。無論如何要做到這一點?

在此先感謝。

enter image description hereenter image description here

這裏是我的代碼:

<p:wizard widgetVar="wiz" showNavBar="true" flowListener="#{detentionForm.onFlowProcess}"> 
       <p:tab id="typeOfLeader" title="Leader Selection"> 
        <p:panel header="Leader Selection"> 
         <p:messages showSummary="true" showDetail="false"/> 
         <p:panelGrid columns="2"> 
          #{msgs.typeOfLeaderPunishment} 
          <p:selectOneMenu value="#{detentionForm.typeOfLeaderSelectedID}" style="width:400px" panelStyle="width:150px" effect="fade"> 
           <f:selectItems value="#{detentionForm.teacherTypes}" var="type" itemLabel="#{type.label}" itemValue="#{type.value}" /> 
          </p:selectOneMenu> 
         </p:panelGrid> 
        </p:panel> 
       </p:tab> 
       <p:tab id="typeOfPunishment" title="Punishment Type"> 
        <p:panel header="Type of Punishment"> 
         <p:panelGrid columns="2"> 
          #{msgs.typeOfDetention} 
          <p:selectOneMenu value="#{detentionForm.typeOfPunishment}" style="width:400px" panelStyle="width:150px" effect="fade" > 
           <f:selectItems value="#{detentionForm.detentionTypes}" var="type" itemLabel="#{type.label}" itemValue="#{type.value}" /> 
          </p:selectOneMenu> 
         </p:panelGrid> 

        </p:panel> 
       </p:tab> 

      </p:wizard> 

#{detentionForm.detentionTypes}是需要填充一次我按下「下一步」按鈕,第二個選項卡的ArrayList。


這是我支持bean:

@ViewScoped 
@Named("detentionForm") 
public class DetentionFormBean implements Serializable{ 
    @Resource(name="jdbc/DetentionCentre") 
    private DataSource ds; 

    private Details details = (Details) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("userDetails"); 



    private String typeOfPunishment; 
    private String typeOfLeader = details.getTypeOfLeader(); //Can change if user is House Leader. Eg. They can become a teacher when making a punishment 
    private int typeOfLeaderID; //ID in the database of the teacher's type of leadership 
    private int typeOfLeaderSelectedID = 1; //Set default to teacher as House leader can be teacher and normal teacher will be a teacher! 
    private ArrayList<SelectItem> detentionTypes = new ArrayList<SelectItem>(); //SelectItem objects that shows what punishments each teacher can do 
    private ArrayList<SelectItem> teacherTypes = new ArrayList<SelectItem>(); //SelectItem objects that shows what type of leadership a teacher can be (Teacher or House Leader) 


    //gets all the data necessary from the database to show on the page 
    @PostConstruct 
    public void initialize(){ 
     this.setTypeOfLeaderID(details.getUserName()); //gets the ID from the database to find out the default type of leadership 
     this.findTeacherTypes(this.typeOfLeader); 
    } 

    public String onFlowProcess(FlowEvent event) { //change later for backing as well >>>>>>>>>>>>>> 
      String stepToGo = event.getNewStep(); 
      if(stepToGo.equals("typeOfPunishment")){ 
       this.findDetentionTypes(); 

      } 
     return stepToGo; 

    } 

    //populates teacherType Arraylist depending the user's teacher type. House Leader can be a Teacher or a House Leader. 
    private void findTeacherTypes(String type) { 
     if(type.equals("House Leadership Team")){ 
      this.teacherTypes.add(new SelectItem(Integer.toString(this.typeOfLeaderID), type)); //House leader is 2 
      this.teacherTypes.add(new SelectItem(Integer.toString(this.typeOfLeaderID - 1), "Teacher")); //Teacher is 1 
     }else{ //type is Teacher 
      this.teacherTypes.add(new SelectItem(Integer.toString(this.typeOfLeaderID), type)); //Teacher is 1 
     } 
    } 


    //populates detentiontypes depending on type of leader 
    private void findDetentionTypes() { 
     System.out.println(">>>>Inside findDetentionTypes()!!!"); 
     PreparedStatement ps; 
     Connection con; 
     String sqlInitialData = "SELECT r.punishment_type, p.type FROM detentioncentredb.tbl_teacher_roles_allowed_punishments r, detentioncentredb.tbl_punishment_types p WHERE r.teacher_roles = ? AND p.ID = r.punishment_type"; 
     ResultSet rs; 

     try { 
      con = ds.getConnection(); 
      ps = con.prepareStatement(sqlInitialData); 
      ps.setString(1, Integer.toString(this.typeOfLeaderSelectedID)); 
      rs = ps.executeQuery(); 
      while(rs.next()){ 
       SelectItem s = new SelectItem(rs.getString("punishment_type"), rs.getString("type")); 
       this.detentionTypes.add(s); 
      } 
      rs.close(); 
      ps.close(); 
      con.close(); 

     } catch (SQLException ex) { 
      Logger.getLogger(DetentionFormBean.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

    //getter and setter methods 

    public String getTypeOfPunishment() { 
     return typeOfPunishment; 
    } 

    public void setTypeOfPunishment(String typeOfPunishment) { 
     this.typeOfPunishment = typeOfPunishment; 
    } 

    public String getTypeOfLeader() { 
     return typeOfLeader; 
    } 

    public ArrayList<SelectItem> getDetentionTypes() { 
     return detentionTypes; 
    } 

    public ArrayList<SelectItem> getTeacherTypes() { 
     return teacherTypes; 
    } 

    public int getTypeOfLeaderID() { 
     return typeOfLeaderID; 
    } 

    //gets the typeOfLeader the current user is from the database. This retrieves the ID of the teacher's type of leadership. NOT WHAT THE TEACHER SELECTED. 
    private void setTypeOfLeaderID(int userName){ 
     PreparedStatement ps; 
     Connection con; 
     String sqlTypeOfLeaderID = "SELECT TypeOfLeader FROM detentioncentredb.tbl_teachers WHERE RegNumber = ?"; 
     ResultSet rs; 
     try { 
      con = ds.getConnection(); 
      ps = con.prepareStatement(sqlTypeOfLeaderID); 
      ps.setInt(1, userName); 
      rs = ps.executeQuery(); 
      while(rs.next()){ 
       this.typeOfLeaderID = rs.getInt("TypeOfLeader"); 
      } 
      rs.close(); 
      ps.close(); 
      con.close(); 
     } catch (SQLException ex) { 
      Logger.getLogger(DetentionFormBean.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

    public int getTypeOfLeaderSelectedID() { 
     return typeOfLeaderSelectedID; 
    } 

    public void setTypeOfLeaderSelectedID(int typeOfLeaderSelectedID) { 
     this.typeOfLeaderSelectedID = typeOfLeaderSelectedID; 
    } 


} 
+0

什麼是primeface的版本?我正在使用v4.0RC1,並且更新了onFlowProcess()中所做的每個更改。 – Ghetolay

回答

2

您可以使用AJAX 通知你走進你的嚮導在服務器端。從PF documentation

如果您希望得到在服務器端,當嚮導試圖回去或轉發通知,定義flowListener

<p:wizard flowListener="#{userWizard.handleFlow}"> 
... 
</p:wizard> 

public String handleFlow(FlowEvent event) { 
    String currentStepId = event.getCurrentStep(); 
    String stepToGo = event.getNextStep(); 
    if(skip) 
     return "confirm"; 
    else 
     return event.getNextStep(); 
    } 

在你的情況,你需要的時候stepToGo達到typeOfPunishment,基於嚮導的第一步已經選擇哪些用戶加載detentionTypes陣列:

public String handleFlow(FlowEvent event) { 
    String stepToGo = event.getNextStep(); 
    if(stepToGo.equals("typeOfPunishment")){ 
     detentionTypes = service.loadDetentionTypes(this.TypeOfLeaderSelectedID); 
    } 
} 
+0

是的,我試過了。它填充列表,但不會更新頁面,因此將SelectOnempMenu留在其中。這是我遇到的問題。 –

+0

你可以發佈你的支持bean代碼嗎?特別是你正在做流監聽器方法。您還應該調試該方法並查看數組中的值。 –

+0

在問題中添加了後臺bean代碼。我已經完成了你對流量監聽者所說的話。我調試了應用程序,發現我的'detentionTypes' arraylist正在被填充。但是,下一個選項卡不會再次呈現以使用該填充的數組列表。 –