0

The following code implements a datatable inside a layout, in the datatable i added an edit button in each row

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

When i click on the edit button, the dialog box (code below) is shown but the inputs are empty, what i wanted is to show the infos of the row in the dialog bow, i'm still a beginner, i searched everywhere... but no results

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

i would be so grateful if anyone can help, i really need this

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
kelmag
  • 1
  • 1
  • 1
  • Try to give the form inside the dialog an id and put update=":idOfForm" on the button. And also change the bean to ViewScoped, RequestScoped is rare – Jaqen H'ghar Apr 04 '16 at 05:52

1 Answers1

-1

Change the command button to the following, use an actionlistener:

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

This is the dialog, add the id property to it. Then change the value of the panel grid to selectedPerson because this corresponds to the correct object in the 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>

The managed bean function should look as follows. This action listener is called when the button is clicked and it then retrieves the id of the selected person and loops through the list of persons to find the one you are looking for:

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

}

ArnoldKem
  • 100
  • 2