首页 > 解决方案 > 如何防止 PrimeFaces 可编辑 DataTable 在变量中设置更新值?

问题描述

我的 PrimeFaces (7.0) DataTable 中有这些 ajax 侦听器:

<p:ajax event="rowEditInit" listener="#{editKey.onRowEditInit(event, key)}"/>
<p:ajax event="rowEdit" listener="#{editKey.onRowEdit(event, key)}"/>

onRowEditInit在我的 bean 中使用它来保存行的初始值,oldKey当它处于编辑模式时,它工作正常。

private KeyManagementKeys oldKey;

public void onRowEditInit(RowEditEvent event, KeyManagementKeys key) {
    oldKey = key;
}

public void onRowEdit(RowEditEvent event, KeyManagementKeys newkey) {
    keylistService.updateKey(newkey, oldKey);
}

问题是,oldKey当我在 UI 中编辑行时,它会更新为编辑后的值。所以通过onRowEdit调用时间来完成编辑,newKey并且oldKey具有相同的值。

如何防止与 DataTable 值 ( KeyManagementKeys) 具有相同类型的任何变量的自动值更新?任何帮助是极大的赞赏!

标签: ajaxprimefaces

解决方案


你能试试这个(我不知道 KeyManagementKeys 类如何,所以代码可能需要一些更改),以解耦这两个变量

public void onRowEditInit(RowEditEvent event, KeyManagementKeys key) {
    KeyManagementKeys oldKey = new KeyManagementKeys();
    BeanUtils.copyProperties(oldKey, key);
}

推荐阅读