本案例是附件上传时记录新上传的附件修改文件名
附件上传时记录新上传的附件可以在data的onValueChange事件操作
具体实现如下:
1.添加引用
var DocUtils = require('$UI/system/components/justep/docCommon/docUtil');
2.在model的勾选函数中定义3个实例变量
var Model = function() {
this.callParent();
this.docID = [];
this.changeRows = [];
this.docName = [];
};
3.在data的onValueChange事件中记录改变的行和新增加的附件信息
Model.prototype.mainDataValueChange = function(event) {
//判断是附件组件关联的关系的值发生改变
if (event.col == 'fFJ') {
var newValue = event.newValue;
var oldValue = event.oldValue;
var jsonList = eval("(" + newValue + ")");
if(jsonList && jsonList.length>0){
for (var i = 0; i < jsonList.length; i++) {
var docID = jsonList[i].docID;
//如果新增中的docID在旧值中不存在说明附件是新上传的
if (!oldValue || oldValue.toString().indexOf(docID) < 0) {
this.docID.push(jsonList[i].docID);//把docID放到实例变量中this.docID数组中
var row = event.row;
//判断实例变量this.changeRows数组中不存在当前改变行时,把当前行放到this.changeRows实例变量数组中
if ($.inArray(row, this.changeRows) < 0) {
this.changeRows.push(row)
}
}
}
}
}
};
4.在data的onBeforeSave事件中修改附件组件json格式中的docName的值
Model.prototype.mainDataBeforeSave = function(event) {
for (var i = 0; i < this.changeRows.length; i++) {
//从实例变量中this.changeRows中获取改变的行中的附件组件关联的关系值
var fAttachmentPicture = this.changeRows[i].val("fFJ");
var jsonList = eval("(" + fAttachmentPicture + ")");
if (jsonList.length > 0) {
for (var j = 0; j < jsonList.length; j++) {
var docID = jsonList[j].docID;
//判断docID在实例变量this.docID中修改其对应docName的值
var index = $.inArray(docID, this.docID);
if (index >= 0) {
var docName = jsonList[j].docName
jsonList[j].docName = docName.substring(0, docName.lastIndexOf(".") * 1) + "Modify" + docName.substring(docName.lastIndexOf("."));//给docName赋新值
this.docName.push(jsonList[j].docName);//把修改后的docName的值放到实例变量this.docName数组中
}
event.source.setValue("fFJ", JSON.stringify(jsonList), this.changeRows[i]);//把修改后的json格式数据给关系赋值
}
}
}
};
5.在data的onAfterSave中修改SA_DocNode中docName的值并通知文档服务器
Model.prototype.mainDataAfterSave = function(event) {
var self = this;
setTimeout(function() {
if (self.docID.length > 0 && self.docName.length > 0) {
for (var i = 0; i < self.docID.length; i++) {
//修改SA_DocNode中的文件名以及通知文档服务器
DocUtils.Utils.changeDocName(self.docID[i], self.docName[i], self.getContext());
DocUtils.InnerUtils.syncCustomFileds(self.docID[i], self.getContext());
}
}
//清空实例变量的值
self.docID.splice(0, self.docID.length);
self.docName.splice(0, self.docName.length);
self.changeRows.splice(0, self.changeRows.length);
}, 500);
};
评一波