You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

同一界面Grid间默认值同步:需选字段后预保存前更新

实时同步两个Grid默认值的解决方案

我来帮你搞定这个实时同步的问题~你现在的代码是在TSFormulaByProds的DefaultSiteID字段渲染时取TSFormula的默认值,但这个事件只会在字段需要显示的时候触发,而且没有主动刷新第二个Grid的缓存,所以只有保存后缓存更新了才会看到变化。要实现选中第一个Grid字段后、保存前就同步,得从监听第一个Grid的变化并主动更新第二个Grid缓存入手:

方案1:监听第一个Grid的行选中事件

当第一个Grid(TSFormula对应的Grid)的行被选中或者字段修改时,直接更新第二个Grid(TSFormulaByProds)的所有行的DefaultSiteID,然后刷新界面:

protected void TSFormula_RowSelected(PXCache cache, PXRowSelectedEventArgs e)
{
    var formulaRow = (TSFormula)e.Row;
    if (formulaRow == null) return;

    // 获取第二个Grid的数据缓存
    var formulaByProdCache = this.Caches[typeof(TSFormulaByProds)];
    
    // 遍历第二个Grid的所有行,更新默认站点ID
    foreach (TSFormulaByProds row in formulaByProdCache.Select())
    {
        // 跳过已标记删除的行,根据你的需求调整
        if (formulaByProdCache.GetStatus(row) != PXEntryStatus.Deleted)
        {
            // 用SetValueExt确保字段的验证逻辑也能触发(如果有的话)
            formulaByProdCache.SetValueExt<TSFormulaByProds.defaultSiteID>(row, formulaRow.DefaultSiteID);
        }
    }

    // 触发字段更新事件,刷新缓存状态
    formulaByProdCache.RaiseFieldUpdated<TSFormulaByProds.defaultSiteID>(null, null);
    // 主动请求第二个Grid刷新界面
    this.TSFormulaByProds.View.RequestRefresh();
}

方案2:监听第一个Grid的字段修改事件

如果只需要在TSFormula的DefaultSiteID字段修改时才同步,而不是行选中就同步,可以用FieldUpdated事件:

protected void TSFormula_DefaultSiteID_FieldUpdated(PXCache cache, PXFieldUpdatedEventArgs e)
{
    var formulaRow = (TSFormula)e.Row;
    if (formulaRow == null) return;

    var formulaByProdCache = this.Caches[typeof(TSFormulaByProds)];
    foreach (TSFormulaByProds row in formulaByProdCache.Select())
    {
        if (formulaByProdCache.GetStatus(row) != PXEntryStatus.Deleted)
        {
            formulaByProdCache.SetValueExt<TSFormulaByProds.defaultSiteID>(row, formulaRow.DefaultSiteID);
        }
    }

    formulaByProdCache.RaiseFieldUpdated<TSFormulaByProds.defaultSiteID>(null, null);
    this.TSFormulaByProds.View.RequestRefresh();
}

为什么原来的代码不生效?

你原来的FieldSelecting事件只是在TSFormulaByProds的DefaultSiteID要显示时返回一个值,但并没有修改缓存中该行的实际数据。当你保存后,缓存数据被持久化,下次渲染时才会拿到新值。而上面的方案直接修改了缓存中的数据,并主动请求界面刷新,所以不用保存就能看到同步后的结果。

内容的提问来源于stack exchange,提问作者Suri yadav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:57:31