实战例子一:
(原始帖子参考:http://club.kisdee.com/home.php?mod=space&uid=421257&do=blog&id=118335)
所有存在条码扫描的字段的同学请注意,参考修改。
记得条码应用的要用这个,不要自己focus
源码:
from Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel import *
def ButtonClick(e):
if ( e.Key == "FENTER" ):
this.View.GetControl("FBASE").SetEnterNavLock(True)
if ( e.Key == "FTAB" ):
this.View.GetControl("FBASE").SetTabNavLock(True)
if ( e.Key == "FNULL" ):
this.View.GetControl("FBASE").SetEnterNavLock(False)
this.View.GetControl("FBASE").SetTabNavLock(False)
复制代码
(强烈不建议在单据体里面直接扫描录入)
理由是:由于单据体与服务端可能存在复杂交互逻辑,很多二开插件会依赖单据体的行改变事件和单元格值改变事件处理业务逻辑,并刷新界面表格数据,尤其是在B/S架构中,网络的延迟导致数据的到达客户端的时间无法预测,如果这是在用来进行高速扫描枪数据扫描,就会存在服务端回发数据与扫描枪之间对同一界面元素的争用与覆盖(通俗讲就是打架);依据界面只展示最后数据,并会覆盖前面数据的原理,将导致数据的不确定性。所以必须严格禁止在表格中直接扫描。如果您能接受这个数据的不确定性带来的数据错误的风险,您可以用,但我们强烈不建议。最正常的使用方法是上面介绍的。
实战例子二: (以下例子基于K/3 Cloud 2016年3月补丁,包括5.0,6.0版本)
两种方案选择:
A: 使用普通文本字段实现扫描条码;
步骤:
1,BOSIDE中拖放文本字段控件FBARCODE,并设置即时触发更新属性为勾选;
2,编写业务插件代码如下代码中类TestBarcodeScanerPlugIn;
特别说明:特别针对单行文本录入字段,当SetEnterNavLock(True)执行后,单行文本录入框进入条码扫描模式:
#设置True后:
1、焦点不跳转;
2、服务端回填扫描框的数据不显示,实现快速连续扫描(2016年3月补丁,包括5.0,6.0);
3、鼠标焦点时离开不会自动提交当前扫描框的数据,只有回车到来才会提交,实现快速连续扫描断码补填(2016最新补丁);
B:使用新的专用扫描控件实现扫描条码:(适用于超级快速的扫描)
1,BOSIDE中拖放文本字段控件FSPECSCANTEXT,并设置即时触发更新属性为勾选;.
2,编写构造插件代码如下代码中类TestBarcodeScanerCreatePlugIn,修改编辑控件xtype为kdscantext;
3,编写业务插件代码如下代码中类TestBarcodeScanerPlugIn;
插件代码:
using System;
using System.Data;
using Kingdee.BOS.Util;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Resource;
using Kingdee.BOS.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm;
using System.Collections.Generic;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using System.IO;
using Kingdee.BOS.Orm.DataEntity;
namespace Kingdee.BOS.Test.PlugIn
{
/// <summary>
/// 测试扫描专用控件构造
/// </summary>
public class TestBarcodeScanerCreatePlugIn : AbstractDynamicWebFormBuilderPlugIn
{
public override void CreateControl(CreateControlEventArgs e)
{
base.CreateControl(e);
if (e.ControlAppearance.Key.EqualsIgnoreCase("FSPECSCANTEXT"))
{
var editor = e.Control["item"] as JSONObject;
if (editor != null)
editor["xtype"] = "kdscantext";
}
}
}
/// <summary>
/// 测试条形码扫描枪插件
/// </summary>
public class TestBarcodeScanerPlugIn : AbstractDynamicFormPlugIn
{
Dictionary<string, bool> verifyCodes = new Dictionary<string, bool>();
public override void AfterBindData(EventArgs e)
{
base.AfterBindData(e);
if (verifyCodes.Count == 0)
{
verifyCodes["1901232892136"] = true;
verifyCodes["227250948129"] = true;
verifyCodes["227250948128"] = true;
}
this.View.GetControl("FBARCODE").SetEnterNavLock(true);
}
public override void ButtonClick(ButtonClickEventArgs e)
{
if (e.Key == "FVERIFY")
{
}
if (e.Key == "FENTER")
{
this.View.GetControl("FBARCODE").SetEnterNavLock(true);
}
if (e.Key == "FTAB")
{
this.View.GetControl("FBARCODE").SetTabNavLock(true);
}
if (e.Key == "FNULL")
{
this.View.GetControl("FBARCODE").SetEnterNavLock(false);
this.View.GetControl("FBARCODE").SetTabNavLock(false);
}
}
public override void BeforeUpdateValue(BeforeUpdateValueEventArgs e)
{
if (!(e.Key.Equals("FBARCODE") || e.Key.Equals("FSPECSCANTEXT")))
{
return;
}
var code = e.Value != null ? e.Value.ToString() : "[null]";
e.Cancel = true;
if (string.IsNullOrWhiteSpace(code))
return;
// 添加新行
var msg = "Error"; var verify = false;
if (!verifyCodes.TryGetValue(code, out verify))
{
verify = false;
}
if (verify)
{
msg = "OK";
}
var isAppend = (bool)this.Model.GetValue("F_KD_Append");
if (isAppend)
{
AppendRow(code, msg);
}
else
{
NewRowUpdateView(code, msg);
}
}
private void AppendRow(string code, string msg)
{
var ekey = "FENTITY";
this.Model.CreateNewEntryRow(ekey);
int rowIndex = this.Model.GetEntryCurrentRowIndex(ekey);
this.Model.SetValue("F_KD_BARCODE", code, rowIndex);
this.Model.SetValue("F_KD_REMARK", msg, rowIndex);
var info = this.View.OpenParameter.FormMetaData.BusinessInfo;
var entity = info.GetEntity(ekey);
var rowData = this.Model.GetEntityDataObject(entity, rowIndex);
var field = info.GetField("F_KD_REMARK");
this.View.StyleManager.SetEnabled(field.Key, rowData, "BillStatusByEntry", false);
var redColor = "#FF0000";
var whiteColor = "#FFFFFF";
var blackColor = "#000000";
var greenColor = "#00FFCC";
var backColor = greenColor;
var foreColor = whiteColor;
var grid = this.View.GetControl<EntryGrid>(ekey);
if (msg != "OK")
{
backColor = redColor;
}
else
{
foreColor = blackColor;
}
grid.SetForecolor("F_KD_REMARK", foreColor, rowIndex);
grid.SetBackcolor("F_KD_REMARK", backColor, rowIndex);
}
private void NewRowUpdateView(string code, string msg)
{
var ekey = "FENTITY";
var info = this.View.OpenParameter.FormMetaData.BusinessInfo;
var ent = info.GetEntity(ekey);
var entModel = this.Model.GetEntityDataObject(ent);
var barCodeField = info.GetField("F_KD_BARCODE");
var remarkField = info.GetField("F_KD_REMARK");
var rowData = new DynamicObject(ent.DynamicObjectType);
barCodeField.DynamicProperty.SetValue(rowData, code);
remarkField.DynamicProperty.SetValue(rowData, msg);
entModel.Add(rowData);
this.View.UpdateView(ekey);
var lastIdx = this.Model.GetRowIndex(ent, rowData);
this.View.GetControl<EntryGrid>(ekey).SetFocusRowIndex(lastIdx);
this.View.GetControl<EntryGrid>(ekey).SelectRows(new int[] { lastIdx });
}
}
}
复制代码
推荐阅读