How to programmatically add dialogs with primeface

2019-08-27 16:58发布

问题:

I want to display some data through primefaces datalist component. To achieve this I have an arrayList like ArrayList<Person>.

The person class looks something like this

class Person{
    private String name;
    private String age;
    private ArrayList<String> hobbies;
}

To display the data I'm using the following code:

<p:dataList value="{gameBean.persons}" var="person" itemType="disc">  
    Name: #{person.getName()}, Age: #{person.getAge()}, 
    <h:link value="Hobbies" onclick="dlg1.show();" />
</p:dataList>

What I want do do now, is to create a link that opens a dialog when clicked:

 <p:dialog header="Hobbies" widgetVar="dlg1" modal="true"height="100">
    //iterate through hobbies list to print it 
 </p:dialog>

So far this is working because I've hard coded the dialog as mentioned above in the xhtml file.

This method is of course not working for a dynamic amount of persons as I can not hard code the dialogs and the links. My question is, how can I create this dialogs programmatically and assign the right widgetVar variable to the onClick method of the Links?

Any help is highly apprechiated, cheers Nikolaus

回答1:

You can try this:

<h:form id="form">
    <p:dataList value="{gameBean.persons}" var="person" itemType="disc">  
        Name: #{person.getName()}, Age: #{person.getAge()}, 
        <p:column>
            <p:commandLink value="Hobbies" actionListener="#{gameBean.onPersonSelect(person)}" 
                       oncomplete="dlg1.show();" update=":form:hobbiesDlg" />
        </p:column>
    </p:dataList>

    <p:dialog header="Hobbies" id="hobbiesDlg" widgetVar="dlg1" modal="true"height="100">
        //iterate through hobbies of gameBean.person to show here
    </p:dialog>
</h:form>

@ManagedBean
@ViewScoped
public class GameBean {
   private Person person;

   public void onPersonSelect(Person person) {
      this.person = person;
   }
}