2016-04-03 89 views
0

下面的代碼實現佈局內的數據表,在數據表我的每一行傳遞參數:對話框

<p:dataTable id="tbl" var="person" value="#{mybean.listPersons}" > 
      <p:column> 
       <f:facet name="header"> 
        <h:outputText value="Name " /> 
       </f:facet> 
       <h:outputText value="#{person.name}" /> 
      </p:column> 
      <p:column> 
       <f:facet name="header"> 
        <h:outputText value="Age :" /> 
       </f:facet> 
       <h:outputText value="#{person.age}" /> 
      </p:column> 
      <p:column> 
       <p:commandButton icon="ui-icon-pencil" 
       oncomplete="PF('dlg1').show();" action="mybean.setSelectedPerson(person)" /> 
      </p:column> 
</p:dataTable> 

當我點擊編輯按鈕,在對話框中添加編輯按鈕框(代碼如下)顯示,但輸入是空的,我想要的是顯示在對話框行的信息,我仍然是一個初學者,我到處搜索......但沒有結果

<p:dialog header="Modify" widgetVar="dlg1" > 
     <h:form > 
       <p:growl id="msgs" showDetail="true" /> 
       <h:panelGrid id="form2" value="#{myBean.person}" var="person"> 
         <p:outputLabel value="Name :" /> 
         <p:inputText value="#{person.name}" /> 
         <p:outputLabel value="Age :" /> 
         <p:inputText value="#{person.age}" /> 
         <p:commandButton value="Submit" action="#{myBean.modifyPerson(person)}" /> 
       </h:panelGrid> 
    </h:form> 
</p:dialog> 


@ManagedBean 
@RequestScoped 
public class muBean implements Serializable{ 
    private Person selectedPerson; 
    //getter and setter 

    public void modifyPerson(Person p) { 
     this.selectedPerson = p; 
    } 
} 

我會很感激,如果有人可以幫助,我真的需要這個

+0

儘量給對話框中的表單一個id並在按鈕上放置update =「:idOfForm」。並且將bean更改爲ViewScoped,RequestScoped很少見 –

回答

-1

更改命令按鈕下面,使用一個ActionListener:

<p:commandButton icon="ui-icon-pencil" update=":persondlgid" oncomplete="dlg1.show();" actionListener ="mybean.findSelectedPerson"> 
    <f:param name="personalid" value="#{person.id}" /> 
<p:commandButton/> 

這是該對話框中,id屬性添加到它。然後在面板網格的值更改爲selectedPerson因爲這相當於managedbean正確的對象:

<p:dialog header="Modify" widgetVar="dlg1" id="persondlgid" > 
    <h:form> 
      <p:growl id="msgs" showDetail="true" /> 
      <h:panelGrid id="form2" value="#{myBean.selectedPerson}" var="person"> 
      <p:outputLabel value="Name :" /> 
      <p:inputText value="#{person.name}" /> 
      <p:outputLabel value="Age :" /> 
      <p:inputText value="#{person.age}" /> 
      <p:commandButton value="Submit" action="#{myBean.modifyPerson(person)}" /> 
      </h:panelGrid> 
    </h:form> 
</p:dialog> 

管理bean功能應如下所示。單擊該按鈕時,這個動作監聽器被調用,然後它檢索所選擇的人的ID和遍歷人員名單找到你正在尋找一個:

public void findSelectedPerson(ActionEvent event){ 
    if(event.getComponent().getAttributes().get("personid") != null){ 
     Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap(); 
     int personid = (params.get("personid")!= null) ? Integer.parseInt(params.get("personid")) : -1; 
     // Loop through the persons array 
     for(Person p : listPersons){ 
     if(p.getId() == personid){ 
      selectedPerson = p; 
      break; 
     } 
    } 

}

相關問題