gpt4 book ai didi

jsf - 如何显示 p :dataTable in a p:dialog and update after save 中当前行的详细信息

转载 作者:行者123 更新时间:2023-12-02 00:12:29 25 4
gpt4 key购买 nike

我有一个 JSF 2 应用程序,它有两页,一页用于列出学生,另一页用于显示给定学生的详细信息。列表页面在学生表的每一行中都有一个指向详细信息页面的链接,单击该链接会在浏览器中打开一个新选项卡以显示这些详细信息。

现在,要求更改为不再在新选项卡中显示详细信息,而是在列表页面的模式对话框中显示详细信息。

我的想法是简单地将详细信息页面内容嵌入到模式对话框中,这样列表页面就不会变得太大并且难以维护。从这里开始我的疑惑。经过一番研究后,我将列表每一行中的链接更改为以下按钮:

<p:commandButton value="Details" type="button"
onclick="PF('dialog-details').show()">
</p:commandButton>

对话框声明如下:

<p:dialog widgetVar="dialog-details" header="Details" modal="true" width="95%">
<ui:include src="student_details.xhtml">
<ui:param name="id" value="#{student.id}"/>
</ui:include>
</p:dialog>

最后,详情页改成这样:

<ui:composition
xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui" xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">

<f:metadata>
<f:viewParam name="id" value="#{studentBean.id}" />
</f:metadata>

<h1 class="title ui-widget-header ui-corner-all">Details of #{studentBean.bean.name} / #{studentBean.bean.number}</h1>
</ui:composition>

当我单击按钮时,对话框真正显示,内容是详细信息页面。我在对话框中看到以下内容:

Details of  / 

完全没有错误,但应该显示的数据却没有。断点设置在 StudentBean.setId() (此方法加载名为 bean 的属性,其中 Student 实例对应于传递的 id),但它永远不会被命中。

经过一段时间的思考,我明白了为什么它不起作用。详情页传递的参数为student.id ,但是student是用作 var 的名称在 <p:datatable/>显示所有学生,所以 student<p:dialog/> 中无效位于 <p:datatable/> 之外.

所以,我需要的是一种使用给定行中相应学生的 id 显示对话框的方法。理想情况下,我希望在此处进行 ajax 调用,以便仅在需要时才加载详细信息。

有什么想法吗?

最佳答案

该按钮应该是一个ajax按钮,它设置bean中当前迭代的实体,然后更新对话框的内容,最后显示它。该对话框应该只引用 bean 中的该实体并在保存时更新列表和表格。将对话框放置在主窗体之外并拥有自己的窗体非常重要。

这是一个启动示例:

<h:form id="master">
<p:dataTable value="#{bean.entities}" var="entity">
<p:column>#{entity.property1}</p:column>
<p:column>#{entity.property2}</p:column>
<p:column>#{entity.property3}</p:column>
...
<p:column>
<p:commandButton value="View" action="#{bean.setEntity(entity)}"
update=":detail" oncomplete="PF('detail').show()" />
</p:column>
</p:dataTable>
</h:form>

<p:dialog id="detail" widgetVar="detail">
<h:form>
<p:inputText value="#{bean.entity.property1}" />
<p:inputText value="#{bean.entity.property2}" />
<p:inputText value="#{bean.entity.property3}" />
...
<p:button value="Close" onclick="PF('detail').hide(); return false" />
<p:commandButton value="Save" action="#{bean.save}"
update=":master" oncomplete="if(!args.validationFailed) PF('detail').hide()" />
</h:form>
</p:dialog>

将其放在 @ViewScoped bean 中:

private List<Entity> entities; // +getter
private Entity entity; // +getter+setter

@EJB
private EntityService entityService;

@PostConstruct
public void load() {
entities = entityService.list();
entity = null;
}

public void save() {
entityService.save(entity);
load();
}

另请参阅:

关于jsf - 如何显示 p :dataTable in a p:dialog and update after save 中当前行的详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28236228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com