index.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <template>
  2. <div class="tree-wrapper">
  3. <el-tree
  4. :data="treeList"
  5. :props="defaultProps"
  6. v-loading="treeLoading"
  7. :node-key="nodeKey"
  8. ref="tree"
  9. :highlight-current="true"
  10. :expand-on-click-node="false"
  11. @node-click="handleNodeClick"
  12. :default-expanded-keys="defaultExpandedKeys"
  13. v-bind="$attrs"
  14. :default-expand-all="defaultExpandAll"
  15. >
  16. </el-tree>
  17. </div>
  18. </template>
  19. <script>
  20. import { getProduceTreeByPid } from '@/api/saleManage/quotation';
  21. import { getInfoByIds,getTreeByIds } from '@/api/classifyManage/index';
  22. // let originId = '';
  23. // let originType = '';
  24. export default {
  25. props: {
  26. // treeList私有化处理
  27. treeFormate: {
  28. type: Function,
  29. default: null
  30. },
  31. defaultProps: {
  32. type: Object,
  33. default: function () {
  34. return {
  35. children: 'children',
  36. value: 'id',
  37. label: 'name'
  38. };
  39. }
  40. },
  41. defaultExpandAll: {
  42. type: Boolean,
  43. default: function () {
  44. return false;
  45. }
  46. },
  47. // 初始请求treeList
  48. init: {
  49. type: Boolean,
  50. default: true
  51. },
  52. isFirstRefreshTable: {
  53. type: Boolean,
  54. default: true
  55. },
  56. id: {
  57. type: String,
  58. default: '0'
  59. },
  60. nodeKey: {
  61. type: String,
  62. default: 'id'
  63. },
  64. typeIds: {
  65. type: Array,
  66. default: () => []
  67. },
  68. ids:{
  69. type: Array,
  70. default: () => []
  71. },
  72. itemType:{
  73. type:String,
  74. default:'1'
  75. }
  76. // appendRoot: {
  77. // type: Boolean,
  78. // default: false
  79. // },
  80. },
  81. data() {
  82. return {
  83. defaultExpandedKeys: [],
  84. treeList: [],
  85. treeLoading: false,
  86. parentName: '',
  87. parentId: '',
  88. currentKey: ''
  89. };
  90. },
  91. mounted() {
  92. if (this.init) {
  93. this.getTreeData();
  94. }
  95. },
  96. methods: {
  97. getInstance() {
  98. return this.$refs.tree;
  99. },
  100. // 获取树结构数据
  101. async getTreeData(params = {}) {
  102. try {
  103. this.treeLoading = true;
  104. let data = {};
  105. if (this.typeIds.length > 0) {
  106. let res = await getInfoByIds(this.typeIds.toString());
  107. data= res.data
  108. }else if(this.ids.length > 0){
  109. let res = await getTreeByIds({ ids: this.ids.join(',') });
  110. data= res.data
  111. } else {
  112. data = await getProduceTreeByPid(this.itemType);
  113. }
  114. console.log(data,'data')
  115. this.treeLoading = false;
  116. this.$emit('setRootId', data[0].id);
  117. this.treeList = this.$util.toTreeData({
  118. data: data || [],
  119. idField: 'id',
  120. parentIdField: 'parentId'
  121. });
  122. this.defaultExpandedKeys = [this.treeList[0].id || ''];
  123. this.$nextTick(() => {
  124. // 默认高亮第一级树节点
  125. if (this.treeList[0] && this.isFirstRefreshTable) {
  126. this.setCurrentKey(this.treeList[0].id);
  127. this.handleNodeClick(
  128. this.treeList[0],
  129. this.$refs.tree.getCurrentNode()
  130. );
  131. }
  132. });
  133. return this.treeList;
  134. } catch (error) {
  135. console.error(error);
  136. }
  137. this.treeLoading = false;
  138. },
  139. // 递归 - 往树children里面添加parentName
  140. // _setParentName (tree) {
  141. // let data = tree;
  142. // for (let i = 0; i < data.length; i++) {
  143. // if (data[i].parentId === '0') {
  144. // this.parentName = data[i].name;
  145. // originId = data[i].id;
  146. // originType = data[i].type;
  147. // }
  148. // data[i]['originId'] = originId;
  149. // data[i]['originType'] = originType;
  150. // data[i]['parentId'] = data[i]['parentId'];
  151. // if (data[i].children && data[i].children.length > 0) {
  152. // this._setParentName(data[i].children);
  153. // }
  154. // }
  155. // return data;
  156. // },
  157. handleNodeClick(data, node) {
  158. this.$emit('handleNodeClick', data, node);
  159. },
  160. // 设置默认高亮行
  161. setCurrentKey(id) {
  162. this.currentKey = id;
  163. this.$refs.tree.setCurrentKey(this.currentKey);
  164. },
  165. // 获取树的选中状态
  166. getSelectList() {
  167. const selectList = this.$refs.tree.getCurrentNode();
  168. return selectList;
  169. }
  170. }
  171. };
  172. </script>
  173. <style lang="scss" scoped>
  174. .tree-wrapper {
  175. width: 100%;
  176. height: 100%;
  177. overflow: auto;
  178. :deep(.el-tree) {
  179. display: inline-block;
  180. min-width: 100%;
  181. }
  182. }
  183. </style>