TreeView.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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: [
  80. "changeId",
  81. "emptyQustion",
  82. "bookId",
  83. "tryFree",
  84. "changeTreeData",
  85. "currentTreeID",
  86. ],
  87. data() {
  88. return {
  89. treeData: [],
  90. draggable: false,
  91. foldAllAfterMounted: true,
  92. dialogFlag: false,
  93. formDialog: {
  94. name: "",
  95. radio: "1",
  96. is_courseware: "false",
  97. children: [],
  98. },
  99. curNode: null,
  100. curIndex: "",
  101. ondragend: {},
  102. oldLists: [], // 移动之前的数组
  103. oldPid: "", // 移动节点的父id
  104. oldId: "", // 移动节点的id
  105. destId: "", // 目标节点id
  106. destPosition: 0, // 目标位置0移动节点在前1后
  107. is_coursewareFlag: "false", // 移动的是否是课件节点
  108. activeIndex: "", // 高亮节点
  109. nodeLevel: "", // 高亮节点的level
  110. nodeName: "",
  111. pidList:[],
  112. };
  113. },
  114. watch: {
  115. // 监听预览页面翻页的变化 树组件做出相同的操作
  116. async currentTreeID(newval, oldval) {
  117. if (newval) {
  118. this.activeIndex = newval;
  119. await this.unfoldData(this.activeIndex, this.treeData);
  120. await this.unfoldFather(this.treeData);
  121. }
  122. },
  123. },
  124. methods: {
  125. // 递归找到当前节点的所有父节点
  126. unfoldData(activeIndex, data, index, child) {
  127. data.forEach((item, i) => {
  128. if (item.id == activeIndex) {
  129. this.pidList.push(item.pid);
  130. if (Object.prototype.toString.call(index).indexOf("Number") != -1) {
  131. this.pidList.push(child.pid);
  132. }
  133. } else if (item.children) {
  134. this.unfoldData(activeIndex, item.children, i, item);
  135. }
  136. });
  137. },
  138. // 展示父节点
  139. unfoldFather(data) {
  140. data.forEach((item, i) => {
  141. this.pidList.forEach((p) => {
  142. if (item.id == p) {
  143. item.$folded = false;
  144. } else if (item.children) {
  145. this.unfoldFather(item.children);
  146. }
  147. });
  148. });
  149. },
  150. foldAll() {
  151. if (this.$refs.tree !== undefined) {
  152. this.$refs.tree.foldAll();
  153. }
  154. },
  155. getList() {
  156. let _this = this;
  157. let MethodName = "book-book_manager-GetBookChapterStruct";
  158. let data = {
  159. book_id: this.bookId,
  160. };
  161. getContent(MethodName, data).then((res) => {
  162. _this.handleData(res, 0);
  163. _this.treeData = res.nodes;
  164. _this.changeTreeData(this.treeData);
  165. _this.$nextTick(() => {
  166. _this.foldAll();
  167. });
  168. });
  169. },
  170. // 递归
  171. handleData(data, nodeIndex) {
  172. if (data.nodes) {
  173. data.nodes.forEach((item) => {
  174. item.label = item.name;
  175. item.pid = data.id;
  176. item.nodexIndex = nodeIndex;
  177. if (item.nodes) {
  178. item.children = item.nodes;
  179. item.lists = item.nodes;
  180. this.handleData(item, nodeIndex + 1);
  181. }
  182. });
  183. }
  184. },
  185. handleNodeClick(data) {
  186. this.activeIndex = data.id;
  187. this.nodeLevel = data.level_index;
  188. this.nodeName = data.name;
  189. this.changeId(data.id, data.name, data.is_free_trial);
  190. },
  191. // 返给父级当前高亮节点的index以及level
  192. handleParentIndex() {
  193. return this.activeIndex + "###" + this.nodeLevel + "###" + this.nodeName;
  194. },
  195. },
  196. created() {
  197. this.getList();
  198. console.log(this.bookId);
  199. //this.$refs.tree.foldAllAfterMounted = true;
  200. },
  201. };
  202. </script>
  203. <style lang="scss" scoped>
  204. .tree_box {
  205. display: flex;
  206. justify-content: flex-start;
  207. align-items: center;
  208. height: 40px;
  209. cursor: pointer;
  210. .kuazhan {
  211. font-size: 40px;
  212. padding: 0 10px;
  213. cursor: pointer;
  214. }
  215. .tree_box_item {
  216. // display: flex;
  217. width: 100%;
  218. padding-left: 4px;
  219. color: #2c2c2c;
  220. height: 100%;
  221. align-items: center;
  222. line-height: 44px;
  223. padding-right: 60px;
  224. overflow: hidden;
  225. white-space: nowrap;
  226. text-overflow: ellipsis;
  227. font-weight: bold;
  228. }
  229. .tree_box_item:hover {
  230. color: #ff9900;
  231. > i {
  232. color: #ff9900;
  233. }
  234. }
  235. .tree_box_item_active,
  236. .tree_box_leaf:hover {
  237. color: #fff;
  238. background: #ff9900;
  239. > i {
  240. color: #fff;
  241. }
  242. }
  243. .tree_box_item_light {
  244. color: #ff9900;
  245. > i {
  246. color: #ff9900;
  247. }
  248. }
  249. }
  250. </style>
  251. <style lang="scss">
  252. .he_tree_view .he-tree .tree-node {
  253. border: 0;
  254. padding: 0;
  255. margin: 0;
  256. }
  257. .he_tree_content {
  258. [class*=" el-icon-"],
  259. [class^="el-icon-"] {
  260. color: rgba(0, 0, 0, 0.27);
  261. font-size: 16px;
  262. }
  263. .el-icon-plus {
  264. font-weight: bold;
  265. }
  266. .el-dialog__body {
  267. padding-right: 20px !important;
  268. }
  269. .el-icon_box {
  270. width: 24px;
  271. text-align: center;
  272. font-size: 0;
  273. > img {
  274. width: 100%;
  275. margin-top: 4px;
  276. }
  277. }
  278. }
  279. .he_tree_view {
  280. .tree-node-back {
  281. padding: 0 !important;
  282. }
  283. .tree_box_leaf {
  284. // padding-left: 80px !important;
  285. display: block;
  286. width: 340px !important;
  287. font-weight: normal !important;
  288. }
  289. }
  290. </style>