二开案例.表单插件.动态创建下拉列表字段
【应用场景】使用插件,给表单动态的创建下拉列表字段。
【案例演示】新建动态表单,通过插件动态创建下拉列表字段。
【实现步骤】
<1>编写表单插件,代码如下。
using Kingdee.BOS;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.ControlElement;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using Kingdee.BOS.Web.DynamicForm;
using System;
using System.ComponentModel;
using System.Reflection;
namespace Jac.XkDemo.BOS.Business.PlugIn
{
/// <summary>
/// 【表单插件】动态创建下拉列表字段
/// </summary>
[Description("【表单插件】动态创建下拉列表字段"), HotUpdate]
public class CreateComboFieldFormPlugIn : AbstractDynamicFormPlugIn
{
#region event
/// <summary>
/// 动态构造表单元数据
/// </summary>
/// <param name="e"></param>
public override void OnSetLayoutInfo(SetLayoutInfoArgs e)
{
base.OnSetLayoutInfo(e);
// 创建元数据副本
var metadataCopy = (FormMetadata)ObjectUtils.CreateCopy(this.View.OpenParameter.FormMetaData);
// 动态创建BusinessInfo
var businessInfo = metadataCopy.BusinessInfo; //(BusinessInfo)ObjectUtils.CreateCopy(this.View.BusinessInfo);
// 动态创建LayoutInfo
var layoutInfo = metadataCopy.GetLayoutInfo(); //ObjectUtils.CreateCopy(this.View.LayoutInfo);
// 动态创建下拉列表字段
var fieldAp = MetadataUtils.CreateField<ComboField, ComboFieldAppearance>(this.Context, businessInfo, "FBillHead", "F_Jac_Combo_Country", "F_Jac_Combo_Country", "国家(动态)", 200, 10);
var field = (ComboField)fieldAp.Field;
field.MustInput = 1;
field.FireUpdateEvent = 1;
field.EnumType = "8a6e30f0-2c26-4639-aff5-76749daa355e";
var enumObject = new EnumObject(new DynamicObject(EnumObject.EnumObjectType));
enumObject.Id = ((ComboField)fieldAp.Field).EnumType;
var enumItem = new EnumItem();
enumItem.EnumId = "China";
enumItem.Caption = new LocaleValue("中国");
enumItem.Value = Convert.ToString("1");
enumItem.Seq = 1;
enumObject.Items.Add(enumItem);
enumItem = new EnumItem();
enumItem.EnumId = "America";
enumItem.Caption = new LocaleValue("美国");
enumItem.Value = Convert.ToString("2");
enumItem.Seq = 2;
enumObject.Items.Add(enumItem);
field.EnumObject = enumObject;
// 将动态字段注入业务元数据
businessInfo.Add(fieldAp.Field);
// 将动态字段注入外观元数据
layoutInfo.Add(fieldAp);
// 借助容器控件输出动态字段
var parentPanel = this.View.GetControl<Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel.Container>("F_Jac_Panel");
var layout = new LayoutInfo();
layout.Add(fieldAp);
parentPanel.AddControls(layout);
// 重建元数据的动态类型
businessInfo.GetDynamicObjectType(true);
// 使用动态构建的元数据替换现有元数据
MetadataUtils.SetProperty(this.View, "BusinessInfo", businessInfo);
MetadataUtils.SetProperty(this.View, "BillBusinessInfo", businessInfo);
MetadataUtils.SetProperty(this.View, "LayoutInfo", layoutInfo);
MetadataUtils.SetProperty(this.View, "BillLayoutInfo", layoutInfo);
}
public override void BarItemClick(BarItemClickEventArgs e)
{
base.BarItemClick(e);
if (e.BarItemKey.EqualsIgnoreCase("tbGetValue"))
{
var val1 = (string)this.Model.GetValue("F_Jac_Combo");
var val2 = (string)this.Model.GetValue("F_Jac_Combo_Country");
var msg = string.Format("发货状态:{0};国家:{1}", val1, val2);
this.View.ShowMessage(msg);
return;
}
}
#endregion
#region 元数据操作辅助类
/// <summary>
/// 元数据操作辅助类
/// </summary>
public static class MetadataUtils
{
/// <summary>
/// 创建字段
/// </summary>
/// <typeparam name="TField"></typeparam>
/// <typeparam name="TFieldAppearance"></typeparam>
/// <param name="ctx"></param>
/// <param name="businessInfo"></param>
/// <param name="entityKey"></param>
/// <param name="caption"></param>
/// <param name="propName"></param>
/// <param name="fieldName"></param>
/// <param name="top"></param>
/// <param name="left"></param>
/// <returns></returns>
public static TFieldAppearance CreateField<TField, TFieldAppearance>(Context ctx, BusinessInfo businessInfo, string entityKey, string fieldName, string propName, string caption, int top = 0, int left = 0)
where TFieldAppearance : FieldAppearance, new()
where TField : Field, new()
{
// 创建字段
var field = new TField();
field.Id = Guid.NewGuid().ToString().Replace("-", "");
field.Key = fieldName;
field.EntityKey = entityKey;
field.Entity = businessInfo.GetEntity(entityKey);
field.Name = new LocaleValue(caption, ctx.UserLocale.LCID);
field.FieldName = fieldName;
field.PropertyName = string.IsNullOrWhiteSpace(propName) ? fieldName : propName;
field.FireUpdateEvent = 0;
// 创建字段外观
var fieldAppearance = new TFieldAppearance();
fieldAppearance.Field = field;
fieldAppearance.Id = Guid.NewGuid().ToString().Replace("-", "");
fieldAppearance.Key = fieldName;
fieldAppearance.EntityKey = entityKey;
fieldAppearance.Caption = field.Name;
fieldAppearance.VisibleExt = "11111111";
fieldAppearance.LabelWidth = new LocaleValue("100", ctx.UserLocale.LCID);
fieldAppearance.Width = new LocaleValue("300", ctx.UserLocale.LCID);
if (top > 0)
{
fieldAppearance.Top = new LocaleValue(top.ToString(), ctx.UserLocale.LCID);
}
if (left > 0)
{
fieldAppearance.Left = new LocaleValue(left.ToString(), ctx.UserLocale.LCID);
}
return fieldAppearance;
}
/// <summary>
/// 给属性赋值
/// </summary>
/// <param name="obj"></param>
/// <param name="name"></param>
/// <param name="value"></param>
public static void SetProperty(object obj, string name, object value)
{
if (obj == null)
{
return;
}
var property = obj.GetType().GetProperty(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (property == null)
{
throw new Exception(string.Format("类型{0}不存在属性{1}", obj.GetType().FullName, name));
}
property.SetValue(obj, value, null);
}
}
#endregion
}
}
<2>拷贝插件组件到应用站点的WebSite\Bin目录下,重启IIS。
<3>BOSIDE新建动态表单[Jac_DTCJZD],添加一个下拉列表字段,用于和动态构建的下拉列表字段对比测试,添加一个面板,用于容纳动态构建的下拉列表字段,注册表单插件,保存元数据,发布该动态表单到主控台,开发完毕。
现在可以登录业务站点,打开动态表单【动态创建字段】,此时动态创建的下拉列表字段也显示出来啦,我们可以拿它和BOSIDE设计的原生下拉列表字段操作对比测试一下,他们的最终运行效果是一样的喔~~
---------------------------------------------------------------------------------------------------------
【注意事项】
<1>动态创建字段通常意味着要修改元数据。
<2>切记:不要修改原生的那份元数据!!!
整个运行时就一份,你改了,其它地方也都会被改,会造成一系列莫名其妙的疑难杂症。
哪些算是原生的元数据?
(1)通过平台公共属性获取的元数据:
this.View.BusinessInfo
this.View.BillBusinessInfo
this.View.LayoutInfo
this.View.OpenParameter.FormMetaData
(2)通过平台接口获取的元数据
MetaDataServiceHelper.Load
FormMetaDataCache.GetCachedFormMetaData
<3>在原生元数据的副本上可以随意修改。
---------------------------------------------------------------------------------------------------------
【金蝶云星空BOS二次开发案例演示】https://vip.kingdee.com/article/94751030918525696
二开案例.表单插件.动态创建下拉列表字段
本文2024-09-23 04:20:04发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-164636.html