标签:多选基础资料
需求背景:多选基础资料赋值
实现方案:
场景一:新增单据时,多选基础资料为空
public class Bill0702Plugin extends AbstractBillPlugIn {
// 单据上多选基础资料字段标识(以用户为例),按实际业务修改
private static String KEY_MULBASEDATA = "users";
// 多选基础资料字段待赋的值
private String [] ids = {"651428061366979584", "651428061366978561"};
@Override
public void afterCreateNewData(EventObject e) {
super.afterCreateNewData(e);
// 方法一
// this.getModel().setValue(KEY_MULBASEDATA, ids);
// 方法二
DynamicObjectCollection usersForDisplay = new DynamicObjectCollection();
DynamicObjectType type = EntityMetadataCache.getDataEntityType("bos_user");
DynamicObject tempUser = null;
for (String tempId : ids) {
tempUser = BusinessDataServiceHelper.loadSingle(tempId, type);
usersForDisplay.add(tempUser);
}
this.getModel().setValue(KEY_MULBASEDATA, usersForDisplay);
}
}
场景二:多选基础资料已有值时如何赋值
public class Bill0702Plugin extends AbstractBillPlugIn {
// 单据上多选基础资料字段标识(以用户为例),按实际业务修改
private static String KEY_MULBASEDATA = "users";
// 多选基础资料字段待赋的值
private String [] ids = {"651428061366979584", "651428061366978561"};
@Override
//多选基础资料已有值时如何赋值
@Override
public void propertyChanged(PropertyChangedArgs e) {
// TODO Auto-generated method stub
super.propertyChanged(e);
String key = e.getProperty().getName();
ChangeData[] changeSet = e.getChangeSet();
if(key.equals("kded_userfield")) {
DynamicObject newValue = (DynamicObject) changeSet[0].getNewValue();
//待赋值的数据
String uid= (Long) newValue.get("masterid")+"";
//方法一:取多选基础资料值DynamicObjectCollection,将待赋值数据DynamicObject加入
//这种方法缺点是当待赋值与已赋值多选基础资料重复时,会导致多选基础资料重复,需要自己手动去重
DynamicObjectCollection personcol = this.getModel().getDataEntity(true).getDynamicObjectCollection(KEY_MULBASEDATA);
DynamicObject data = BusinessDataServiceHelper.loadSingle(uid, "bos_user");
personcol.addNew().set("fbasedataid",data);
this.getView().updateView(KEY_MULBASEDATA);
//方法二:将待赋值数据加入到ids数组,再重新赋值
/*
String[] uids = addArray(ids,uid);
//赋值
this.getModel().setValue(KEY_MULBASEDATA, uids);*/
}
}
}
实现效果:
推荐阅读