Primefaces10升级后datatable切换选中行时次表rowkey残留无法清空求助
PrimeFaces10 主从表选中状态累加问题解决方案
该问题是PrimeFaces10的已知前端渲染逻辑缺陷,多选DataTable的widget对象会缓存已选中的rowKey数组,后端更新selection属性时只会追加新值到缓存中,不会清空历史缓存,所以出现选中行累加现象,后端Java代码的selection值实际是正确的。
可行解决方案
方案1:前端手动清空缓存(优先选择,不影响其他业务场景)
直接修改表1的rowSelect事件ajax配置,在请求发起前清空表2的前端rowKey缓存:
<p:ajax event="rowSelect" process="@this" onstart="PF('groupsTable').selectedRowKeys = []; PF('groupsTable').unselectAllRows();" update=":hw-editmenu:hw-emf :dlgSaveComp:dlgAskSaveForm :dlgDeleteComp:dlgAskDeleteForm :eKeyListForm :tabView:groupsForm:groupsTable" />
- 原理:直接操作PF组件前端实例的
selectedRowKeys属性,彻底清除上一次的缓存记录,规避unselectAllRows方法不清空缓存的bug。
方案2:修正后端更新逻辑笔误
你提供的Backing Bean代码中存在id拼写错误,也会导致表2更新失效,修改refreshSelectedChildGroups方法中的更新id:
private void refreshSelectedChildGroups() { if (selectedEKey != null) { this.setSelChildGroups(selectedEKey.getUserGroups()); } else { this.setSelChildGroups(new ArrayList<>()); } // 原代码id为groupTable,实际表id是groupsTable,修正拼写 PrimeFaces.current().ajax().update(Arrays.asList("tabView:groupsForm:groupsTable")); }
方案3:后端显式重置选中值
如果需要从后端层面避免引用绑定问题,可以修改赋值逻辑,先清空再赋值新的选中集合:
private void refreshSelectedChildGroups() { // 先显式清空旧选中值 this.setSelChildGroups(new ArrayList<>()); if (selectedEKey != null) { // 新建集合赋值,避免和原业务对象引用绑定 this.setSelChildGroups(new ArrayList<>(selectedEKey.getUserGroups())); } PrimeFaces.current().ajax().update(Arrays.asList("tabView:groupsForm:groupsTable")); }
内容的提问来源于stack exchange,提问作者R.Quinlan
相关产品推荐
相关产品推荐

