Sfoglia il codice sorgente

重构子目换算表,适应粘贴时,可包含只读列,自动处理。

zhongzewei 5 anni fa
parent
commit
d9ccafe94b
1 ha cambiato i file con 99 aggiunte e 87 eliminazioni
  1. 99 87
      web/maintain/ration_repository/js/coe.js

+ 99 - 87
web/maintain/ration_repository/js/coe.js

@@ -1,7 +1,6 @@
 /**
  * Created by CSL on 2017-05-18.
  */
-//modiyied by zhong on 2017/9/21
 
 $(document).ready(function () {
     function refreshALlWorkBook() {
@@ -530,15 +529,18 @@ let gljAdjOprObj = {
     workSheet: null,
     currentGljAdjList: [],
     gljList: [],//只含编号和名称的总工料机列表
+
     setting: {
         header: [
             {headerName:"调整类型", headerWidth:80, dataCode:"coeType", dataType: "String", hAlign: "center", vAlign: "center", readOnly: false},
             {headerName:"人材机编码", headerWidth:80, dataCode:"gljCode", dataType: "String", formatter: '@', hAlign: "center", vAlign: "center", readOnly: false},
-            {headerName:"名称", headerWidth:100, dataCode:"gljName", dataType: "String", hAlign: "center", vAlign: "center", readOnly: true},
+            // readOnly: true --> false,需要兼容粘贴列包含只读项
+            {headerName:"名称", headerWidth:100, dataCode:"gljName", dataType: "String", hAlign: "center", vAlign: "center", readOnly: false},
             {headerName:"操作符", headerWidth:60, dataCode:"operator", dataType: "String", hAlign: "center", vAlign: "center", readOnly: false},
             {headerName:"数量", headerWidth:80, dataCode:"amount", dataType: "String", hAlign: "center", vAlign: "center" , readOnly: false},
             {headerName:"替换为编码", headerWidth:80, dataCode:"replaceCode", dataType: "String", formatter: '@', hAlign: "center", vAlign: "center", readOnly: false},
-            {headerName:"替换为名称", headerWidth:100, dataCode:"replaceName", dataType: "String", hAlign: "center", vAlign: "center", readOnly: true}
+            // readOnly: true --> false
+            {headerName:"替换为名称", headerWidth:100, dataCode:"replaceName", dataType: "String", hAlign: "center", vAlign: "center", readOnly: false}
         ],
         comboItems: {
             //调整类型下拉菜单
@@ -558,7 +560,6 @@ let gljAdjOprObj = {
         me.workSheet.bind(GC.Spread.Sheets.Events.EditStarting, me.onEditStart);
         me.workSheet.bind(GC.Spread.Sheets.Events.EditEnded, me.onEditEnded);
         me.workSheet.bind(GC.Spread.Sheets.Events.EnterCell, me.onEnterCell);
-        me.workSheet.bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
         me.workSheet.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
     },
     buildBaseCell: function (sheet) {
@@ -590,16 +591,37 @@ let gljAdjOprObj = {
     onEditStart: function (sender, args) {
         let me = gljAdjOprObj;
         let dataCode = me.setting.header[args.col].dataCode;
-        if(!coeOprObj.currentCoe || args.row >= me.currentGljAdjList.length  ){//超出编辑范围
-            if(dataCode=== 'gljCode' || dataCode=== 'replaceCode') args.cancel = true;
+        // 超出范围-没有选中的调整主项
+        if (!coeOprObj.currentCoe) {
+            args.cancel = true;
             return;
         }
-        if(dataCode=== 'gljCode' && me.currentGljAdjList[args.row].coeType !== '单个工料机'&& me.currentGljAdjList[args.row].coeType !== '替换人材机'){ //单个人才机和替换人材机,编码才能编辑
+        // 名称、替换为名称总是不可编辑(表头设置readOnly没有设置为true,因为需要粘贴)
+        let alwaysNotEditable = ['gljName', 'replaceName'];
+        if (alwaysNotEditable.includes(dataCode)) {
             args.cancel = true;
             return;
         }
-        if(dataCode=== 'replaceCode' && me.currentGljAdjList[args.row].coeType !== '替换人材机'){//替换人材机类型 替换编码才能编辑
+        // 新一行编辑时,除了总是可编辑的单元格:调整类型、操作符、数量,其他不可编辑
+        let alwaysEditable = ['coeType', 'operator', 'amount'];
+        if (args.row > me.currentGljAdjList.length - 1 &&
+            !alwaysEditable.includes(dataCode)) {
             args.cancel = true;
+            return;
+        }
+        let curSubCoe = me.currentGljAdjList[args.row];
+        if (!curSubCoe) {
+            return;
+        }
+        // 调整类型不为“单个工料机”和“替换人材机”时,人材机编码不可编辑
+        if (!['单个工料机', '替换人材机'].includes(curSubCoe.coeType) && dataCode === 'gljCode') {
+            args.cancel = true;
+            return;
+        }
+        // 调整类型不为“替换人材机”时,替换为编码不可编辑
+        if (curSubCoe.coeType !== '替换人材机' && dataCode === 'replaceCode') {
+            args.cancel = true;
+            return;
         }
     },
     onEditEnded: function (sender, args) {
@@ -667,85 +689,77 @@ let gljAdjOprObj = {
             }
         }
     },
-    onClipboardPasting: function (sender, info) {
-
-    },
-    getValidPasteDatas: function (pasteItems, info) {//2018-12-21  这里的if else 太多了,不好维护
+    getValidPasteData: function (pasteItems, info) {
         let me = gljAdjOprObj;
-        let rst = [];
-        for(let i = 0, len = pasteItems.length; i < len; i++){
-            let row = i + info.cellRange.row;
-            let validObj = {};
-            //update
-            if(row < me.currentGljAdjList.length){
-                let updateObj = me.currentGljAdjList[row];
-                validObj.index = row;//要有下标做为匹配的依据,不然在复制多行并且某个单元格是只读的情况下,这里返回的updateList个数会比选中的行数少,造成更新行和实际不匹配的情况
-                if(typeof pasteItems[i].coeType !== 'undefined' && typeof pasteItems[i].gljCode !== 'undefined'){
-                    let gljName = me.getGljName(pasteItems[i].gljCode, me.gljList);
-                    if((updateObj.coeType === '单个工料机'||updateObj.coeType === '替换人材机') && gljName){
-                        validObj.coeType = pasteItems[i].coeType;
-                        validObj.gljCode = pasteItems[i].gljCode;
-                        validObj.gljName = gljName;
-                    }
-                    else if((pasteItems[i].coeType !== '单个工料机'||pasteItems[i].coeType !== '替换人材机') && me.setting.comboItems.coeType.indexOf(pasteItems[i].coeType) !== -1){
-                        validObj.coeType = pasteItems[i].coeType;
-                    }
+        function isDef(v) {
+            return v !== undefined && v !== null;
+        }
+        // 粘贴项匹配处理
+        let rules = {
+            coeType: function (v, cur, tar) {
+                if (v === '') {
+                    tar.coeType = v;
+                    tar.gljCode = v;
+                    tar.gljName = v;
+                    tar.replaceCode = v;
+                    tar.replaceName = v;
+                } else if (me.setting.comboItems.coeType.includes(v)) {
+                    tar.coeType = v;
                 }
-                else if(typeof pasteItems[i].coeType === 'undefined' && typeof pasteItems[i].gljCode !== 'undefined'){
-                    let gljName = me.getGljName(pasteItems[i].gljCode, me.gljList);
-                    if(typeof updateObj.coeType !== 'undefined' && (updateObj.coeType === '单个工料机'||updateObj.coeType === '替换人材机') && gljName){
-                        validObj.gljCode = pasteItems[i].gljCode;
-                        validObj.gljName = gljName;
+            },
+            gljCode: function (v, cur, tar) {
+                if (v === '') {
+                    tar.gljCode = v;
+                    tar.gljName = v;
+                } else if (['单个工料机', '替换人材机'].includes(tar.coeType) ||
+                    (!isDef(tar.coeType) && isDef(cur) && ['单个工料机', '替换人材机'].includes(cur.coeType))) {
+                    let gljName = me.getGljName(v, me.gljList);
+                    if (gljName) {
+                        tar.gljCode = v;
+                        tar.gljName = gljName;
                     }
                 }
-                else if(typeof pasteItems[i].coeType !== 'undefined' && typeof pasteItems[i].gljCode === 'undefined'){
-                    if(me.setting.comboItems.coeType.indexOf(pasteItems[i].coeType) !== -1){
-                        validObj.coeType = pasteItems[i].coeType;
-                        if(validObj.coeType !== '单个工料机' && typeof updateObj.coeType !== '单个工料机' && updateObj.gljCode.toString().trim().length > 0){
-                            validObj.gljCode = '';
-                            validObj.gljName = '';
-                        }
-                    }
-                } else {
-                    if(typeof pasteItems[i].operator !== 'undefined' && me.setting.comboItems.operator.indexOf(pasteItems[i].operator) !== -1){
-                        validObj.operator = pasteItems[i].operator;
-                    }
-                    if(typeof pasteItems[i].amount !== 'undefined' && !isNaN(pasteItems[i].amount)){
-                        validObj.amount = pasteItems[i].amount;
-                    }
+            },
+            amount: function (v, cur, tar) {
+                if (!isNaN(v)) {
+                    tar.amount = v;
                 }
-                if(typeof pasteItems[i].replaceCode !== 'undefined' && updateObj.coeType === '替换人材机'){
-                    let gljName = me.getGljName(pasteItems[i].replaceCode, me.gljList);
-                    validObj.replaceCode = pasteItems[i].replaceCode;
-                    validObj.replaceName = gljName;
+            },
+            operator: function (v, cur, tar) {
+                if (v === '' || me.setting.comboItems.operator.includes(v)) {
+                    tar.operator = v;
                 }
-            }
-            else {
-                if(typeof pasteItems[i].coeType !== 'undefined' && typeof pasteItems[i].gljCode !== 'undefined'){
-                    let gljName = me.getGljName(pasteItems[i].gljCode, me.gljList);
-                    if((pasteItems[i].coeType === '单个工料机'||pasteItems[i].coeType === '替换人材机') && gljName){
-                        validObj.coeType = pasteItems[i].coeType;
-                        validObj.gljCode = pasteItems[i].gljCode;
-                        validObj.gljName = gljName;
-                    }
-                    else if((pasteItems[i].coeType !== '单个工料机'||pasteItems[i].coeType !== '替换人材机') && me.setting.comboItems.coeType.indexOf(pasteItems[i].coeType) !== -1){
-                        validObj.coeType = pasteItems[i].coeType;
+            },
+            replaceCode: function (v, cur, tar) {
+                if (v === '') {
+                    tar.replaceCode = v;
+                    tar.replaceName = v;
+                } else if (tar.coeType === '替换人材机' ||
+                    (!isDef(tar.coeType) && isDef(cur) && cur.coeType === '替换人材机')) {
+                    let replaceName = me.getGljName(v, me.gljList);
+                    if (replaceName) {
+                        tar.replaceCode = v;
+                        tar.replaceName = replaceName;
                     }
                 }
-                else if(typeof pasteItems[i].gljCode === 'undefined') {
-                    if(typeof pasteItems[i].coeType !== 'undefined' && me.setting.comboItems.coeType.indexOf(pasteItems[i].coeType) !== -1){
-                        validObj.coeType = pasteItems[i].coeType;
-                    }
-                    if(typeof pasteItems[i].operator !== 'undefined' && me.setting.comboItems.operator.indexOf(pasteItems[i].operator) !== -1){
-                        validObj.operator = pasteItems[i].operator;
-                    }
-                    if(typeof pasteItems[i].amount !== 'undefined' && !isNaN(pasteItems[i].amount)){
-                        validObj.amount = pasteItems[i].amount;
-                    }
+            }
+        };
+        let rst = [];
+        for(let i = 0, len = pasteItems.length; i < len; i++){
+            let row = i + info.cellRange.row;
+            let target = {},
+                curObj = me.currentGljAdjList[row],
+                pasteItem = pasteItems[i];
+            if(row < me.currentGljAdjList.length){
+                target.index = row;//要有下标做为匹配的依据,不然在复制多行并且某个单元格是只读的情况下,这里返回的updateList个数会比选中的行数少,造成更新行和实际不匹配的情况
+            }
+            for (let pasteKey in pasteItem) {
+                if (rules[pasteKey]) {
+                    rules[pasteKey](pasteItem[pasteKey], curObj, target);
                 }
             }
-            if(Object.keys(validObj).length > 0){
-                rst.push(validObj);
+            if(Object.keys(target).length > 0){
+                rst.push(target);
             }
         }
         return rst;
@@ -753,23 +767,21 @@ let gljAdjOprObj = {
     onClipboardPasted: function (sender, info) {
         let me = gljAdjOprObj, row;
         let items = sheetCommonObj.analyzePasteData(me.setting, info);
-        let validDatas = me.getValidPasteDatas(items, info);
-        for(let i = 0, len = validDatas.length; i < len; i++){
+        let validData = me.getValidPasteData(items, info);
+        for(let i = 0, len = validData.length; i < len; i++){
             row = i + info.cellRange.row;
             //update
-            if(row < me.currentGljAdjList.length && typeof validDatas[i].index !=='undefined'){
-                let updateObj = me.currentGljAdjList[validDatas[i].index];//这里改成读取下标
-                delete  validDatas[i].index; //清除下标
-                for(let attr in validDatas[i]){
-                    updateObj[attr] = validDatas[i][attr];
-                }
+            if(row < me.currentGljAdjList.length && typeof validData[i].index !=='undefined'){
+                let updateObj = me.currentGljAdjList[validData[i].index];//这里改成读取下标
+                delete  validData[i].index; //清除下标
+                Object.assign(updateObj, validData[i])
             }
             //insert
             else{
-                me.currentGljAdjList.push(validDatas[i]);
+                me.currentGljAdjList.push(validData[i]);
             }
         }
-        if(validDatas.length > 0){
+        if(validData.length > 0){
             coeOprObj.save([], [coeOprObj.currentCoe], [], false, function () {
                 me.show(me.currentGljAdjList);
             });