TreeView.vue 7.5 KB

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