|
| 1 | +<template> |
| 2 | + <el-table :data="formatData" :row-style="showRow" v-bind="$attrs"> |
| 3 | + <el-table-column v-if="columns.length===0" width="150"> |
| 4 | + <template slot-scope="scope"> |
| 5 | + <span v-for="space in scope.row._level" :key="space" class="ms-tree-space"/> |
| 6 | + <span v-if="iconShow(0,scope.row)" class="tree-ctrl" @click="toggleExpanded(scope.$index)"> |
| 7 | + <i v-if="!scope.row._expanded" class="el-icon-plus"/> |
| 8 | + <i v-else class="el-icon-minus"/> |
| 9 | + </span> |
| 10 | + {{ scope.$index }} |
| 11 | + </template> |
| 12 | + </el-table-column> |
| 13 | + <el-table-column v-for="(column, index) in columns" v-else :key="column.value" :label="column.text" :width="column.width"> |
| 14 | + <template slot-scope="scope"> |
| 15 | + <!-- Todo --> |
| 16 | + <!-- eslint-disable-next-line vue/no-confusing-v-for-v-if --> |
| 17 | + <span v-for="space in scope.row._level" v-if="index === 0" :key="space" class="ms-tree-space"/> |
| 18 | + <span v-if="iconShow(index,scope.row)" class="tree-ctrl" @click="toggleExpanded(scope.$index)"> |
| 19 | + <i v-if="!scope.row._expanded" class="el-icon-plus"/> |
| 20 | + <i v-else class="el-icon-minus"/> |
| 21 | + </span> |
| 22 | + {{ scope.row[column.value] }} |
| 23 | + </template> |
| 24 | + </el-table-column> |
| 25 | + <slot/> |
| 26 | + </el-table> |
| 27 | +</template> |
| 28 | + |
| 29 | +<script> |
| 30 | +/** |
| 31 | + Auth: Lei.j1ang |
| 32 | + Created: 2018年1月19日-13:59 |
| 33 | +*/ |
| 34 | +import treeToArray from './eval' |
| 35 | +export default { |
| 36 | + name: 'TreeTable', |
| 37 | + props: { |
| 38 | + /* eslint-disable */ |
| 39 | + data: { |
| 40 | + type: [Array, Object], |
| 41 | + required: true |
| 42 | + }, |
| 43 | + columns: { |
| 44 | + type: Array, |
| 45 | + default: () => [] |
| 46 | + }, |
| 47 | + evalFunc: Function, |
| 48 | + evalArgs: Array, |
| 49 | + expandAll: { |
| 50 | + type: Boolean, |
| 51 | + default: false |
| 52 | + } |
| 53 | + }, |
| 54 | + computed: { |
| 55 | + // 格式化数据源 |
| 56 | + formatData: function() { |
| 57 | + let tmp |
| 58 | + if (!Array.isArray(this.data)) { |
| 59 | + tmp = [this.data] |
| 60 | + } else { |
| 61 | + tmp = this.data |
| 62 | + } |
| 63 | + const func = this.evalFunc || treeToArray |
| 64 | + const args = this.evalArgs ? Array.prototype.concat([tmp, this.expandAll], this.evalArgs) : [tmp, this.expandAll] |
| 65 | + return func.apply(null, args) |
| 66 | + } |
| 67 | + }, |
| 68 | + methods: { |
| 69 | + showRow: function(row) { |
| 70 | + const show = (row.row.parent ? (row.row.parent._expanded && row.row.parent._show) : true) |
| 71 | + row.row._show = show |
| 72 | + return show ? 'animation:treeTableShow 1s;-webkit-animation:treeTableShow 1s;' : 'display:none;' |
| 73 | + }, |
| 74 | + // 切换下级是否展开 |
| 75 | + toggleExpanded: function(trIndex) { |
| 76 | + const record = this.formatData[trIndex] |
| 77 | + record._expanded = !record._expanded |
| 78 | + }, |
| 79 | + // 图标显示 |
| 80 | + iconShow(index, record) { |
| 81 | + return (index === 0 && record.children && record.children.length > 0) |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | +</script> |
| 86 | +<style rel="stylesheet/css"> |
| 87 | + @keyframes treeTableShow { |
| 88 | + from {opacity: 0;} |
| 89 | + to {opacity: 1;} |
| 90 | + } |
| 91 | + @-webkit-keyframes treeTableShow { |
| 92 | + from {opacity: 0;} |
| 93 | + to {opacity: 1;} |
| 94 | + } |
| 95 | +</style> |
| 96 | + |
| 97 | +<style lang="scss" rel="stylesheet/scss" scoped> |
| 98 | + $color-blue: #2196F3; |
| 99 | + $space-width: 18px; |
| 100 | + .ms-tree-space { |
| 101 | + position: relative; |
| 102 | + top: 1px; |
| 103 | + display: inline-block; |
| 104 | + font-style: normal; |
| 105 | + font-weight: 400; |
| 106 | + line-height: 1; |
| 107 | + width: $space-width; |
| 108 | + height: 14px; |
| 109 | + &::before { |
| 110 | + content: "" |
| 111 | + } |
| 112 | + } |
| 113 | + .processContainer{ |
| 114 | + width: 100%; |
| 115 | + height: 100%; |
| 116 | + } |
| 117 | + table td { |
| 118 | + line-height: 26px; |
| 119 | + } |
| 120 | + |
| 121 | + .tree-ctrl{ |
| 122 | + position: relative; |
| 123 | + cursor: pointer; |
| 124 | + color: $color-blue; |
| 125 | + margin-left: -$space-width; |
| 126 | + } |
| 127 | +</style> |
0 commit comments