It's easy if you use a view scoped bean. Basically, you just need to set the current item of the data list as a property of the bean and update the dialog's content which should present that item and then open the dialog. Assuming that the dialog represents an edit form which you'd like to close on successful save, then you'd like to update the data list as well with the new value. You could use <p:remoteCommand>
for this. You can of course also add the client ID of the data list to the update
attribute of the command button in the dialog, but that's thus unnecessary when there's a validation error.
Here's a concrete kickoff example:
<h:form>
<p:dataList id="list" value="#{bean.items}" var="item">
#{item.id} #{item.value}
<p:commandButton value="edit"
action="#{bean.setItem(item)}"
update=":editForm"
oncomplete="editDialog.show()" />
</p:dataList>
<p:remoteCommand name="updateList" update="list" />
</h:form>
<p:dialog widgetVar="editDialog">
<h:form id="editForm">
<p:messages autoUpdate="true" />
<h:inputText value="#{bean.item.value}" required="true" />
<p:commandButton value="save"
action="#{bean.save}"
oncomplete="if (!args.validationFailed) { editDialog.hide(); updateList(); }" />
</h:form>
</p:dialog>
with this bean:
@ManagedBean
@ViewScoped
public class Bean implements Serializable {
private List<Item> items;
private Item item;
@EJB
private ItemService service;
@PostConstruct
public void loadItems() {
items = service.list();
}
public void save() {
service.save(item);
loadItems();
}
public List<Item> getItems() {
return items;
}
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
}