将参数从数据表传递到p:对话框

时间:2016-04-03 22:52:27

标签: jsf jsf-2 primefaces datatable dialog

以下代码在布局中实现数据表,在数据表中我在每行中添加了一个编辑按钮

<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;
    }
}

如果有人能提供帮助,我将非常感激,我真的需要这个

1 个答案:

答案 0 :(得分:-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;
        }
    }

}