单据体动态添加基础资料列原创
金蝶云社区-云社区用户18b96283
云社区用户18b96283
3人赞赏了该文章 2,913次浏览 未经作者许可,禁止转载编辑于2021年10月13日 17:08:38
  1. 页面设计


    Image_20211013165728.png

  2. 运行效果


    Image_20211013170437.png

  3. 实现过程


  4. package demo.formplugin;
    
    import kd.bos.dataentity.entity.DynamicObject;
    import kd.bos.dataentity.entity.DynamicObjectCollection;
    import kd.bos.dataentity.entity.LocaleString;
    import kd.bos.dataentity.metadata.dynamicobject.DynamicSimpleProperty;
    import kd.bos.entity.EntityMetadataCache;
    import kd.bos.entity.EntryType;
    import kd.bos.entity.MainEntityType;
    import kd.bos.entity.datamodel.RowDataEntity;
    import kd.bos.entity.datamodel.events.AfterAddRowEventArgs;
    import kd.bos.entity.datamodel.events.BizDataEventArgs;
    import kd.bos.entity.datamodel.events.GetEntityTypeEventArgs;
    import kd.bos.entity.property.BasedataProp;
    import kd.bos.entity.property.EntryProp;
    import kd.bos.form.BindingContext;
    import kd.bos.form.ClientProperties;
    import kd.bos.form.IFormView;
    import kd.bos.form.container.Container;
    import kd.bos.form.container.FormRoot;
    import kd.bos.form.control.Control;
    import kd.bos.form.control.EntryGrid;
    import kd.bos.form.control.events.ItemClickEvent;
    import kd.bos.form.events.BeforeDoOperationEventArgs;
    import kd.bos.form.events.LoadCustomControlMetasArgs;
    import kd.bos.form.events.OnGetControlArgs;
    import kd.bos.form.field.BasedataEdit;
    import kd.bos.form.field.events.BeforeF7SelectEvent;
    import kd.bos.form.field.events.BeforeF7SelectListener;
    import kd.bos.form.plugin.AbstractFormPlugin;
    import kd.bos.metadata.entity.businessfield.BasedataField;
    import kd.bos.metadata.form.control.EntryAp;
    import kd.bos.metadata.form.control.EntryFieldAp;
    import kd.pkcosmic.framework.util.PKDBDUtils;
    import kd.pkcosmic.framework.util.PKPublicUtils;
    
    import java.util.EventObject;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class DynamicAddColumnsPlugin extends AbstractFormPlugin implements BeforeF7SelectListener {
    
        private static String KEY_ENTRY_ENTITY="pk_entryentity";
    
        @Override
        public void initialize() {
            super.initialize();
            this.getView().addCustomControls(KEY_ENTRY_ENTITY);
        }
        @Override
        public void registerListener(EventObject e) {
            super.registerListener(e);
            this.addItemClickListeners("pk_advcontoolbarap");
        }
    
        @Override
        public void afterAddRow(AfterAddRowEventArgs e) {
            //新增分录 赋默认值 1 最好写在这里
            RowDataEntity[] rowDataEntities=e.getRowDataEntities();
            long materialId=1113786302362302464L;
            for (int index = 0; index < rowDataEntities.length; index++) {
                RowDataEntity rowDe =rowDataEntities[index];
                getModel().setValue("material",materialId,rowDe.getRowIndex());
            }
    
        }
    
        @Override
        public void itemClick(ItemClickEvent evt) {
            super.itemClick(evt);
            String key=evt.getItemKey();
            if ("pk_advconbaritemap".equals(key)){
                int row=this.getModel().getEntryRowCount(KEY_ENTRY_ENTITY);
                long materialId=1113786302362302464L;
                //新增分录 赋默认值 2
                this.getModel().setValue("material",materialId,row-1);
            }
        }
    
        @Override
        public void beforeDoOperation(BeforeDoOperationEventArgs args) {
            super.beforeDoOperation(args);
            Object source=args.getSource();
        }
    
        @Override
        public void beforeBindData(EventObject e) {
            super.beforeBindData(e);
            EntryAp entryAp =this.createEntryAp();
            IFormView formView=this.getView();
            EntryGrid entryGrid = formView.getControl(KEY_ENTRY_ENTITY);
            List<Control> fieldEdits = entryAp.buildRuntimeControl().getItems();
            for(Control fieldEdit : fieldEdits){
                fieldEdit.setView(formView);
                entryGrid.getItems().add(fieldEdit);
                //分组容器,重新获取分组容器里的控件
                if (fieldEdit instanceof Container) {
                    List<Control> listCol = ((Container)fieldEdit).getItems();
                    for (Control col: listCol) {
                        col.setView(formView);
                        entryGrid.getItems().add(col);
                    }
                }
            }
        }
    
    
        @Override
        public void loadCustomControlMetas(LoadCustomControlMetasArgs e) {
            super.loadCustomControlMetas(e);
            Map<String, Object> mapEntry = new HashMap<>();
            EntryAp entryAp =this.createEntryAp();
            mapEntry.put(ClientProperties.Id, KEY_ENTRY_ENTITY);
            mapEntry.put(ClientProperties.Columns, entryAp.createControl().get(ClientProperties.Columns));
            e.getItems().add(mapEntry);
        }
    
        @Override
        public void onGetControl(OnGetControlArgs e) {
            super.onGetControl(e);
            String key = e.getKey();
            if ("material".equals(key)) {
                BasedataEdit bdMatEdit = new BasedataEdit();
                bdMatEdit.setKey("material");
                bdMatEdit.setEntryKey(KEY_ENTRY_ENTITY);
                bdMatEdit.setView(getView());
                e.setControl(bdMatEdit);
            } else  if ("pk_entryentity".equals(key)){
                EntryGrid entryGrid = new EntryGrid();
                entryGrid.setView(getView());
                entryGrid.setKey(key);
                entryGrid.setEntryKey(key);
    
                EntryGrid oGrid = (EntryGrid) e.getControl();
                if (oGrid == null) {
                    oGrid = (EntryGrid) findControl(KEY_ENTRY_ENTITY);
                }
                BasedataEdit bdMatEdit = new BasedataEdit();
                bdMatEdit.setKey("material");
                bdMatEdit.setEntryKey(KEY_ENTRY_ENTITY);
                bdMatEdit.setView(getView());
                oGrid.getItems().add(bdMatEdit);
    
                entryGrid.getItems().addAll(oGrid.getItems());
                e.setControl(entryGrid);
            }
        }
        /**
         * 动态创建动态列模型
         * @return
         */
        private EntryAp createEntryAp() {
            EntryAp entryAp = new EntryAp();
            entryAp.setKey("entryap");
            entryAp.setShowSeq(true);
            //配码
            EntryFieldAp apMat = new EntryFieldAp();
            apMat.setId("material");
            apMat.setKey("material");
            apMat.setWidth(new LocaleString("20px"));
    
            apMat.setName(new LocaleString("物料"));
            BasedataField bdMat = new BasedataField();
            bdMat.setBaseEntityNumber("bd_material");
            apMat.setField(bdMat);
            entryAp.getItems().add(apMat);
            return entryAp;
    
        }
    
        @Override
        public void getEntityType(GetEntityTypeEventArgs e) {
            // 取原始的主实体
            MainEntityType oldMainType = e.getOriginalEntityType();
            MainEntityType newMainType = null;
            try {
                newMainType = (MainEntityType) oldMainType.clone();
            } catch (CloneNotSupportedException cloneNotSupportedException) {
                cloneNotSupportedException.printStackTrace();
            }
            registerEntityType(newMainType);
            e.setNewEntityType(newMainType);
        }
    
        private void registerEntityType(MainEntityType newMainType) {
            //单据体实体
            EntryType entryType = (EntryType) newMainType.getAllEntities().get(KEY_ENTRY_ENTITY);
            //配码
            BasedataProp bdMatProp = new BasedataProp();
            bdMatProp.setDbIgnore(true);
            bdMatProp.setBaseEntityId("bd_material");
            bdMatProp.setName("material");
            bdMatProp.setComplexType(EntityMetadataCache.getDataEntityType("bd_material"));
    
            DynamicSimpleProperty pMatRefId = bdMatProp.createRefIDProp();
            pMatRefId.setPrimaryKey(false);
            pMatRefId.setName("material_id");
            pMatRefId.setDbIgnore(true);
            bdMatProp.setRefIdProp(pMatRefId);
            bdMatProp.setRefIdPropName("material_id");
    
            entryType.registerComplexProperty(bdMatProp);
            entryType.addProperty(pMatRefId);
        }
    
        @Override
        public void createNewData(BizDataEventArgs e) {
            DynamicObject dynamicObject = new DynamicObject(this.getModel().getDataEntityType());
            DynamicObjectCollection entryCol = dynamicObject.getDynamicObjectCollection(KEY_ENTRY_ENTITY);
            e.setDataEntity(dynamicObject);
        }
    
        @Override
        public void afterBindData(EventObject e) {
            EntryGrid entryGrid = this.getControl(KEY_ENTRY_ENTITY);
            entryGrid.bindData(new BindingContext(this.getModel().getDataEntity(true)));
        }
    
        @Override
        public void afterCreateNewData(EventObject e) {
            e.getSource();
            //加载传递过来的数据
        }
    
        private Control findControl(String key) {
            FormRoot root= (FormRoot) getView().getRootControl();
            return findControl(key,root.getItems());
        }
    
        private Control findControl(String key, List<Control> items) {
            for (Control ctl : items) {
                if (PKPublicUtils.equals(key,ctl.getKey())) {
                    return ctl;
                } else if (ctl instanceof Container) {
                    Control subCtl = findControl(key,((Container)ctl).getItems());
                    if (subCtl != null) return subCtl;
                }
            }
            return null;
        }
    
        @Override
        public void beforeF7Select(BeforeF7SelectEvent beforeF7SelectEvent) {
            //F7基础资料做数据隔离
        }
    
    }
图标赞 3
3人点赞
还没有人点赞,快来当第一个点赞的人吧!
图标打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!