单据体动态添加列原创
金蝶云社区-CM9527
CM9527
8人赞赏了该文章 4,327次浏览 未经作者许可,禁止转载编辑于2023年02月20日 09:37:22

    首先在设计器添加一个空白的单据体,单据体标识为entryentity。

image.png

动态添加列主要涉及到以下几个事件

image.png

@Override
    public void beforeBindData(EventObject e) {
        super.beforeBindData(e);
        FormShowParameter formShowParameter = this.getView().getFormShowParameter();
        Map<String, Object> value = formShowParameter.getCustomParams();
        if (value.containsKey("pageid")) {
            String pageid = value.get("pageid").toString();
            EntryAp entryAp = this.createEntryAp(pageid);
            IFormView formView = this.getView();
            EntryGrid entryGrid = formView.getControl(KEY_ENTRYENTITY);
            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<>();
        FormShowParameter formShowParameter = getFowmShowParameter(e);
        String pageid = formShowParameter.getCustomParam("pageid");
        EntryAp entryAp = this.createEntryAp(pageid);
        mapEntry.put(ClientProperties.Id, KEY_ENTRYENTITY);
        mapEntry.put(ClientProperties.Columns, entryAp.createControl().get(ClientProperties.Columns));
        e.getItems().add(mapEntry);
    }
    
    @Override
    public void createNewData(BizDataEventArgs e) {
        super.createNewData(e);
        DynamicObject dynamicObject = new DynamicObject(this.getModel().getDataEntityType());
        DynamicObjectCollection entryCol = dynamicObject.getDynamicObjectCollection(KEY_ENTRYENTITY);
        e.setDataEntity(dynamicObject);
    }
    
    @Override
    public void createNewData(BizDataEventArgs e) {
        super.createNewData(e);
        DynamicObject dynamicObject = new DynamicObject(this.getModel().getDataEntityType());
        DynamicObjectCollection entryCol = dynamicObject.getDynamicObjectCollection(KEY_ENTRYENTITY);
        e.setDataEntity(dynamicObject);
    }
    
    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        EntryGrid entryGrid = this.getControl(KEY_ENTRYENTITY);
        entryGrid.bindData(new BindingContext(this.getModel().getDataEntity(true)));
    }
    
    @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);
    }
    
    @Override
    public void onGetControl(OnGetControlArgs e) {
        String key = e.getKey();
        FormShowParameter formShowParameter = getFowmShowParameter(e);
        String pageid = formShowParameter.getCustomParam("pageid");
        List<FillDimension> dimensionList = getFillDimensionList(pageid);
        Optional<FillDimension> first = dimensionList.stream().filter((x) -> {
            return x.getFormId().equals(e.getKey());
        }).findFirst();
        if (first.isPresent()) {
            FillDimension element = (FillDimension) first.get();
            BasedataEdit bdMatEdit = new BasedataEdit();
            bdMatEdit.setKey(element.getFormId());
            bdMatEdit.setEntryKey(KEY_ENTRYENTITY);
            bdMatEdit.setView(getView());
            e.setControl(bdMatEdit);
        } else if (KEY_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_ENTRYENTITY);
            }
            for (FillDimension fillDimension : dimensionList) {
                BasedataEdit bdMatEdit = new BasedataEdit();
                bdMatEdit.setKey(fillDimension.getFormId());
                bdMatEdit.setEntryKey(KEY_ENTRYENTITY);
                bdMatEdit.setView(getView());
                oGrid.getItems().add(bdMatEdit);
            }
            entryGrid.getItems().addAll(oGrid.getItems());
            e.setControl(entryGrid);
        }
    }
    
    /**
     * 动态创建动态列模型
     *
     * @return
     */
    private EntryAp createEntryAp(String pageid) {
        EntryAp entryAp = new EntryAp();
        entryAp.setKey("entryap");
        entryAp.setShowSeq(true);
        List<FillDimension> dimensionList = getFillDimensionList(pageid);
        if (dimensionList.size() > 0) {
            for (FillDimension fillDimension : dimensionList) {
                EntryFieldAp apMat = new EntryFieldAp();
                apMat.setId(fillDimension.getFormId());
                apMat.setKey(fillDimension.getFormId());
                apMat.setWidth(new LocaleString("100px"));
                apMat.setName(new LocaleString(fillDimension.getName()));
                BasedataField bdMat = new BasedataField();
                bdMat.setBaseEntityNumber(fillDimension.getFormId());
                apMat.setField(bdMat);
                entryAp.getItems().add(apMat);
            }
        }
        return entryAp;
    }
    
    private void registerEntityType(MainEntityType newMainType) {
        //单据体实体
        EntryType entryType = (EntryType) newMainType.getAllEntities().get(KEY_ENTRYENTITY);
        //配码
        FormShowParameter formShowParameter = this.getView().getFormShowParameter();
        Map<String, Object> value = formShowParameter.getCustomParams();
        if (value.containsKey("pageid")) {
            String pageid = value.get("pageid").toString();
            List<FillDimension> dimensionList = getFillDimensionList(pageid);
            if (dimensionList.size() > 0) {
                for (FillDimension dimension : dimensionList) {
                    BasedataProp bdMatProp = new BasedataProp();
                    bdMatProp.setDbIgnore(true);
                    bdMatProp.setBaseEntityId(dimension.getFormId());
                    bdMatProp.setName(dimension.getFormId());
                    bdMatProp.setComplexType(EntityMetadataCache.getDataEntityType(dimension.getFormId()));
                    bdMatProp.setFeatures(FeatureOption.MultiFillable.getValue());

                    DynamicSimpleProperty pMatRefId = bdMatProp.createRefIDProp();
                    pMatRefId.setPrimaryKey(false);
                    pMatRefId.setName(dimension.getFormId() + "_id");
                    pMatRefId.setDbIgnore(true);
                    bdMatProp.setRefIdProp(pMatRefId);
                    bdMatProp.setRefIdPropName(dimension.getFormId() + "_id");

                    entryType.registerComplexProperty(bdMatProp);
                    entryType.addProperty(pMatRefId);
                }
            }
        }
    }
    
    @Override
    public void afterCreateNewData(EventObject e) {
        e.getSource();
        //加载传递过来的数据
        bindDataFromDB();
    }

image.png

工具栏的增行,插行,删行,批量填充都是用的平台的操作



图标赞 8
8人点赞
还没有人点赞,快来当第一个点赞的人吧!
图标打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!