tree.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <template>
  2. <div class="tree-wrapper">
  3. <!-- <ele-modal
  4. width="800px"
  5. :visible.sync="showEditFlag"
  6. :close-on-click-modal="false"
  7. custom-class="ele-dialog-form"
  8. append-to-body
  9. @close="cancel"
  10. title="使用对象"
  11. > -->
  12. <!-- <el-tree
  13. :data="treeList"
  14. :props="defaultProps"
  15. v-loading="treeLoading"
  16. :node-key="nodeKey"
  17. show-checkbox
  18. ref="tree"
  19. :expand-on-click-node="false"
  20. @check-change="handleNodeClick"
  21. v-bind="$attrs"
  22. :check-strictly="true"
  23. :default-expand-all="defaultExpandAll"
  24. >
  25. </el-tree> -->
  26. <tree-transfer
  27. :from_data="treeList"
  28. :to_data="selection"
  29. :defaultProps="defaultProps"
  30. height="540px"
  31. node_key="id"
  32. pid="parentId"
  33. @add-btn="add"
  34. @remove-btn="remove"
  35. :title="['对象', '已选对象']"
  36. >
  37. </tree-transfer>
  38. <!-- <template v-slot:footer>
  39. <el-button @click="cancel">取消</el-button>
  40. <el-button type="primary" @click="save"> 确认 </el-button>
  41. </template>
  42. </ele-modal> -->
  43. </div>
  44. </template>
  45. <script>
  46. import { getTreeByPid } from '@/api/classifyManage';
  47. import { getDocTreeListAPI } from '@/api/businessCode';
  48. import treeTransfer from 'el-tree-transfer'; // 引入
  49. export default {
  50. data() {
  51. return {
  52. defaultProps: { children: 'children', label: 'name' },
  53. treeList: [],
  54. treeLoading: false,
  55. selection: []
  56. };
  57. },
  58. components: {
  59. treeTransfer
  60. },
  61. methods: {
  62. init(selection) {
  63. this.selection = selection;
  64. this.getTreeData();
  65. },
  66. // 监听穿梭框组件添加
  67. add(fromData, toData, obj) {
  68. // 树形穿梭框模式transfer时,返回参数为左侧树移动后数据、右侧树移动后数据、移动的{keys,nodes,halfKeys,halfNodes}对象
  69. // 通讯录模式addressList时,返回参数为右侧收件人列表、右侧抄送人列表、右侧密送人列表
  70. console.log('fromData:', fromData);
  71. console.log('toData:', toData);
  72. console.log('obj:', obj);
  73. },
  74. // 监听穿梭框组件移除
  75. remove(fromData, toData, obj) {
  76. // 树形穿梭框模式transfer时,返回参数为左侧树移动后数据、右侧树移动后数据、移动的{keys,nodes,halfKeys,halfNodes}对象
  77. // 通讯录模式addressList时,返回参数为右侧收件人列表、右侧抄送人列表、右侧密送人列表
  78. console.log('fromData:', fromData);
  79. console.log('toData:', toData);
  80. console.log('obj:', obj);
  81. },
  82. // 获取树结构数据
  83. async getTreeData() {
  84. try {
  85. this.treeLoading = true;
  86. const res = await getTreeByPid(0);
  87. let res1 = await getDocTreeListAPI({ type: '0' });
  88. this.treeLoading = false;
  89. let treeList = [];
  90. if (res?.code === '0') {
  91. this.filterDoc(res1);
  92. treeList = [
  93. ...res.data
  94. .map((item) => {
  95. item.parentId = 0;
  96. return item;
  97. })
  98. .filter((item) => ['9', '1'].includes(item.id)),
  99. ...res1
  100. ];
  101. let rightIds = this.getRightDataIds(this.selection, []);
  102. this.treeList = this.filterLeftData(treeList, rightIds, []);
  103. }
  104. } catch (error) {}
  105. this.treeLoading = false;
  106. },
  107. getRightDataIds(list, rightIds) {
  108. for (let rightItem of list) {
  109. rightIds.push(rightItem.id);
  110. if (rightItem.children && rightItem.children.length) {
  111. this.getRightDataIds(rightItem.children, rightIds);
  112. }
  113. }
  114. return rightIds;
  115. },
  116. filterDoc(data) {
  117. data.forEach((item) => {
  118. item['children'] = item.sonDirectoryList;
  119. delete item.sonDirectoryList
  120. if (item.parentId == 0) {
  121. item.parentId = 0;
  122. item.name = '文档';
  123. }
  124. if (item['children'].length > 0) {
  125. this.filterDoc(item['children']);
  126. }
  127. });
  128. },
  129. filterLeftData(list, rightIds, newList) {
  130. for (let leftItem of list) {
  131. if (rightIds.includes(leftItem.id)) {
  132. if (leftItem.children && leftItem.children.length) {
  133. let insetBool = false;
  134. for (let child of leftItem.children) {
  135. if (!rightIds.includes(child.id)) insetBool = true;
  136. }
  137. if (insetBool) {
  138. let jsonItem = JSON.parse(JSON.stringify(leftItem));
  139. jsonItem.children = [];
  140. newList.push(jsonItem);
  141. this.filterLeftData(
  142. leftItem.children,
  143. rightIds,
  144. jsonItem.children
  145. );
  146. }
  147. }
  148. } else {
  149. let jsonItem = JSON.parse(JSON.stringify(leftItem));
  150. newList.push(jsonItem);
  151. }
  152. }
  153. return newList;
  154. },
  155. cancel(){
  156. this.selection=[]
  157. },
  158. getList() {
  159. return this.selection;
  160. }
  161. }
  162. };
  163. </script>
  164. <style lang="scss" scoped>
  165. .tree-wrapper {
  166. width: 100%;
  167. height: 100%;
  168. overflow: auto;
  169. :deep(.el-tree) {
  170. display: inline-block;
  171. min-width: 100%;
  172. }
  173. }
  174. </style>