tul*_*s85 6 ajax jsf primefaces
我有一个动态数据表列表,我需要为每个表的单行启用行选择。下面的代码仅当用户选择最后一个数据表的一行时才起作用,可能是因为 Ajax 事件被替换并且只有最后一个事件起作用。
如果用户从另一个数据表中选择一行,则会调用 onRowSelect 方法,但有一个NullPointerExceptionon 变量selectedRow。
也许我需要在Java bean中创建多个onrowselect方法,每个数据表一个,但该表的数量是可变的。
我该如何解决这个问题?
<c:forEach items="#{azPerformancePrenPubAll.selectedCompanyTemp}" var="companyCode" varStatus="loop">
<p:accordionPanel id="acc_#{companyCode}" widgetVar="accordionAziendale_#{companyCode}" activeIndex="-1">
<p:tab title="#{azPerformancePrenPubAll.selectedCompanyName.get(loop.index)}">
<p:dataTable id="tablePerformance_#{companyCode}" rendered="#{!azPerformancePrenPubAll.isCompanyVisible}"
widgetVar="tablePerformance" var="performance" value="#{azPerformancePrenotatiPubAll.listPerformances.get(loop.index)}"
styleClass="perfDataTable no-border" rowIndexVar="rowIndex"
selectionMode="single" selection="#{azPerformancePrenPubAll.selectedRow}" rowKey="#{performance.id}">
<p:ajax event="rowSelect" listener="#{azPerformancePrenPubAll.onRowSelect}" update="formPerformance,pageSubDescription,pageDescription"/>
...
Run Code Online (Sandbox Code Playgroud)
如果用户从数据表中选择一行而不是最后一行,则selectedRow在循环中使用公共变量可能会导致出现错误。NullPointerException
您可以尝试在支持 bean中创建一个 Map来保存selectedRow每个companyCode.
private Map<String, YourDataType> selectedRows = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)
然后在你的 XHTML 中:
selection="#{azPerformancePrenPubAll.selectedRows[companyCode]}"
Run Code Online (Sandbox Code Playgroud)
此外,您还可以将companyCode或任何其他唯一标识符传递给侦听器中的方法。
<p:ajax event="rowSelect" listener="#{azPerformancePrenPubAll.onRowSelect(companyCode)}" />
Run Code Online (Sandbox Code Playgroud)
在您的 Java bean 中:
public void onRowSelect(String companyCode) {
// Logic here
}
Run Code Online (Sandbox Code Playgroud)
当您使用 时widgetVar="tablePerformance",请确保这对于每个循环迭代也是唯一的。
使用独特的 widgetVar 作品,我仍然只使用变量 selectedRow
如果您确认使用 uniquewidgetVar可以解决问题,同时仍然使用单个selectedRow变量,则可以按如下方式修改答案:
您可以NullPointerException通过确保每个数据表组件都有唯一的widgetVar. 这将允许每个表在客户端独立操作,防止selectedRow.
widgetVar="tablePerformance_#{companyCode}"
Run Code Online (Sandbox Code Playgroud)
通过使其唯一,无论哪个表触发事件,都可以正确更新widgetVar单个变量。selectedRowrowSelect
这简化了您的支持 bean,因为无需为每个方法维护映射companyCode或处理多个onRowSelect方法。它满足功能要求并且是一个干净的解决方案。
| 归档时间: |
|
| 查看次数: |
172 次 |
| 最近记录: |