二开案例.表单插件.表格动态创建列
【应用场景】表格动态创建列。
【案例演示】创建动态表单,通过插件方式,给表格动态创建列,动态绑定数据。
【实现步骤】
<1>编写表单插件,代码如下。
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Util;
using System;
using System.ComponentModel;
namespace Jac.XkDemo.BOS.Business.PlugIn
{
/// <summary>
/// 【表单插件】表格动态创建列
/// </summary>
[Description("【表单插件】表格动态创建列"), HotUpdate]
public class CreateDynamicListFormPlugIn : AbstractDynamicFormPlugIn
{
#region var
/// <summary>
/// 单据体标识
/// </summary>
private const string entityKey = "F_Jac_Entity";
/// <summary>
/// 需要动态创建的列的列名
/// </summary>
private string[] fieldNames = { "编码", "名称", "备注" };
#endregion
#region event
/// <summary>
/// 动态创建表格列并绑定数据
/// </summary>
/// <param name="e"></param>
public override void AfterBindData(EventArgs e)
{
base.AfterBindData(e);
BindData();
}
/// <summary>
/// 主菜单点击事件
/// </summary>
/// <param name="e"></param>
public override void BarItemClick(BarItemClickEventArgs e)
{
base.BarItemClick(e);
if (e.BarItemKey.EqualsIgnoreCase("tbRefresh"))
{
BindData();
}
}
/// <summary>
/// 动态创建表格列并绑定数据
/// </summary>
private void BindData()
{
var columnNames = this.View.Model.GetValue("F_Jac_Text") as string;
if (!string.IsNullOrWhiteSpace(columnNames))
{
fieldNames = columnNames.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
}
else
{
fieldNames = new[] { "编码", "名称", "备注" };
}
var actionData = new JSONObject();
actionData["key"] = entityKey;
actionData["methodname"] = "CreateDyanmicList";
JSONArray coMeta = new JSONArray();
//添加需要创建的列
coMeta.Add(GetColumns());
coMeta.Add("");
actionData["args"] = coMeta;
this.View.AddAction("InvokeControlMethod", actionData, x =>
{
//单据体动态创建列之后通过回调方式为该单据体添加数据
var grid = this.View.GetControl<EntryGrid>(entityKey);
grid.SetData(GetEntityData());
});
}
/// <summary>
/// 生成动态列指令集
/// </summary>
/// <returns></returns>
private JSONObject GetColumns()
{
// 生成构建表格的指令集
var entityAppearance = this.View.LayoutInfo.GetEntityAppearance(entityKey);
var gridMeta = entityAppearance.CreateControl();
gridMeta.Put("primaryKey", ExtConst.GRID_INDEX_FIELD_KEY);
gridMeta.Put("indexKey", ExtConst.GRID_INDEX_FIELD_KEY);
gridMeta.Put("startRow", 0);
gridMeta.Put("pageSize", entityAppearance.PageRows);
//gridMeta.Put("editable", false);
// 生成列
var columns = (JSONArray)gridMeta["columns"];
for (var i = 0; i < fieldNames.Length; ++i)
{
var fieldName = fieldNames[i];
var col = new JSONObject();
var xtype = "textfield";
col.Put("xtype", xtype);
col.Put("colIndex", i + 2);
col.Put("dataIndex", fieldName);
col.Put("header", fieldName);
col.Put("width", "300");
col.Put("visible", true);
// 如果需要某列的单元格可编辑,必须设置该列的编辑控件属性
// col.Put("editor", CreateEditorControl(xtype));
columns.Add(col);
}
return gridMeta;
}
/// <summary>
/// 把元素对应的编辑控件输出为Json对象
/// </summary>
/// <returns></returns>
private JSONObject CreateEditorControl(string xtype)
{
var ctlRet = new JSONObject();
ctlRet.Put("xtype", xtype);
ctlRet.Put("editable", true);
return ctlRet;
}
/// <summary>
/// 生成表格数据
/// </summary>
/// <returns></returns>
private JSONObject GetEntityData()
{
var data = new JSONObject();
var rows = new JSONArray();
for (var x = 1; x <= 20; ++x)
{
var row = new JSONArray();
row.Add(x);
row.Add(x);
foreach (var fieldName in fieldNames)
{
row.Add(string.Format("{0}:{1}", fieldName, Guid.NewGuid()));
}
rows.Add(row);
}
data.Put("rows", rows);
return data;
}
#endregion
}
}
<2>拷贝插件组件到应用站点的WebSite\Bin目录下,重启IIS。
<3>BOSIDE新建动态表单【表格动态创建列[Jac_CreateDynamicList]】(见文末附件),主要元素如下图所示,注册表单插件,保存元数据。
/*
-- 发布菜单
DELETE T_META_CONSOLEDETAIL WHERE FDetailFuncId='5fe3f66ecad206'
INSERT INTO T_META_CONSOLEDETAIL(FDETAILFUNCID,FSUBFUNCID,FNUMBER,FOBJECTID,FTYPE,FSTATUS,FPERMISSIONITEMID,FSEQ,FPARAM,FCUSTOMPARAMS,FTHUMB,FLEVEL,FVISIBLE,FFUNCTIONGROUP,FAUTHPMOBJECTTYPEID,FHTMLSTATUS,FEXCELSTATUS,FH5OBJECTID) VALUES ('5fe3f66ecad206','4983fb4e08554b8499dbfdee90a3c26d',N'BGDTCJL','Jac_CreateDynamicList','0','1',' ',6,N'{"FormId":"Jac_CreateDynamicList","ShowType":"1","formType":"bill"}',N' ',' ',null,29,0,' ','1','0',null)
DELETE T_META_CONSOLEDETAIL_L WHERE FDetailFuncId = '5fe3f66ecad206' AND FLOCALEID =2052
INSERT INTO T_META_CONSOLEDETAIL_L(FPKID,FDETAILFUNCID,FLOCALEID,FNAME,FDESCRIPTION) VALUES ('5fe3f66ecad207','5fe3f66ecad206',2052,N'表格动态创建列',N' ')
*/
---------------------------------------------------------------------------------------------------------
现在可以登录业务站点,打开菜单【表格动态创建列】,体验一下插件创建表格动态列的运行效果啦。
点刷新菜单后,表格数据会再次刷新。
---------------------------------------------------------------------------------------------------------
【参考资料】
【二开案例.表单插件.表格动态创建列(元数据版)】https://vip.kingdee.com/article/130367358388408320
---------------------------------------------------------------------------------------------------------
【金蝶云星空BOS二次开发案例演示】https://vip.kingdee.com/article/94751030918525696
二开案例.表单插件.表格动态创建列
本文2024-09-23 04:19:19发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-164562.html