公布k/3cloud表格控件块粘贴代码逻辑,供客户参考使用
金蝶云社区-星星火
星星火
0人赞赏了该文章 2,777次浏览 未经作者许可,禁止转载编辑于2014年08月08日 11:27:23

近来收到不少关于块粘贴需求反馈,有些问题BOS不太好处理,例如用户希望枚举全部按名称检索赋值,这个和BOS这边的通用逻辑有冲突。因此将块粘贴代码逻辑贴出来给到大家,大家可以在表单插件EntityBlockPasting事件中自己处理,然后将cancel设置为true。以下代码用户可以参考一下,插件代码中需要将其中一些属性或方法修改,例如this.BusinessInfo替换为this.View.BusinessInfo,UpdateValue替换为this.View.Model.SetValue,this.StyleManager替换为((IDynamicFormView)this.View).StyleManager
[code]public void EntityBlockPasting(string startKey, int startRow, string blockValue)
{
if (!blockValue.IsNullOrEmptyOrWhiteSpace())
{
EntityBlockPastingEventArgs args = new EntityBlockPastingEventArgs(startKey, startRow, blockValue);
this.EventsProxy.FireEntityBlockPasting(args);
if (args.Cancel)
{
return;
}

Field fd = this.BusinessInfo.GetField(startKey);
if (fd == null) return;
int entityRowCount = this.Model.GetEntryRowCount(fd.EntityKey);
string[] rowValues = blockValue.Split('\n');
int blockRowCount = rowValues.Length - 2;
if (blockRowCount > entityRowCount - startRow)
{
// 当前实体行数不足,补齐行数据
for (int i = entityRowCount - startRow; i < blockRowCount; i++)
{
this.Model.CreateNewEntryRow(fd.EntityKey);
}
}
Dictionary fieldKeyList = new Dictionary();
if (rowValues.Length > 0 && !string.IsNullOrEmpty(Convert.ToString(rowValues[0])))
{
SetFieldKeyList(startKey, rowValues[0].ToString(), fd, fieldKeyList);
}
int row = startRow;
for (int i = 1; i < rowValues.Length; i++)
{
string strValue = Convert.ToString(rowValues[i]);
if (strValue.IsNullOrEmpty())
{
// 最后空行结束
return;
}
string[] strs = strValue.Split('\t');
int fieldCount = strs.Length;
if (fieldKeyList.Count == 0)
{
// 可能找不到列,则返回
return;
}
int index = 0;
foreach (string str in strs)
{
//判断复制的数据列数是否大于列表可粘贴的行数
if (index <= fieldKeyList.Count - 1)
{
string key = fieldKeyList[index];
// 未锁定状态下更新数据,锁定跳过
if (this.StyleManager.GetEnabled(this.LayoutInfo.GetFieldAppearance(key)))
{
this.UpdateValue(fieldKeyList[index], row, str);
}
index++;
}
}
row++;
}
}
}

private void SetFieldKeyList(string startKey, string colNames, Field fd, Dictionary fieldKeyList)
{
if (fieldKeyList.Count > 0)
{
return;
}
int start = 0;
//从复制字段开始截取表头字符串,避免不必要的循环
string[] fieldNames = colNames.Substring(colNames.IndexOf(startKey) - 1).Split(',');
foreach (string fieldName in fieldNames)
{
if (startKey.EqualsIgnoreCase(fieldName))
{
fieldKeyList[start++] = fieldName;
continue;
}
if (start > 0)
{
fieldKeyList[start++] = fieldName;
continue;
}
}

}[/code]