动态修改单据头字段,分录/单据体字段的前端显示值原创
金蝶云社区-丁梦洋
丁梦洋
0人赞赏了该文章 94次浏览 未经作者许可,禁止转载编辑于2024年07月24日 09:44:17

在列表插件里,可以通过packageData事件修改单元格的显示值,如下

@Override
public void packageData(PackageDataEvent e) {
    super.packageData(e);
    AbstractColumnDesc columnDesc = (AbstractColumnDesc) e.getSource();
    if ("列字段标识".equals(columnDesc.getFieldKey())){
        e.setFormatValue("替换文本");
    }
}

那可否修改表单里字段的前端显示值?当然可以!

对于基础资料类型的字段,不管是单据头上的,还是分录上的,都可以给字段控件注册BasedataEditListener来实现。

afterBindingData事件在构建字段前端值后,返回前端前调用(页面显示,F7选完值都会触发),通过修改displayProp设置显示属性,修改editSearchProp设置编辑显示属性,下面代码是给“创建人”字段显示值拼接"【测试】"。

@Override
public void initialize() {
    super.initialize();
    BasedataEdit basedataEdit = this.getControl("creator");
    basedataEdit.addBasedataEditListener(this);
}

@Override
public void afterBindingData(AfterBindingDataEvent evt) {
    DynamicObject value = (DynamicObject) evt.getDataEntity();
    if (Objects.isNull(value)){
        return;
    }
    evt.setDisplayProp("【测试】"+value.getString("name"));
    evt.setEditSearchProp("【测试】"+value.getString("name"));
}


对于分录/单据体字段,可以给分录控件EntryGrid注册DataBindListener来实现。

entryGridBindData事件在分录封装完前端格式数据data,返回给前端前调用。这个data格式有点复杂,行数据在“rows”里,需要先从“dataindex”(字段名与字段索引值的映射)拿到字段索引,此索引对应此字段的值在“rows”里的位数,然后重新set值。注意这个值的类型不固定,要根据字段类型,比如基础资料字段就是一个object数组。

image.png

下面代码是如果分录文本字段“a2h6_textfield3”的值为空,替换显示值为“—”(此方法只改前端显示,不改后端model里的值)

@Override
public void initialize() {
    super.initialize();
    EntryGrid entryGrid = this.getControl("a2h6_entryentity");
    entryGrid.addDataBindListener(this);
}

@Override
public void entryGridBindData(EntryGridBindDataEvent e) {
    Map<String, Object> data = e.getData();
    List<Object> rows = (List<Object>) data.get("rows");
    if (CollectionUtils.isEmpty(rows)){
        return;
    }
    Map<String, Integer> dataindex = (Map<String, Integer>) data.get("dataindex");
    Integer index = dataindex.get("a2h6_textfield3");
    for (Object rowObj : rows) {
        List<Object> list = (List<Object>) rowObj;
        if (StringUtils.isEmpty(String.valueOf(list.get(index)))) {
            list.set(index, "—");
        }
    }
}


对于单据头字段,甚至是分录字段,可以在afterBindData方法里,再次设置前端值,覆盖平台已设置的值。

@Override
public void afterBindData(EventObject e) {
    super.afterBindData(e);
    //单据头字段
    IClientViewProxy clientViewProxy = this.getView().getService(IClientViewProxy.class);
    clientViewProxy.setFieldProperty("name", "v", new LocaleString("【测试】"+this.getModel().getValue("name")));

    EntryGrid grid = this.getControl("a2h6_entryentity");
    DynamicObjectCollection entry = this.getModel().getEntryEntity("a2h6_entryentity");
    for (int i = 0; i < entry.size(); i++) {
        //分录字段,如果字段值为空,替换为—
        clientViewProxy.setEntryFieldValue(grid, "a2h6_textfield3", i,
                StringUtils.defaultIfEmpty(entry.get(i).getString("a2h6_textfield3"),"—"));
    }
}

setFieldProperty和setEntryFieldValue是平台设置字段前端值所用的方法,是最底层的,也是最通用的,不过要注意设置的新值的格式类型,得和平台设置的类型一致,这里我们可以给这两方法所在行打断点,进入方法后,选中this.getControlState(key),运行代码,来对照平台设置的旧值。

image.png

赞 0