tree.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <template>
  2. <view class="content">
  3. <view class="mix-tree-list">
  4. <block v-for="(item, index) in treeList" :key="index">
  5. <view
  6. class="mix-tree-item"
  7. style="position: relative;"
  8. :style="[{
  9. paddingLeft: item.rank*15 + 'px',
  10. zIndex: item.rank*-1 +50
  11. }]"
  12. :class="{
  13. border: treeParams.border === true,
  14. show: item.show,
  15. last: item.lastRank,
  16. showchild: item.showChild
  17. }"
  18. @click.stop="treeItemTap(item, index)"
  19. >
  20. <u-icon :name="item.icon?item.icon:'play-right-fill'" color="#333" v-if="!item.lastRank" style="margin-right: 15upx;" size="28"></u-icon>
  21. <!-- <image class="mix-tree-icon" :src="item.lastRank ? treeParams.lastIcon : item.showChild ? treeParams.currentIcon : treeParams.defaultIcon"></image> -->
  22. {{item.name}}
  23. </view>
  24. </block>
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. export default {
  30. props: {
  31. list: {
  32. type: Array,
  33. default(){
  34. return [];
  35. }
  36. },
  37. params: {
  38. type: Object,
  39. default(){
  40. return {}
  41. }
  42. }
  43. },
  44. data() {
  45. return {
  46. treeList: [],
  47. treeParams: {
  48. defaultIcon: '/static/mix-tree/defaultIcon.png',
  49. currentIcon: '/static/mix-tree/currentIcon.png',
  50. lastIcon: '',
  51. border: false
  52. }
  53. }
  54. },
  55. watch: {
  56. list(list){
  57. this.treeParams = Object.assign(this.treeParams, this.params);
  58. console.log(this.treeParams, this.params);
  59. this.renderTreeList(list);
  60. }
  61. },
  62. methods: {
  63. //扁平化树结构
  64. renderTreeList(list=[], rank=0, parentId=[]){
  65. list.forEach((item,index)=>{
  66. this.treeList.push({
  67. id: item.id,
  68. name: item.name,
  69. parentId, // 父级id数组
  70. rank, // 层级
  71. showChild: false, //子级是否显示
  72. show: rank === 0 // 自身是否显示
  73. })
  74. if(Array.isArray(item.children) && item.children.length > 0){
  75. let parents = [...parentId];
  76. parents.push(item.id);
  77. this.renderTreeList(item.children, rank+1, parents);
  78. }else{
  79. this.treeList[this.treeList.length-1].lastRank = true;
  80. }
  81. })
  82. },
  83. // 点击
  84. treeItemTap(item){
  85. let list = this.treeList;
  86. let id = item.id;
  87. if(item.lastRank === true){
  88. //点击最后一级时触发事件
  89. this.$emit('treeItemClick', item);
  90. return;
  91. }
  92. item.showChild = !item.showChild;
  93. if(!item.icon){
  94. this.$set(item,'icon','arrow-down-fill');
  95. }else if(item.icon && item.icon == 'play-right-fill'){
  96. item.icon = 'arrow-down-fill';
  97. }else {
  98. this.$set(item,'icon','play-right-fill');
  99. }
  100. list.forEach(childItem=>{
  101. if(item.showChild === false){
  102. //隐藏所有子级
  103. if(!childItem.parentId.includes(id)){
  104. return;
  105. }
  106. if(childItem.lastRank !== true){
  107. childItem.showChild = false;
  108. }
  109. childItem.show = false;
  110. }else{
  111. if(childItem.parentId[childItem.parentId.length-1] === id){
  112. childItem.show = true;
  113. }
  114. }
  115. })
  116. }
  117. }
  118. }
  119. </script>
  120. <style>
  121. .mix-tree-list{
  122. display: flex;
  123. flex-direction: column;
  124. padding-left: 30upx;
  125. }
  126. .mix-tree-item{
  127. display: flex;
  128. align-items: center;
  129. font-size: 30upx;
  130. color: #333;
  131. height: 0;
  132. opacity: 0;
  133. transition: .2s;
  134. position: relative;
  135. }
  136. .show {
  137. border-bottom: 1px solid #eee;
  138. }
  139. .mix-tree-item.border{
  140. border-bottom: 1px solid #eee;
  141. }
  142. .mix-tree-item.show{
  143. height: 80upx;
  144. opacity: 1;
  145. }
  146. .mix-tree-icon{
  147. width: 26upx;
  148. height: 26upx;
  149. margin-right: 8upx;
  150. opacity: .9;
  151. }
  152. .mix-tree-item.showchild:before{
  153. transform: rotate(90deg);
  154. }
  155. .mix-tree-item.last:before{
  156. opacity: 0;
  157. }
  158. </style>