elementUI 表格 如何合并相同数据的列?
在编写表格table中,会出现合并数据相同的列这种需求,如图:
实现方法
添加span-method 方法,elementUI提供了span-method方法 让我们可以动态合并表格,其中有四个参数,分别是row, column, rowIndex, columnIndex
objectSpanMethod的具体实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| setdates(arr) { var obj = {}, k, arr1 = []; for(var i = 0, len = arr.length; i < len; i++) { k = arr[i].date; if(obj[k]) obj[k]++; else obj[k] = 1; } console.log(obj) for(var o in obj) { for(let i=0;i<obj[o];i++){ if(i===0){ this.arr1.push(obj[o]) }else{ this.arr1.push(0) } } } console.log(this.arr1);
}, objectSpanMethod({ row, column, rowIndex, columnIndex }) { if (columnIndex === 0 ) { let _row = this.arr1[rowIndex] let _col = this.arr1[rowIndex] > 0 ? 1 : 0 setdates(arr) { var obj = {}, k, arr1 = []; for(var i = 0, len = arr.length; i < len; i++) { k = arr[i].date; if(obj[k]) obj[k]++; else obj[k] = 1; } console.log(obj) for(var o in obj) { for(let i=0;i<obj[o];i++){ if(i===0){ this.arr1.push(obj[o]) }else{ this.arr1.push(0) } } } console.log(this.arr1);
}, objectSpanMethod({ row, column, rowIndex, columnIndex }) { if (columnIndex === 0 ) { let _row = this.arr1[rowIndex] let _col = this.arr1[rowIndex] > 0 ? 1 : 0 return [_row,_col] } }
|
完整代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| :data="tableData" border style="width: 100%" :span-method="objectSpanMethod" > prop="date" label="日期" width="180"> prop="name" label="姓名" width="180"> prop="address" label="地址">
|