| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 | /** * Created by CSL on 2017-06-08. */ //modified by zhong on 2017-09-25var rationCoeOprObj = {    sheet: null,    libID: null,    curRation: null,    tempDelArr: [],    cache: {},    setting: {        header:[            {headerName:"编号",headerWidth:120,dataCode:"serialNo", dataType: "Number", hAlign: 'left'},            {headerName:"名称",headerWidth:400,dataCode:"name", dataType: "String", hAlign: 'left'},            {headerName:"内容",headerWidth:300,dataCode:"content", dataType: "String", hAlign: 'left'}        ],        view:{            comboBox:[],            lockColumns:[1,2]        }    },    buildSheet: function(sheet) {        var me = this;        me.sheet = sheet;        //me.libID = storageUtil.getSessionCache("RationGrp","repositoryID"); // 不可靠,有时取不到        me.libID = pageOprObj.rationLibId;        if (me.libID == undefined){me.libID = getQueryString('repository')};        sheetCommonObj.initSheet(me.sheet, me.setting, 30);        me.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);        me.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);        me.sheet.bind(GC.Spread.Sheets.Events.EditStarting, me.onEditStarting);        me.sheet.bind(GC.Spread.Sheets.Events.EditEnded, me.onEditEnded);       // me.sheet.bind(GC.Spread.Sheets.Events.RangeChanged, me.onRangeChanged);    },    onClipboardPasting: function(sender, args) {        var me = rationCoeOprObj;        let rationSection = rationOprObj.getCache();        let rationRow = rationOprObj.workBook.getSheet(0).getSelections()[0].row;        me.curRation = rationRow < rationSection.length ? rationSection[rationRow] : null;        if (args.cellRange.colCount != 1 || args.cellRange.col != 0 || !(me.curRation) || (me.curRation && me.curRation.type === rationOprObj.type.std)) {            args.cancel = true;        }    },    onClipboardPasted: function(e, info) {        var me = rationCoeOprObj;        if (me.libID) {            // 修改第一列(编号)            if (info.cellRange.col == 0) {                me.tempDelArr = [];                var coeNos = [];                var items = sheetCommonObj.analyzePasteData({header:[{dataCode: "serialNo"}] }, info);                let curCache = typeof me.cache["_Coe_" + me.curRation.ID] !== 'undefined' ? me.cache["_Coe_" + me.curRation.ID] : [];                let isRefresh = false;                for(let i = 0, len = items.length; i < len; i++){                    if(!isNaN(items[i].serialNo)){                        let row = i + info.cellRange.row;                        //update                        if(row < curCache.length){                            let isExist = false;                            for(let j = 0, jLen = curCache.length; j < jLen; j++){                                if(items[i].serialNo === curCache[j].serialNo && j !== row){                                    isExist = true;                                    break;                                }                            }                            if(!isExist){                                me.tempDelArr.push({org: curCache[row], newNo: items[i].serialNo});                                coeNos.push(items[i].serialNo);                            }                            else{                                isRefresh = true;                            }                        }                        else{                            coeNos.push(items[i].serialNo);                        }                    }                }                //delete in front                if(me.tempDelArr.length > 0){                   for(let i = 0, len = me.tempDelArr.length; i < len; i++){                       for(let j = 0; j < curCache.length; j++){                           if(me.tempDelArr[i].org.serialNo === curCache[j].serialNo){                               curCache.splice(j, 1);                               break;                           }                       }                   }                }                me.addCoeItems(coeNos);                if(isRefresh){                    me.showCoeItems(me.curRation.ID);                }            } else {                //修改其它列。            }        }    },    onEditStarting: function (sender, args) {        let me = rationCoeOprObj;        let rationSection = rationOprObj.getCache();        let rationRow = rationOprObj.workBook.getSheet(0).getSelections()[0].row;        me.curRation = rationRow < rationSection.length ? rationSection[rationRow] : null;        if(!me.curRation || args.col !== 0 || (me.curRation && me.curRation.type === rationOprObj.type.std)){            args.cancel = true;        }    },    onEditEnded: function(sender, args){        var me = rationCoeOprObj;        if (args.col == 0 && args.editingText && args.editingText.toString().trim().length > 0 && !isNaN(args.editingText)) {   // 编号列            let curCahe = typeof me.cache["_Coe_" + me.curRation.ID] !== 'undefined' ? me.cache["_Coe_" + me.curRation.ID] : [];            me.tempDelArr = [];            //update            if(args.row < curCahe.length && args.editingText != curCahe[args.row].serialNo){                let isExist = false;                for(let i = 0, len = curCahe.length; i < len; i++){                    if(args.editingText == curCahe[i].serialNo){                        isExist = true;                        break;                    }                }                if(!isExist){                    me.tempDelArr.push({org: curCahe[args.row], newNo: args.editingText});                    curCahe.splice(args.row, 1);                    me.addCoeItems([args.editingText]);                }                else{                    me.showCoeItems(me.curRation.ID);                }            }            //insert            else{                me.addCoeItems([args.editingText]);            }        }        else{            sheetCommonObj.cleanData(me.sheet, me.setting, -1);            me.showCoeItems(me.curRation.ID);        }    },    bindRationCoeDel: function () {        let me = rationCoeOprObj;        let workBook = me.sheet.getParent();        workBook.commandManager().register('rationCoeDel', function () {            if(!me.curRation || me.curRation.type === rationOprObj.type.std){                return;            }            let sels = me.sheet.getSelections(), isUpdate = false;            let curCahe = me.cache["_Coe_" + me.curRation.ID];            for(let i = 0, len = sels.length; i < len; i ++ ){                if(sels[i].colCount === me.setting.header.length){                    if(sels[i].row < curCahe.length){                        isUpdate = true;                        curCahe.splice(sels[i].row, sels[i].rowCount);                    }                }            }            if(isUpdate){                me.updateCurRation(function () {                    me.sheet.getParent().focus(true);                });                sheetCommonObj.cleanData(me.sheet, me.setting, -1);                me.showCoeItems(me.curRation.ID);            }        });        workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);        workBook.commandManager().setShortcutKey('rationCoeDel', GC.Spread.Commands.Key.del, false, false, false, false);    },    getRecoveryArr: function (tempDelArr, newArr) {//获得更新的coe不存在,恢复删除的被更新数据        let rst = [];        for(let i = 0, len = tempDelArr.length; i < len; i++){            let isExist = false;            for(let j = 0, jLen = newArr.length; j < jLen; j++){                if(tempDelArr[i].newNo == newArr[j].serialNo){                    isExist = true;                    break;                }            }            if(!isExist){                rst.push(tempDelArr[i].org);            }        }        return rst;    },    addCoeItems: function(coeNos) {        var me = this;        sheetCommonObj.cleanData(me.sheet, me.setting, -1);        var curCache = me.cache["_Coe_" + me.curRation.ID];        var temp = [];        if (curCache) {            for (var i = 0; i < coeNos.length; i++) {                var isExist = false;                for (let obj of curCache) {                    if (obj.serialNo == coeNos[i]) {                        isExist = true;                        break;                    };                };                if (!isExist) {                    temp.push(coeNos[i]);                };            };        }else{            for (let obj of coeNos){temp.push(obj)};        };        if(temp.length == 0){            me.showCoeItems(me.curRation.ID);            //sheetCommonObj.lockCells(me.sheet, me.setting);        }else{            CommonAjax.post('api/getCoeItemsByNos', {libID: me.libID, coeNos: temp}, function (rstData) {                var rstArr = [];                for (let obj of rstData){rstArr.push(obj)};                if (curCache) {                    curCache = curCache.concat(rstArr);                }else{                    curCache = rstArr;                }                let recoveryArr = me.getRecoveryArr(me.tempDelArr, rstData);                if(recoveryArr.length > 0){                    curCache = curCache.concat(recoveryArr);                }                me.cache["_Coe_" + me.curRation.ID] = curCache;                me.updateCurRation(function () {                    me.sheet.getParent().focus(true);                });                me.showCoeItems(me.curRation.ID);            });        };    },    getCoeItems: function(ration, callback) {        var me = this;        me.curRation = ration;        /*if (ration == undefined || ration.rationCoeList == undefined ||            ration.rationCoeList.length == 0){return;};*/        var coeList = ration.rationCoeList;        let coeIDs = [];        for(let i = 0, len = coeList.length; i < len; i++){            coeIDs.push(coeList[i].ID);        }        var curCache = me.cache["_Coe_" + ration.ID];        if (curCache) {            me.showCoeItems(ration.ID);            //sheetCommonObj.lockCells(me.sheet, me.setting);        } else if(!curCache && typeof coeList !== 'undefined' && coeList.length > 0) {            var data = {"libID": me.libID, "coeIDs": coeIDs};            CommonAjax.post('api/getCoeItemsByIDs', data, function (rstData) {                sheetCommonObj.cleanData(me.sheet, me.setting, -1);                var tempResult = [];                for (let obj of rstData) {                    tempResult.push(obj);                };                me.cache["_Coe_" + ration.ID] = tempResult;                me.showCoeItems(ration.ID);                //sheetCommonObj.lockCells(me.sheet, me.setting);                if(callback) callback();            });        };    },    showCoeItems: function(rationID) {        var me = this;        var curCache = me.cache["_Coe_" + rationID];        if (curCache) {            curCache.sort(function(a, b) {                var rst = 0;                if (a.serialNo > b.serialNo) rst = 1                else if (a.serialNo < b.serialNo) rst = -1;                return rst;            });            sheetsOprObj.showData(me.sheet, me.setting, curCache);        }    },    updateCurRation: function(callback) {        var me = this, updateArr = [];        if (me.curRation) {            var rst = [];            var curCache = me.cache["_Coe_" + me.curRation.ID];            if (curCache) {                for (let obj of curCache) {                    rst.push(obj.ID);                };                me.curRation.rationCoeList = rst;                updateArr.push(me.curRation);                rationOprObj.mixUpdateRequest(updateArr, [], [], function () {                    if(callback) callback();                });            };        };    }}
 |