treeMenus.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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, item.is_courseware)"
  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. watch: {
  65. list: function () {
  66. this.scope();
  67. }
  68. },
  69. created() {
  70. this.scope();
  71. },
  72. methods: {
  73. scope() {
  74. this.scopesDefault = [];
  75. this.list.forEach((item, i) => {
  76. this.scopesDefault[i] = false;
  77. if ('nodes' in item) {
  78. this.scopes[i] = true;
  79. } else {
  80. this.scopes[i] = false;
  81. }
  82. });
  83. },
  84. curCourse(id, is_courseware) {
  85. this.$emit('curCourse', id, is_courseware);
  86. },
  87. handleTreeClick(is_leaf, i, id, is_courseware) {
  88. if (is_leaf) {
  89. this.curCourse(id, is_courseware);
  90. return false;
  91. } else if (this.scopesDefault[i] === true) {
  92. this.$set(this.scopesDefault, i, false);
  93. } else {
  94. this.$set(this.scopesDefault, i, this.scopes[i]);
  95. }
  96. }
  97. }
  98. };
  99. </script>
  100. <style lang="scss">
  101. .tree-node {
  102. &-name {
  103. display: flex;
  104. padding: 12px 0;
  105. cursor: pointer;
  106. font-weight: 700;
  107. :nth-child(2):hover {
  108. color: $basicColor;
  109. }
  110. &-leaf {
  111. display: inline-block;
  112. width: 20px;
  113. }
  114. }
  115. &.selected {
  116. background-color: $basicColor;
  117. color: #fff;
  118. }
  119. &.children {
  120. &.has-children {
  121. margin-left: 20px;
  122. }
  123. }
  124. &:not(.has-children) .tree-node-name:hover {
  125. color: #fff;
  126. background-color: $basicColor;
  127. :nth-child(2):hover {
  128. color: #fff;
  129. }
  130. }
  131. }
  132. </style>