Primefaces Datatable 项目选择、确认对话框和 RequestContext
Primefaces Datatable item selection, confirmation dialog and RequestContext
编辑: 在构造视图时我尝试填充集合后一切正常,在该视图的支持 bean 的 @PostContsruct init() 方法中 class.它是@ViewScoped。所以问题在于组件不知何故无法识别用作数据值的集合table,实际上具有值,因为它以不常见的方式填充。无论如何,如果我在构建视图时填充集合,一切都会完美无缺。 但这当然不是解决方案,因为必须在提到的模糊 ajax 事件中填充集合。 所以这是某种生命周期/范围问题。
EDIT2: 我试图将 table 添加到与其中具有 ajax 模糊效果的组件相同的表单中,显示弹出窗口和该弹出窗口中的数据 table 并填充集合。这也奏效了。因此,生命周期/范围在 ajax 模糊事件和数据 table 功能方面有所不同,因为它们采用不同的形式。现在我应该以某种方式设法将它们保持在同一范围内,同时在弹出对话框中包含数据table。现在,出于上述测试目的,数据table 笨拙地以相同的形式驻留。
我在 p:confirmDialog
中有一个 Primefaces p:dataTable
,当我 select 一个项目时,该项目的 setter 只获得 null
值。所以设置了一个 null
值,下面的操作显然不起作用,因为我们没有可以使用的值。
我认为问题与我必须使用 RequestContext
来显示对话框有关。这是因为在 form
.
的 blur
事件中检查集合时应显示该对话框
首先,从 InputText 检查 blur
事件:
<p:inputText id="customerIdInput"
value="#{newCustomerCaseController.id}"
label="custId" required="true">
<f:ajax event="blur" listener="#{newCustomerCaseController.performBackgroundSearch}" />
</p:inputText>
然后,在那个 blur
事件上,将在支持 bean 中调用侦听器方法,它通过 RequestContext
显示如下对话框:
public void performBackgroundSearch() {
showPopUpBecauseCertainConditionsAreMet();
}
public void showPopUpBecauseCertainConditionsAreMet() {
setExistingCustomerCases(customerCaseService.searchByEmployerId(employerId));
RequestContext.getCurrentInstance().update("caseform");
RequestContext.getCurrentInstance().execute(
"PF('employerAlreadyHasCustomerCasesDialog').show()");
}
}
这是 xhtml 中的确认对话框和 table:
<p:confirmDialog widgetVar="employerAlreadyHasCustomerCasesDialog" width="500" appendTo="@(body)" showEffect="fade" hideEffect="fade"
closable="false" >
<h:form id="caseform">
<p:dataTable id="casetable" var="custcase" rowKey="#{custcase.caseId}" value="#{newCustomerCaseController.existingCustomerCases}"
selectionMode="single" selection="#{newCustomerCaseController.selectedAlreadyExistingCustomerCase}" >
<p:ajax event="rowSelect" process="@this" listener="#{newCustomerCaseController.onExistingCaseRowSelect()}" />
<p:column headerText="caseId">
<h:outputText value="#{custcase.caseId}" />
</p:column>
</p:dataTable>
</h:form>
<!--some unimportant buttons which we don't touch in this example-->
</p:confirmDialog>
最后,让我们看看onExistingCaseRowSelect
监听器方法做了什么。它只尝试重定向,例如使用 customerCase
的 caseId
导航到不同的 url,我们在 p:dataTable
.[=33 中尝试 select =]
public void onExistingCaseRowSelect(SelectEvent event) {
try {
facesContext.getExternalContext().redirect("/customercase.xhtml?caseId=" + selectedAlreadyExistingCustomerCase.getCaseId());
}catch (Exception e) {
}
}
这里的问题是 selectedAlreadyExistingCustomerCase
在 table 中单击时没有在辅助 bean 中设置。 当我单击 table 中的行时,正在设置 null
。 我已经用 getter 和 [=68= 中的打印行检查了这一点].我认为这里的问题是由于通过 RequestContext.getCurrentInstance().update("componentName");
更新视图而发生了一些事情
如何在 table 中获取单击的行值 - customerCase
即 - 设置为支持 bean 中的 selected 值,而不是null
?
设法让它工作。所以这就是我的做法:我将对话框 window 与数据表放在一起,并将其全部放入启动 ajax 模糊事件的相同形式。但是我从来没有让 setter 工作,它仍然是空的。无论如何,正如@Kukeltje 在评论中所建议的那样,我使用了 event.getObject(),它只有在将所有内容都置于相同的形式后才起作用。
编辑: 在构造视图时我尝试填充集合后一切正常,在该视图的支持 bean 的 @PostContsruct init() 方法中 class.它是@ViewScoped。所以问题在于组件不知何故无法识别用作数据值的集合table,实际上具有值,因为它以不常见的方式填充。无论如何,如果我在构建视图时填充集合,一切都会完美无缺。 但这当然不是解决方案,因为必须在提到的模糊 ajax 事件中填充集合。 所以这是某种生命周期/范围问题。
EDIT2: 我试图将 table 添加到与其中具有 ajax 模糊效果的组件相同的表单中,显示弹出窗口和该弹出窗口中的数据 table 并填充集合。这也奏效了。因此,生命周期/范围在 ajax 模糊事件和数据 table 功能方面有所不同,因为它们采用不同的形式。现在我应该以某种方式设法将它们保持在同一范围内,同时在弹出对话框中包含数据table。现在,出于上述测试目的,数据table 笨拙地以相同的形式驻留。
我在 p:confirmDialog
中有一个 Primefaces p:dataTable
,当我 select 一个项目时,该项目的 setter 只获得 null
值。所以设置了一个 null
值,下面的操作显然不起作用,因为我们没有可以使用的值。
我认为问题与我必须使用 RequestContext
来显示对话框有关。这是因为在 form
.
blur
事件中检查集合时应显示该对话框
首先,从 InputText 检查 blur
事件:
<p:inputText id="customerIdInput"
value="#{newCustomerCaseController.id}"
label="custId" required="true">
<f:ajax event="blur" listener="#{newCustomerCaseController.performBackgroundSearch}" />
</p:inputText>
然后,在那个 blur
事件上,将在支持 bean 中调用侦听器方法,它通过 RequestContext
显示如下对话框:
public void performBackgroundSearch() {
showPopUpBecauseCertainConditionsAreMet();
}
public void showPopUpBecauseCertainConditionsAreMet() {
setExistingCustomerCases(customerCaseService.searchByEmployerId(employerId));
RequestContext.getCurrentInstance().update("caseform");
RequestContext.getCurrentInstance().execute(
"PF('employerAlreadyHasCustomerCasesDialog').show()");
}
}
这是 xhtml 中的确认对话框和 table:
<p:confirmDialog widgetVar="employerAlreadyHasCustomerCasesDialog" width="500" appendTo="@(body)" showEffect="fade" hideEffect="fade"
closable="false" >
<h:form id="caseform">
<p:dataTable id="casetable" var="custcase" rowKey="#{custcase.caseId}" value="#{newCustomerCaseController.existingCustomerCases}"
selectionMode="single" selection="#{newCustomerCaseController.selectedAlreadyExistingCustomerCase}" >
<p:ajax event="rowSelect" process="@this" listener="#{newCustomerCaseController.onExistingCaseRowSelect()}" />
<p:column headerText="caseId">
<h:outputText value="#{custcase.caseId}" />
</p:column>
</p:dataTable>
</h:form>
<!--some unimportant buttons which we don't touch in this example-->
</p:confirmDialog>
最后,让我们看看onExistingCaseRowSelect
监听器方法做了什么。它只尝试重定向,例如使用 customerCase
的 caseId
导航到不同的 url,我们在 p:dataTable
.[=33 中尝试 select =]
public void onExistingCaseRowSelect(SelectEvent event) {
try {
facesContext.getExternalContext().redirect("/customercase.xhtml?caseId=" + selectedAlreadyExistingCustomerCase.getCaseId());
}catch (Exception e) {
}
}
这里的问题是 selectedAlreadyExistingCustomerCase
在 table 中单击时没有在辅助 bean 中设置。 当我单击 table 中的行时,正在设置 null
。 我已经用 getter 和 [=68= 中的打印行检查了这一点].我认为这里的问题是由于通过 RequestContext.getCurrentInstance().update("componentName");
如何在 table 中获取单击的行值 - customerCase
即 - 设置为支持 bean 中的 selected 值,而不是null
?
设法让它工作。所以这就是我的做法:我将对话框 window 与数据表放在一起,并将其全部放入启动 ajax 模糊事件的相同形式。但是我从来没有让 setter 工作,它仍然是空的。无论如何,正如@Kukeltje 在评论中所建议的那样,我使用了 event.getObject(),它只有在将所有内容都置于相同的形式后才起作用。