treeMenus.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <template>
  2. <div>
  3. <template v-for="(item, i) in list">
  4. <div
  5. :key="item.id"
  6. :class="[
  7. 'tree-node',
  8. { selected: currentCourse === item.id },
  9. { children: depth > 0 },
  10. { 'has-children': item.is_leaf === 'false' }
  11. ]"
  12. >
  13. <div
  14. class="tree-node-name"
  15. :style="{ 'font-weight': depth === 0 || item.nodes ? 700 : 400 }"
  16. @click="handleTreeClick(item.is_leaf === 'true', i, item.id)"
  17. >
  18. <span class="tree-node-name-leaf">
  19. <i
  20. v-if="item.is_leaf === 'false'"
  21. :class="[scopesDefault[i] ? 'el-icon-arrow-down' : 'el-icon-arrow-right']"
  22. />
  23. </span>
  24. <span>{{ item.name }}</span>
  25. </div>
  26. <div v-if="item.is_leaf === 'false'" class="tree-node-children">
  27. <tree-menus
  28. v-show="scopesDefault[i]"
  29. :list="item.nodes"
  30. :depth="depth + 1"
  31. :current-course="currentCourse"
  32. @curCourse="curCourse"
  33. />
  34. </div>
  35. </div>
  36. </template>
  37. </div>
  38. </template>
  39. <script>
  40. export default {
  41. name: 'TreeMenus',
  42. props: {
  43. list: {
  44. default() {
  45. return [];
  46. },
  47. type: Array
  48. },
  49. depth: {
  50. default: 0,
  51. type: Number
  52. },
  53. currentCourse: {
  54. default: '',
  55. type: String
  56. }
  57. },
  58. data() {
  59. return {
  60. scopesDefault: [],
  61. scopes: []
  62. };
  63. },
  64. created() {
  65. this.scope();
  66. },
  67. methods: {
  68. scope() {
  69. this.list.forEach((item, i) => {
  70. this.scopesDefault[i] = false;
  71. if ('nodes' in item) {
  72. this.scopes[i] = true;
  73. } else {
  74. this.scopes[i] = false;
  75. }
  76. });
  77. },
  78. curCourse(val) {
  79. this.$emit('curCourse', val);
  80. },
  81. handleTreeClick(is_leaf, i, id) {
  82. if (is_leaf) {
  83. this.curCourse(id);
  84. return false;
  85. } else if (this.scopesDefault[i] === true) {
  86. this.$set(this.scopesDefault, i, false);
  87. } else {
  88. this.$set(this.scopesDefault, i, this.scopes[i]);
  89. }
  90. }
  91. }
  92. };
  93. </script>
  94. <style lang="scss">
  95. .tree-node {
  96. &-name {
  97. display: flex;
  98. padding: 12px 0;
  99. cursor: pointer;
  100. font-weight: 700;
  101. :nth-child(2):hover {
  102. color: $basicColor;
  103. }
  104. &-leaf {
  105. display: inline-block;
  106. width: 20px;
  107. }
  108. }
  109. &.selected {
  110. background-color: $basicColor;
  111. color: #fff;
  112. }
  113. &.children {
  114. &.has-children {
  115. margin-left: 20px;
  116. }
  117. }
  118. &:not(.has-children) .tree-node-name:hover {
  119. color: #fff;
  120. background-color: $basicColor;
  121. :nth-child(2):hover {
  122. color: #fff;
  123. }
  124. }
  125. }
  126. </style>