Element Table 動態合並 添加整行數據


效果如圖所

添加整行數據,前面幾列還是合並狀態

直接上代碼

 1 // 獲取列表, 
 2     getTableDataList() {
 3       this.tableData3 = [
 4         {
 5           all: "a1",
 6           name: "名字1",
 7           value1: "b1",
 8           value2: 1,
 9           value3: "2017年-10月"
10         },
11         {
12           all: "a1",
13           name: "名字2",
14           value1: "b2",
15           value2: "2",
16           value3: "2017年-10月"
17         },
18       ]
19       this.getSpanArr(this.tableData3);
20       this.getSpanArrOne(this.tableData3);
21     },
22     //前行row、當前列column、當前行號rowIndex、當前列號columnIndex四個屬性
23     cellMerge({ row, column, rowIndex, columnIndex }) {
24       let length = this.tableData3.length;
25       //第0列比較特殊,單獨合並
26       if (columnIndex === 0) {
27         const _row = this.spanArrOne[rowIndex];
28         const _col = _row > 0 ? 1 : 0;
29         return {
30           rowspan: _row,
31           colspan: _col
32         };
33       }
34       //1 2列進行合並
35       if (columnIndex === 1 || columnIndex === 2) {
36         const _row = this.spanArr[rowIndex];
37         const _col = _row > 0 ? 1 : 0;
38         return {
39           rowspan: _row,
40           colspan: _col
41         };
42       }
43     },
44     //1 2 列合並的數據
45     getSpanArr(data) {
46       this.spanArr = []
47       for (var i = 0; i < data.length; i++) {
48         if (i === 0) {
49           this.spanArr.push(1);
50           this.pos = 0;
51         } else {
52           // 判斷當前元素與上一個元素是否相同
53           if (data[i].name === data[i - 1].name) {
54             this.spanArr[this.pos] += 1; //需要合並的行數
55             this.spanArr.push(0); //新增被合並的行
56           } else {
57             this.spanArr.push(1);
58             this.pos = i; //新的需要合並的第幾行數
59           }
60         }
61       }
62     },
63     //0列合並的數據
64     getSpanArrOne(data) {
65       this.spanArrOne = []
66       for (var i = 0; i < data.length; i++) {
67         if (i === 0) {
68           this.spanArrOne.push(1);
69           this.posOne = 0;
70         } else {
71           this.spanArrOne[this.posOne] += 1; //需要合並的行數
72           this.spanArrOne.push(0); //新增被合並的行
73         }
74       }
75     },
76     // 添加整行
77     addRow(index,row){
78       this.tableData3.splice(index+1,0,row)
79       this.getSpanArr(this.tableData3);
80       this.getSpanArrOne(this.tableData3);
81     },

需要注意的是,每次添加都要重新計算需要合並的行數

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM