二开案例.单据插件.公式编辑器
【应用场景】
通过界面鼠标操作完成计算公式的编辑。
【案例演示】
采购订单,编辑界面,通过公式编辑器生成自定义公式。
【实现步骤】
<1>编写单据插件,代码如下。
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.Formula;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
namespace Jac.XkDemo.BOS.Business.PlugIn
{
/// <summary>
/// 【单据插件】公式编辑器
/// </summary>
[Description("【单据插件】公式编辑器"), HotUpdate]
public class ExpressionEditorBillPlugIn : AbstractBillPlugIn
{
public override void BarItemClick(BarItemClickEventArgs e)
{
base.BarItemClick(e);
if (e.BarItemKey.EqualsIgnoreCase("tbGetExp"))
{
// 读取公式信息
var expValue = (string)this.View.Model.GetValue("F_Jac_Expression");
if (!expValue.IsNullOrEmpty())
{
var expText = FormulaUtils.GetFormulaText(expValue, FormulaUtils.GetProvider(string.Empty));
var expDisplayText = FormulaUtils.GetFormulaDisplayText(expValue, FormulaUtils.GetProvider(string.Empty));
this.View.ShowMessage(string.Format("公式:{0}\r\n公式描述:{1}", expText, expDisplayText));
}
}
}
public override void AfterBindData(EventArgs e)
{
base.AfterBindData(e);
BindTree();
}
/// <summary>
/// 公式编辑器初始化左边的树
/// </summary>
private void BindTree()
{
var expressionEditor = this.View.GetControl<ExpressionEditor>("F_Jac_Expression");
// 公式编辑器绑定已有公式
var expValue = (string)this.View.Model.GetValue("F_Jac_Expression");
if (!expValue.IsNullOrEmpty())
{
expressionEditor.SetData(expValue);
}
else
{
expressionEditor.SetData("");
}
var formId = this.View.BillBusinessInfo.GetForm().Id;
var expConfigData = new ExpConfigData();
var nodes = new List<ExpPreItemNode>();
nodes.Add(GetNode(formId));
// 添加业务对象节点
expConfigData.itemstore = nodes;
//// 添加函数
//var lstFunctions = new List<ExpPreItemNode>();
//lstFunctions.Add(new ExpPreItemNode
//{
// id = "sum()",
// text = "求和",
// desc = "求和"
//});
//lstFunctions.Add(new ExpPreItemNode
//{
// id = "avg()",
// text = "计算平均值",
// desc = "计算平均值"
//});
//expConfigData.functionsstore = lstFunctions;
//// 添加操作符
//var lstOperators = new List<ExpPreItemNode>();
//lstOperators.Add(new ExpPreItemNode
//{
// id = "[",
// text = "左中括号",
// desc = "左中括号"
//});
//lstOperators.Add(new ExpPreItemNode
//{
// id = "]",
// text = "右中括号",
// desc = "右中括号"
//});
//expConfigData.operatorsstore = lstOperators;
//// 添加变量
//var lstVariables = new List<ExpPreItemNode>();
//lstVariables.Add(new ExpPreItemNode
//{
// id = "CurrentUser",
// text = "当前用户",
// desc = "当前用户"
//});
//lstVariables.Add(new ExpPreItemNode
//{
// id = "CurrentOrg",
// text = "当前组织",
// desc = "当前组织"
//});
//expConfigData.privatevarstore = lstVariables;
// 公式编辑器绑定左侧的节点树的数据源
expressionEditor.SetStore(expConfigData);
}
/// <summary>
/// 获取业务对象分层展开的节点树
/// </summary>
/// <param name="formId"></param>
/// <returns></returns>
private ExpPreItemNode GetNode(string formId)
{
var formMetadata = FormMetaDataCache.GetCachedFormMetaData(this.Context, formId);
// 根节点
var rootNode = new ExpPreItemNode
{
id = formId,
text = formMetadata.Name.ToString(),
desc = formMetadata.Name.ToString(),
cls = "Folder",
children = new List<ExpPreItemNode>()
};
// 业务对象的单据体
GetEntryInfo(formMetadata, rootNode.children);
return rootNode;
}
/// <summary>
/// 获取业务对象的单据体节点
/// </summary>
/// <param name="formMetadata"></param>
/// <param name="children"></param>
private void GetEntryInfo(FormMetadata formMetadata, List<ExpPreItemNode> children)
{
foreach (var entry in formMetadata.BusinessInfo.Entrys)
{
if (entry.EntityType == Entity.LINK_TYPE)
{
continue;
}
var entryNode = new ExpPreItemNode
{
id = entry.Key,
text = entry.Name.ToString(),
desc = entry.Name.ToString(),
cls = "Folder",
children = new List<ExpPreItemNode>()
};
// 单据体中的字段
GetFieldInfo(entry, entryNode.children);
children.Add(entryNode);
}
}
/// <summary>
/// 获取业务对象的指定单据体的字段节点
/// </summary>
/// <param name="entry"></param>
/// <param name="children"></param>
private void GetFieldInfo(Entity entry, List<ExpPreItemNode> children)
{
foreach (var field in entry.Fields)
{
if (field.Key == "FStockBaseAPJoinQty")
{
// 这句莫名其妙的代码是为了界面不卡死,请无视。
continue;
}
if (field is FlexGroupField || field is FlexValueField || field is BasePropertyField)
{
continue;
}
var name = field.Name.ToString();
var fieldNode = new ExpPreItemNode
{
id = field.Key,
text = name,
desc = string.Format("{0}.{1}", name, field.Key)
};
children.Add(fieldNode);
}
}
}
}
<2>拷贝插件组件到应用站点的WebSite\Bin目录下,重启IIS。
<3>BOSIDE扩展采购订单,添加公式编辑器页签,添加公式编辑器字段,添加菜单项,注册表单插件,保存元数据,开发完毕。
现在可以登录业务站点,打开采购订单编辑界面,检验一下插件效果啦。
【参考文献】
【公式编辑与计算应用】https://vip.kingdee.com/article/15621
标准产品:要补货方案[DRP_Plan] ,要补货公式编辑器[DRP_PlanFormula]
【金蝶云星空BOS二次开发案例演示】https://vip.kingdee.com/article/94751030918525696
二开案例.单据插件.公式编辑器
本文2024-09-23 04:18:27发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-164471.html