Hierarchical method to realize automatic summation of an item in table

Previous blog said: summation different levels of entry in table form, on a watch using a method, this method code is cumbersome, very consumption performance today with a new way to solve this problem:
simon
my On the side, there are multiple large classifications, classification 1 classification 2 classification 3, and then there are small classifications under each classification.You need to count the total of each small classification, and then count the total of each large classification and put it in the total! (This is definitely different according to personal business code requirements, everyone changes according to their own needs, I only write my own here)

		/**
         * 修改单元格自动计算父级值
         */
        makeDataRelation(){
        //行号
            let column = this.selectedCell.columnIndex;
            //第几列
            let thisLv = this.tableData[this.selectedCell.rowIndex].computeLevel;
            //列号
            let thisRowIndex = this.selectedCell.rowIndex;
            //小分类
            if(thisLv > 1){
                this.computeParentValue(thisRowIndex,thisLv)
            }
            //大分类求和
            if(thisLv > 0){
                this.tableData[27][column] =this.tableData[0][column] + this.tableData[5][column] + this.tableData[6][column] + this.tableData[26][column]
            }
        },
        computeParentValue(rowIndex,thisLv){
            if(thisLv > 1){
                let obj = this.searchIdenticalLevel(rowIndex,thisLv);
                let sameLevelIndexArr = obj.sameLevelIndexArr;
                let parentIndex = obj.parentIndex;
                let parentValue = 0;
                //子类求和
                for (let i = 0; i < sameLevelIndexArr.length; i++) {
                    let  el = this.tableData[sameLevelIndexArr[i]];
                    parentValue += el[this.selectedCell.columnIndex];
                }
                //求和赋值给大类
                this.tableData[parentIndex][this.selectedCell.columnIndex] = parentValue;
                this.computeParentValue(parentIndex,--thisLv);
            }
        },
        searchIdenticalLevel(rowIndex,thisLv){
            let obj = {
                parentIndex:0,
                sameLevelIndexArr: []
            };
            let startIndex = 0;
            let endIndex = this.tableData.length-1;
            for (let i = rowIndex-1; i < rowIndex; i--) {
                if(this.tableData[i].computeLevel < thisLv){
                    startIndex = i+1;
                    obj.parentIndex = i;
                    break
                }
            }
            for (let i = rowIndex+1; i < this.tableData.length; i++) {
                if(this.tableData[i].computeLevel < thisLv){
                    endIndex = i-1;
                }
            }
            for (let i = startIndex; i <= endIndex; i++) {
                if(this.tableData[i].computeLevel == thisLv){
                    obj.sameLevelIndexArr.push(i)
                } 
            }
            return obj;
        },

Only one idea is provided here, it depends on the actual situation!

Published 34 original articles · won praise 0 · Views 3634

Guess you like

Origin blog.csdn.net/qq_43469899/article/details/103582405