TreeView.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <template>
  2. <div class="he_tree_content he_tree_view" style="font-size: 14px">
  3. <Tree :draggable="draggable" :value="treeData" ref="tree">
  4. <div
  5. :style="{
  6. paddingLeft:
  7. node.is_courseware === 'false'
  8. ? node.nodexIndex * 22 + 8 + 'px'
  9. : '0px',
  10. }"
  11. class="tree_box"
  12. slot-scope="{ node, index, path, tree }"
  13. >
  14. <span
  15. @click="tree.toggleFold(node, path)"
  16. class="el-icon_box"
  17. v-if="node.is_courseware === 'false'"
  18. >
  19. <template
  20. v-if="
  21. node.children && node.children.length > 0 && node.$folded == true
  22. "
  23. >
  24. <img src="../../../assets/common/icon-tree-arrow-right.png" />
  25. </template>
  26. <template
  27. v-else-if="
  28. node.children && node.children.length > 0 && node.$folded == false
  29. "
  30. >
  31. <img src="../../../assets/common/icon-tree-arrow-bottom.png" />
  32. </template>
  33. </span>
  34. <span
  35. :class="[
  36. 'tree_box_item',
  37. 'tree_box_' + node.nodexIndex,
  38. activeIndex !== '' && JSON.stringify(node).indexOf(activeIndex) > -1
  39. ? 'tree_box_item_light'
  40. : '',
  41. ]"
  42. @click="tree.toggleFold(node, path)"
  43. v-if="node.is_courseware === 'false'"
  44. >
  45. <!-- <template>
  46. <i class="el-icon-folder-add"></i>
  47. </template>-->
  48. {{ node.name }}
  49. </span>
  50. <span
  51. :class="[
  52. 'tree_box_item',
  53. 'tree_box_leaf',
  54. activeIndex == node.id ? 'tree_box_item_active' : '',
  55. ]"
  56. :style="{ paddingLeft: node.nodexIndex * 22 + 12 + 'px' }"
  57. @click="handleNodeClick(node, path)"
  58. v-else
  59. >
  60. <template>
  61. <i class="el-icon-document"></i>
  62. </template>
  63. {{ node.name }}
  64. </span>
  65. </div>
  66. </Tree>
  67. </div>
  68. </template>
  69. <script>
  70. import "he-tree-vue/dist/he-tree-vue.css";
  71. import { getContent } from "@/api/ajax";
  72. import Cookies from "js-cookie";
  73. import { Tree, Fold, Draggable } from "he-tree-vue";
  74. import * as hp from "helper-js";
  75. export default {
  76. components: {
  77. Tree: Tree.mixPlugins([Fold, Draggable]),
  78. },
  79. props: ["changeId", "emptyQustion", "bookId", "tryFree", "changeTreeData"],
  80. data() {
  81. return {
  82. treeData: [],
  83. draggable: false,
  84. foldAllAfterMounted: true,
  85. dialogFlag: false,
  86. formDialog: {
  87. name: "",
  88. radio: "1",
  89. is_courseware: "false",
  90. children: [],
  91. },
  92. curNode: null,
  93. curIndex: "",
  94. ondragend: {},
  95. oldLists: [], // 移动之前的数组
  96. oldPid: "", // 移动节点的父id
  97. oldId: "", // 移动节点的id
  98. destId: "", // 目标节点id
  99. destPosition: 0, // 目标位置0移动节点在前1后
  100. is_coursewareFlag: "false", // 移动的是否是课件节点
  101. activeIndex: "", // 高亮节点
  102. nodeLevel: "", // 高亮节点的level
  103. nodeName: "",
  104. };
  105. },
  106. methods: {
  107. foldAll() {
  108. if (this.$refs.tree !== undefined) {
  109. this.$refs.tree.foldAll();
  110. }
  111. },
  112. getList() {
  113. let _this = this;
  114. let MethodName = "book-book_manager-GetBookChapterStruct";
  115. let data = {
  116. book_id: this.bookId,
  117. };
  118. getContent(MethodName, data).then((res) => {
  119. _this.handleData(res, 0);
  120. _this.treeData = res.nodes;
  121. _this.changeTreeData(this.treeData);
  122. _this.$nextTick(() => {
  123. _this.foldAll();
  124. });
  125. });
  126. },
  127. // 递归
  128. handleData(data, nodeIndex) {
  129. if (data.nodes) {
  130. data.nodes.forEach((item) => {
  131. item.label = item.name;
  132. item.pid = data.id;
  133. item.nodexIndex = nodeIndex;
  134. if (item.nodes) {
  135. item.children = item.nodes;
  136. item.lists = item.nodes;
  137. this.handleData(item, nodeIndex + 1);
  138. }
  139. });
  140. }
  141. },
  142. handleNodeClick(data) {
  143. this.activeIndex = data.id;
  144. this.nodeLevel = data.level_index;
  145. this.nodeName = data.name;
  146. this.changeId(data.id, data.name, data.is_free_trial);
  147. },
  148. // 返给父级当前高亮节点的index以及level
  149. handleParentIndex() {
  150. return this.activeIndex + "###" + this.nodeLevel + "###" + this.nodeName;
  151. },
  152. },
  153. created() {
  154. this.getList();
  155. console.log(this.bookId);
  156. //this.$refs.tree.foldAllAfterMounted = true;
  157. },
  158. };
  159. </script>
  160. <style lang="scss" scoped>
  161. .tree_box {
  162. display: flex;
  163. justify-content: flex-start;
  164. align-items: center;
  165. height: 40px;
  166. cursor: pointer;
  167. .kuazhan {
  168. font-size: 40px;
  169. padding: 0 10px;
  170. cursor: pointer;
  171. }
  172. .tree_box_item {
  173. // display: flex;
  174. width: 100%;
  175. padding-left: 4px;
  176. color: #2c2c2c;
  177. height: 100%;
  178. align-items: center;
  179. line-height: 44px;
  180. padding-right: 60px;
  181. overflow: hidden;
  182. white-space: nowrap;
  183. text-overflow: ellipsis;
  184. font-weight: bold;
  185. }
  186. .tree_box_item:hover {
  187. color: #ff9900;
  188. > i {
  189. color: #ff9900;
  190. }
  191. }
  192. .tree_box_item_active,
  193. .tree_box_leaf:hover {
  194. color: #fff;
  195. background: #ff9900;
  196. > i {
  197. color: #fff;
  198. }
  199. }
  200. .tree_box_item_light {
  201. color: #ff9900;
  202. > i {
  203. color: #ff9900;
  204. }
  205. }
  206. }
  207. </style>
  208. <style lang="scss">
  209. .he_tree_view .he-tree .tree-node {
  210. border: 0;
  211. padding: 0;
  212. margin: 0;
  213. }
  214. .he_tree_content {
  215. [class*=" el-icon-"],
  216. [class^="el-icon-"] {
  217. color: rgba(0, 0, 0, 0.27);
  218. font-size: 16px;
  219. }
  220. .el-icon-plus {
  221. font-weight: bold;
  222. }
  223. .el-dialog__body {
  224. padding-right: 20px !important;
  225. }
  226. .el-icon_box {
  227. width: 24px;
  228. text-align: center;
  229. font-size: 0;
  230. > img {
  231. width: 100%;
  232. margin-top: 4px;
  233. }
  234. }
  235. }
  236. .he_tree_view {
  237. .tree-node-back {
  238. padding: 0 !important;
  239. }
  240. .tree_box_leaf {
  241. // padding-left: 80px !important;
  242. display: block;
  243. width: 340px !important;
  244. font-weight: normal !important;
  245. }
  246. }
  247. </style>