|
| 1 | +:::demo 通过设置 `footer` 数组对象给 table 添加汇总信息,footer 汇总信息可以设置多行,每一个子数组代表一行汇总信息 <br> **注意**:由于汇总信息格式不定,有求和、平均值、最大值等等,以及保留位数不定等问题。所以汇总信息由调用者提供! |
| 2 | +```html |
| 3 | +<template> |
| 4 | + <div> |
| 5 | + <v-table |
| 6 | + is-horizontal-resize |
| 7 | + style="width:100%" |
| 8 | + :height="260" |
| 9 | + :columns="columns" |
| 10 | + :table-data="tableData" |
| 11 | + :footer-cell-class-name="setFooterCellClass" |
| 12 | + :footer="footer" |
| 13 | + :footer-row-height="40" |
| 14 | + ></v-table> |
| 15 | + </div> |
| 16 | +</template> |
| 17 | + |
| 18 | +<style> |
| 19 | + .footer-cell-class-name-title { |
| 20 | + background-color: #f60; |
| 21 | + color: #fff; |
| 22 | + } |
| 23 | +
|
| 24 | + .footer-cell-class-name-normal { |
| 25 | +
|
| 26 | + color: red; |
| 27 | + } |
| 28 | +
|
| 29 | +</style> |
| 30 | + |
| 31 | +<script> |
| 32 | +
|
| 33 | + export default{ |
| 34 | + data() { |
| 35 | + return { |
| 36 | + tableData: [ |
| 37 | + {"name": "赵伟", "amount1": "2", "amount2": "3", "amount3": "上海市黄浦区金陵东路569号17楼"}, |
| 38 | + {"name": "李伟", "amount1": "5", "amount2": "4", "amount3": "上海市奉贤区南桥镇立新路12号2楼"}, |
| 39 | + {"name": "孙伟", "amount1": "3", "amount2": "9", "amount3": "上海市崇明县城桥镇八一路739号"}, |
| 40 | + {"name": "周伟", "amount1": "6", "amount2": "10", "amount3": "上海市青浦区青浦镇章浜路24号"}, |
| 41 | + {"name": "吴伟", "amount1": "1", "amount2": "12", "amount3": "上海市松江区乐都西路867-871号"} |
| 42 | + ], |
| 43 | + columns: [ |
| 44 | + {field: 'name',title: '姓名',width: 100,titleAlign: 'center',columnAlign: 'center',isFrozen: true}, |
| 45 | + {field: 'amount1',title: '数值1',width: 260,titleAlign: 'center',columnAlign: 'center',isResize: true}, |
| 46 | + {field: 'amount2',title: '数值2',width: 330,titleAlign: 'center',columnAlign: 'center',isResize: true}, |
| 47 | + {field: 'amount3',title: '数值3',width: 308,titleAlign: 'center',columnAlign: 'left',isResize: true} |
| 48 | + ], |
| 49 | + footer: [] |
| 50 | + /* 下面的数据结构 |
| 51 | + footer: [ |
| 52 | + ['最小值',1,3,'-'], |
| 53 | + ['求和',17,38,'-'] |
| 54 | + ] |
| 55 | + */ |
| 56 | + } |
| 57 | + }, |
| 58 | +
|
| 59 | + methods: { |
| 60 | +
|
| 61 | + setFooterData(){ |
| 62 | +
|
| 63 | + let result = [], |
| 64 | + amounts1 = this.tableData.map(item => { |
| 65 | + return item.amount1 |
| 66 | + }), |
| 67 | + amounts2 = this.tableData.map(item => { |
| 68 | + return item.amount2 |
| 69 | + }); |
| 70 | +
|
| 71 | + let minVal = ['最小值']; |
| 72 | + minVal.push(Math.min.apply(null, amounts1)); |
| 73 | + minVal.push(Math.min.apply(null, amounts2)); |
| 74 | + minVal.push('-'); |
| 75 | +
|
| 76 | +
|
| 77 | + let sumVal = ['求和']; |
| 78 | + sumVal.push( |
| 79 | + amounts1.reduce((prev, curr) => { |
| 80 | +
|
| 81 | + return parseInt(prev) + parseInt(curr); |
| 82 | + }, 0) |
| 83 | + ) |
| 84 | +
|
| 85 | + sumVal.push( |
| 86 | + amounts2.reduce((prev, curr) => { |
| 87 | +
|
| 88 | + return parseInt(prev) + parseInt(curr); |
| 89 | + }, 0) |
| 90 | + ) |
| 91 | +
|
| 92 | + sumVal.push('-'); |
| 93 | +
|
| 94 | +
|
| 95 | + result.push(minVal); |
| 96 | + result.push(sumVal); |
| 97 | +
|
| 98 | + this.footer = result; |
| 99 | + }, |
| 100 | +
|
| 101 | + // 设置 footer-cell-class |
| 102 | + setFooterCellClass(rowIndex, colIndex, value){ |
| 103 | +
|
| 104 | + if (colIndex === 0) { |
| 105 | +
|
| 106 | + return 'footer-cell-class-name-title' |
| 107 | + } else { |
| 108 | +
|
| 109 | + return 'footer-cell-class-name-normal' |
| 110 | + } |
| 111 | + } |
| 112 | +
|
| 113 | + }, |
| 114 | +
|
| 115 | + created(){ |
| 116 | +
|
| 117 | + this.setFooterData(); |
| 118 | + } |
| 119 | + } |
| 120 | +</script> |
| 121 | +``` |
| 122 | +::: |
0 commit comments