插件案例 —— 动态构建控件,让你前端显示异常灵活原创
金蝶云社区-MiLai
MiLai
14人赞赏了该文章 2,064次浏览 未经作者许可,禁止转载编辑于2020年08月07日 17:45:25

案例背景

有的时候,列具有极强动态性,当然比如通版默认支持的辅助资料,动态构建的字段添加到界面。列的列完全是一个动态效果,那么我们怎么实现一种简便的动态列呢?


案例设计

列的列是动态,那么我们应当要满足录入列的列动态构建及属性确认,比如他是什么一个什么基础资料,关联的ID是什么?他是一个文本?还是一个下拉框。


代码示例 —— 动态构建控件


public FieldAppearance CreateStandardField(DynamicObject field)
        {
        //列的所有的属性应当从对应的数据库取到,这样我们就有目的构建动态列
            string key = Convert.ToString(field["FENTRYID"]);
            string type = Convert.ToString(field["FFIELDTYPE"]);
            string Name = Convert.ToString(field["FFIELDNAME"]);
            string baseDataFormId = Convert.ToString(field["FBASEFORMID"]);
            string enumTypeId = Convert.ToString(field["FENUMTYPEID"]);
            FieldAppearance fieldA = null;
            //构建列标识,避免重复,可以采用类型,关联分录KEY
            string fieldKey = string.Format("F_{0}_{1}", type, key);
            var fieldName = new Kingdee.BOS.LocaleValue(Name);
            switch (type)
            {
                case "0":
                //文本
                    fieldA = new TextFieldAppearance();
                    TextField textField = new TextField();
                    textField.Key = fieldKey;
                    textField.Name = fieldName;
                    textField.PropertyName = textField.Key;
                    textField.FieldName = textField.Key;
                    textField.EntityKey = "FBillHead";
                    textField.Tabindex = 10;
                    textField.ListTabIndex = 10;
                    fieldA.Field = textField;
                    break;
                //复选框
                case "1":
                    fieldA = new CheckBoxFieldAppearance();
                    CheckBoxField cbField = new CheckBoxField();
                    cbField.Key = fieldKey;
                    cbField.Name = fieldName;
                    cbField.PropertyName = cbField.Key;
                    cbField.FieldName = cbField.Key;
                    cbField.EntityKey = "FBillHead";
                    cbField.Tabindex = 10;
                    cbField.ListTabIndex = 10;
                    fieldA.Field = cbField;
                    break;
                //基础资料
                case "2":
                    var metaData = MetaDataServiceHelper.Load(this.Context, baseDataFormId);
                    fieldA = new BaseDataFieldAppearance();
                    var baseDatafield = new BaseDataField(key);
                    baseDatafield.Key = fieldKey;
                    baseDatafield.Name = fieldName;
                    baseDatafield.PropertyName = baseDatafield.Key;
                    baseDatafield.FieldName = baseDatafield.Key;
                    baseDatafield.LookUpObjectID = GetObjectFid(this.Context, baseDataFormId);
                    baseDatafield.LookUpObject = MetaDataServiceHelper.GetLookupObjects(this.Context, new LookUpObjectFilter() { Filter = string.Format("FFORMID='{0}'", baseDataFormId) }).First();
                    baseDatafield.EntityKey = "FBillHead";
                    baseDatafield.Entity = currBusinessInfo.GetEntity(0);
                    baseDatafield.Tabindex = 10;
                    baseDatafield.ListTabIndex = 10;
                    baseDatafield.RefFormDynamicObjectType = ((FormMetadata)metaData).BusinessInfo.GetDynamicObjectType();
                    baseDatafield.NameProperty = new BaseDataFieldRefProperty("FName", "Name");
                    baseDatafield.NumberProperty = new BaseDataFieldRefProperty("FNumber", "Number");
                    fieldA.Field = baseDatafield;
                    fieldA.EntityKey = "FBillHead";
                    break;
                //数字
                case "3":
                    fieldA = new DecimalFieldAppearance();
                    fieldA.ShowSpinButton = true;
                    DecimalField decimalField = new DecimalField();
                    decimalField.Key = fieldKey;
                    decimalField.Name = fieldName;
                    decimalField.PropertyName = decimalField.Key;
                    decimalField.FieldName = decimalField.Key;
                    decimalField.EntityKey = "FBillHead";
                    decimalField.Tabindex = 10;
                    decimalField.ListTabIndex = 10;
                    fieldA.Field = decimalField;
                    break;
                //下拉框
                case "4":
                    fieldA = new ComboFieldAppearance();
                    EnumObject enumObject = new EnumObject(new DynamicObject(EnumObject.EnumObjectType));
                    enumObject.Id = enumTypeId;
                    var enumItem = this.GetEnumItem(this.Context, enumTypeId);
                    if (enumItem != null && enumItem.Count > 0)
                    {
                        foreach (var item in enumItem)
                        {
                            EnumItem eitem = new EnumItem();
                            eitem.Caption = new LocaleValue(Convert.ToString(item["FCAPTION"]));
                            eitem.EnumId = Convert.ToString(item["FENUMID"]);
                            eitem.Value = Convert.ToString(item["FVALUE"]);
                            enumObject.Items.Add(eitem);
                        }
                    }
                    //enumObject.Items
                    var comboField = new ComboField(key);
                    comboField.Key = fieldKey;
                    comboField.Name = fieldName;
                    comboField.PropertyName = comboField.Key;
                    comboField.FieldName = comboField.Key;
                    comboField.EnumObject = enumObject;
                    comboField.EnumType = enumTypeId;
                    comboField.EntityKey = "FBillHead";
                    comboField.Entity = currBusinessInfo.GetEntity(0);
                    comboField.Tabindex = 10;
                    comboField.ListTabIndex = 10;
                    fieldA.Field = comboField;
                    break;
                default:
                    break;
            }
            fieldA.Caption = fieldName;
            fieldA.Key = fieldKey;
            //字段构建完毕,返回给界面显示罗
            return fieldA;
        }


动态的动态构建很灵活,一段JSON,即可完成多样显示,这样是不方便使用辅助资料的时候的一个最佳设计方式。

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