|
| 1 | +export default { |
| 2 | + props: { |
| 3 | + columns: { |
| 4 | + type: Array, |
| 5 | + default: () => [] |
| 6 | + }, |
| 7 | + |
| 8 | + type: { |
| 9 | + type: String, |
| 10 | + default: 'normal' // normal/left/right |
| 11 | + }, |
| 12 | + |
| 13 | + border: [Boolean] |
| 14 | + }, |
| 15 | + |
| 16 | + inject: ['table'], |
| 17 | + |
| 18 | + data () { |
| 19 | + return { |
| 20 | + sums: [] |
| 21 | + }; |
| 22 | + }, |
| 23 | + |
| 24 | + watch: { |
| 25 | + columns () { |
| 26 | + this.sums.length === 0 && this.computedSums(); |
| 27 | + } |
| 28 | + }, |
| 29 | + |
| 30 | + methods: { |
| 31 | + getColStyle (col) { |
| 32 | + const style = {}; |
| 33 | + style.width = `${col.$realWidth}px`; |
| 34 | + |
| 35 | + return style; |
| 36 | + }, |
| 37 | + |
| 38 | + computedSums () { |
| 39 | + if (typeof this.table.summaryMethod === 'function') { |
| 40 | + const res = this.table.summaryMethod({ |
| 41 | + columns: this.table.columns, |
| 42 | + data: this.table.data |
| 43 | + }); |
| 44 | + if (Array.isArray(res)) { |
| 45 | + this.sums = [].concat(res.slice(0, this.columns.length)); |
| 46 | + return; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + const sums = []; |
| 51 | + this.columns.forEach((column, index) => { |
| 52 | + if (index === 0 && this.type !== 'right') { |
| 53 | + sums[0] = this.table.sumText; |
| 54 | + return; |
| 55 | + } |
| 56 | + const values = this.table.data.map(item => Number(item[column.prop])); |
| 57 | + const precisions = []; |
| 58 | + let notNumber = true; |
| 59 | + values.forEach(value => { |
| 60 | + if (!isNaN(value)) { |
| 61 | + notNumber = false; |
| 62 | + const decimal = ('' + value).split('.')[1]; |
| 63 | + precisions.push(decimal ? decimal.length : 0); |
| 64 | + } |
| 65 | + }); |
| 66 | + const precision = Math.max.apply(null, precisions); |
| 67 | + if (!notNumber) { |
| 68 | + sums[index] = values.reduce((prev, curr) => { |
| 69 | + const value = Number(curr); |
| 70 | + if (!isNaN(value)) { |
| 71 | + return parseFloat((prev + curr).toFixed(Math.min(precision, 20))); |
| 72 | + } else { |
| 73 | + return prev; |
| 74 | + } |
| 75 | + }, 0); |
| 76 | + } else { |
| 77 | + sums[index] = ''; |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + this.sums = [].concat(sums); |
| 82 | + }, |
| 83 | + |
| 84 | + getCellStyle (col) { |
| 85 | + const cls = ['v2-table__cell', 'v2-table__footer-cell']; |
| 86 | + |
| 87 | + if (col.align === 'left') { |
| 88 | + cls.push('text-left'); |
| 89 | + } |
| 90 | + |
| 91 | + if (col.align === 'right') { |
| 92 | + cls.push('text-right'); |
| 93 | + } |
| 94 | + |
| 95 | + return cls.join(' '); |
| 96 | + } |
| 97 | + }, |
| 98 | + |
| 99 | + render (h) { |
| 100 | + return ( |
| 101 | + <table |
| 102 | + class={{ 'v2-table__footer': true, 'v2-table__border': this.border, 'v2-table__footer-border': this.border }} |
| 103 | + cellspacing='0' |
| 104 | + border='0' |
| 105 | + cellpadding='0'> |
| 106 | + <colgroup> |
| 107 | + { |
| 108 | + this.columns.map(column => <col style={ this.getColStyle(column) } />) |
| 109 | + } |
| 110 | + </colgroup> |
| 111 | + <tbody> |
| 112 | + <tr class='v2-table__row'> |
| 113 | + { |
| 114 | + this.columns.map((col, j) => { |
| 115 | + return ( |
| 116 | + <td class={ this.getCellStyle(col) } style={{ height: this.table.cellHeight + 'px' }}> |
| 117 | + <div class='cell'> |
| 118 | + {this.sums[j]} |
| 119 | + </div> |
| 120 | + </td> |
| 121 | + ); |
| 122 | + }) |
| 123 | + } |
| 124 | + </tr> |
| 125 | + </tbody> |
| 126 | + </table> |
| 127 | + ); |
| 128 | + }, |
| 129 | + |
| 130 | + mounted () { |
| 131 | + if (this.columns.length) { |
| 132 | + this.computedSums(); |
| 133 | + } |
| 134 | + } |
| 135 | +}; |
0 commit comments