You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.4 KiB

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