2013-04-06 91 views
0

我的h:form包含p:selectOneMenu組件,其中有兩個值single和multiple。 h:form也包含默認的p:inputText。我的目標是僅當選擇了多個值時才添加多個p:inputText組件。請參閱隨附screenshot- enter image description hereAjax動態添加文本框在PrimeFaces v3.5中不起作用

下面是我的看法,這假設發送ajax要求,每當圖標按鈕是clicked-

<h:form> 
    <p:panel header="Dynamically Add textbox"> 
     <p:selectOneMenu id="runType" value="#{repeateRun.runType}"> 
      <f:selectItems value="#{repeateRun.runList}" var="runType" itemLabel="#{runType}" itemValue="#{runType}" /> 
      <p:ajax update="outPanel" listener="#{repeateRun.renderComponent}" /> 
     </p:selectOneMenu> 
     <h:panelGrid id="runList"> 
      <ui:repeat value="#{repeateRun.runs}" var="run"> 
       <p:inputText value="#{run.runValue}" /> 
      </ui:repeat> 
     </h:panelGrid> 
     <p:outputPanel id="outPanel"> 
      <p:commandButton update="runList" icon="ui-icon-plusthick" title="Add more" rendered="#{repeateRun.showAddButton}"> 
       <f:ajax render="runList" listener="#{repeateRun.addRun}" /> 
      </p:commandButton> 
     </p:outputPanel> 
    </p:panel> 
</h:form> 

@ViewScoped@ManagedBean RepeateRun是以下各項

import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.List; 
import javax.annotation.PostConstruct; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.ViewScoped; 
import javax.faces.event.ActionEvent; 
@ManagedBean 
@ViewScoped 
public class RepeateRun implements Serializable { 
    private static final long serialVersionUID = 1L; 
    private List<String> runList; 
    private List<Run> runs; 
    private int runValue; 
    private String runType; 
    private boolean showAddButton = false; 
    private static final String SINGLE = "Single"; 
    private static final String MULTIPLE = "Multiple"; 
    //Note : Getters Setters are removed while putting here 
    @PostConstruct 
    public void initBean() { 
     this.setRunList(this.populateRunList()); 
     this.setRuns(this.populateRuns()); 
    } 
    public void addRun() { 
     if (this.runs == null) { 
      this.setRuns(this.populateRuns()); 
     } else { 
      this.runs.add(this.defaultRun()); 
     } 
    } 
    public void renderComponent() { 
     if (this.getRunType().equals(SINGLE)) { 
      this.setShowAddButton(false); 
     } else { 
      this.setShowAddButton(true); 
     } 
    } 
    private List<String> populateRunList() { 
     List<String> runList = new ArrayList<String>(); 
     runList.add(SINGLE); 
     runList.add(MULTIPLE); 
     return runList; 
    } 
    private Run defaultRun() { 
     Run defaultRun = new Run(); 
     defaultRun.setRunValue(1); 
     return defaultRun; 
    } 
    private List<Run> populateRuns() { 
     List<Run> runs = new ArrayList<Run>(); 
     runs.add(this.defaultRun()); 
     return runs; 
    } 
} 

因此,在f:selectItems中選擇值Multiple後,會出現加號圖標按鈕,但該按鈕未調用附加方法即。addRun。點擊確認方法addRun後,我在addRun方法中放了一些sysout報表。我看到sysout沒有被刷新。與此同時,我在螢火蟲中看到了一些xml的迴應。
問題在哪裏?

+2

你忘了說不清楚是什麼問題被替換的罪魁禍首。究竟發生了什麼(不)? – BalusC 2013-04-06 19:44:22

+0

單擊圖標按鈕時,addRun方法是否執行? – 2013-04-06 19:45:47

+0

@Laabidi:根據迄今爲止發佈的代碼,只要它被呈現,它不會,但是OP在具體問題上並不完全清楚。 – BalusC 2013-04-06 19:47:02

回答

2

問題出在f:ajax,它不適用於p:commandButton。下面是線 -

<p:commandButton update="runList" icon="ui-icon-plusthick" title="Add more" rendered="#{repeateRun.showAddButton}"> 
    <f:ajax render="runList" listener="#{repeateRun.addRun}" /> 
</p:commandButton> 

以上行應與以下行

<p:commandButton actionListener="#{repeateRun.addRun}" update="runList" icon="ui-icon-plusthick" title="Add more" rendered="#{repeateRun.showAddButton}" />