collapse-list.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <template>
  2. <el-scrollbar>
  3. <div class="com-collapse" :style="{ height: allowScroll ? scrollHeight : 'auto' }">
  4. <div class="collapse-box" v-for="(menu, i) in list" :key="menu" :class="{ active: menuIndex == i }">
  5. <div class="box-text" @click="menuClick(menu,i)">
  6. {{ menu.text }}
  7. </div>
  8. <div class="collapse-items">
  9. <div class="item" v-for="(item, j) in menu.children" :key="item" @click.stop="itemClick(item, j)" :class="{ active: itemIndex == j }">
  10. <span class="dot" :class="'bg-' + item.color"></span>
  11. <span class="value"> {{ item.text }}</span>
  12. </div>
  13. </div>
  14. </div>
  15. </div>
  16. </el-scrollbar>
  17. </template>
  18. <script>
  19. export default {
  20. // 名称
  21. name: "List",
  22. // 使用组件
  23. components: {},
  24. // 传入参数
  25. props: {
  26. // 标题 ""不显示
  27. title: {
  28. type: String,
  29. default: "",
  30. },
  31. // 列表面板高度 fill auto 27.778vh
  32. height: {
  33. type: String,
  34. default: "fill",
  35. },
  36. /**
  37. {
  38. id:'',
  39. text: "某某风电场",
  40. children: [{
  41. id:'',
  42. text:'2E01',
  43. color:'green'
  44. }]
  45. }
  46. */
  47. list: {
  48. type: Array,
  49. default: () => [
  50. {
  51. id: "1",
  52. text: "某某风电场",
  53. children: [
  54. {
  55. id: "1",
  56. text: "2E01",
  57. color: "green",
  58. },
  59. {
  60. id: "2",
  61. text: "2E01",
  62. color: "green",
  63. },
  64. {
  65. id: "3",
  66. text: "2E01",
  67. color: "green",
  68. },
  69. ],
  70. },
  71. {
  72. id: "2",
  73. text: "某某风电场",
  74. children: [
  75. {
  76. id: "1",
  77. text: "2E01",
  78. color: "green",
  79. },
  80. {
  81. id: "2",
  82. text: "2E01",
  83. color: "green",
  84. },
  85. {
  86. id: "3",
  87. text: "2E01",
  88. color: "green",
  89. },
  90. ],
  91. },
  92. ],
  93. },
  94. allowScroll: {
  95. type: Boolean,
  96. default: false,
  97. },
  98. scrollHeight: {
  99. type: String,
  100. default: "100%",
  101. },
  102. },
  103. // 自定义事件
  104. emits: {
  105. // 选中事件
  106. click: null,
  107. },
  108. computed: {},
  109. // 数据
  110. data () {
  111. return {
  112. menuIndex: 0,
  113. itemIndex: 0,
  114. };
  115. },
  116. // 函数
  117. methods: {
  118. menuClick (item,index) {
  119. if (this.menuIndex == index) {
  120. this.menuIndex = -1;
  121. } else {
  122. this.menuIndex = index;
  123. }
  124. if(index === 0){
  125. this.$emit("click", item);
  126. }
  127. this.itemIndex = -1;
  128. },
  129. itemClick (item, index) {
  130. this.itemIndex = index;
  131. this.wpId = item.wpId;
  132. this.wtId = item.id;
  133. this.$emit("click", item);
  134. },
  135. setDefaultActiveMenu (menu) {
  136. menu.forEach((pEle, pIndex) => {
  137. let findResult = pEle.children.find(cEle => {
  138. return cEle.wpId === this.wpId;
  139. });
  140. if (findResult) this.menuIndex = pIndex;
  141. pEle.children.forEach((cEle, cIndex) => {
  142. if (cEle.id === this.wtId) {
  143. this.itemIndex = cIndex
  144. }
  145. });
  146. });
  147. }
  148. },
  149. // 生命周期钩子
  150. beforeCreate () {
  151. // 创建前
  152. },
  153. created () {
  154. // 创建后
  155. },
  156. beforeMount () {
  157. // 渲染前
  158. },
  159. mounted () {
  160. this.wpId = this.$route.params.wpId || "";
  161. this.wtId = this.$route.params.wtId || "";
  162. this.setDefaultActiveMenu(this.list);
  163. },
  164. beforeUpdate () {
  165. // 数据更新前
  166. },
  167. updated () {
  168. // 数据更新后
  169. },
  170. watch: {
  171. list (res) {
  172. this.setDefaultActiveMenu(res)
  173. }
  174. }
  175. };
  176. </script>
  177. <style lang="less">
  178. .com-collapse {
  179. .collapse-box {
  180. cursor: pointer;
  181. .box-text {
  182. padding: 0.741vh 1.481vh;
  183. background: fade(@white, 10);
  184. &:hover {
  185. color: #fff;
  186. background: fade(@purple, 60);
  187. }
  188. }
  189. .collapse-items {
  190. display: none;
  191. .item {
  192. padding: 0.741vh 1.481vh;
  193. display: flex;
  194. align-items: center;
  195. .dot {
  196. display: inline-block;
  197. width: 0.741vh;
  198. height: 0.741vh;
  199. margin-right: 1.111vh;
  200. }
  201. .value {
  202. flex: auto;
  203. }
  204. &.active {
  205. color: #fff;
  206. cursor: pointer;
  207. }
  208. }
  209. }
  210. &.active {
  211. .box-text {
  212. color: @white;
  213. background: fade(@purple, 60);
  214. }
  215. & > .collapse-items {
  216. display: block;
  217. }
  218. }
  219. }
  220. }
  221. </style>