CategoryDialog.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <template>
  2. <ele-modal
  3. :title="title"
  4. :visible.sync="treeVisible"
  5. :before-close="handleClose"
  6. :close-on-click-modal="false"
  7. :close-on-press-escape="false"
  8. append-to-body
  9. width="30%"
  10. :maxable="true"
  11. :resizable="true"
  12. >
  13. <div class="ele-border-lighter sys-organization-list">
  14. <el-tree
  15. :data="treeList"
  16. :props="defaultProps"
  17. v-loading="treeLoading"
  18. :node-key="nodeKey"
  19. ref="tree"
  20. :highlight-current="true"
  21. :expand-on-click-node="false"
  22. @node-click="handleNodeClick"
  23. v-bind="$attrs"
  24. >
  25. </el-tree>
  26. </div>
  27. <template v-slot:footer>
  28. <el-button @click="handleClose">关闭 </el-button>
  29. <el-button type="primary" @click="selected">选择</el-button>
  30. </template>
  31. </ele-modal>
  32. </template>
  33. <script>
  34. import { getProduceTreeByPid } from '@/api/saleManage/quotation';
  35. export default {
  36. data() {
  37. return {
  38. treeVisible: false,
  39. treeList: [],
  40. current: null,
  41. title: '选择分类',
  42. pathList: [],
  43. treeLoading: false,
  44. nodeKey: 'id',
  45. defaultProps: {
  46. children: 'children',
  47. value: 'id',
  48. label: 'name'
  49. }
  50. };
  51. },
  52. watch: {},
  53. methods: {
  54. open() {
  55. this.treeVisible = true;
  56. this.getTreeData();
  57. },
  58. handleClose() {
  59. this.treeVisible = false;
  60. this.current = null;
  61. },
  62. // 获取树结构数据
  63. async getTreeData() {
  64. try {
  65. this.treeLoading = true;
  66. let data = await getProduceTreeByPid();
  67. this.treeLoading = false;
  68. this.treeList = this.$util.toTreeData({
  69. data: data || [],
  70. idField: 'id',
  71. parentIdField: 'parentId'
  72. });
  73. } catch (error) {}
  74. this.treeLoading = false;
  75. },
  76. handleNodeClick(data, node) {
  77. this.$emit('handleNodeClick', data, node);
  78. this.current = data;
  79. },
  80. selected() {
  81. if (!this.current) {
  82. return this.$message.warning(`请${this.title}`);
  83. }
  84. this.pathList = this.findParent([], this.current, this.treeList);
  85. let ruleCode = null;
  86. if (this.pathList.length == 0) {
  87. ruleCode = this.current.ruleCode;
  88. } else {
  89. ruleCode =
  90. this.pathList[this.pathList.length - 1] &&
  91. this.pathList[this.pathList.length - 1].ruleCode;
  92. }
  93. this.pathList.unshift(this.current);
  94. const PathInfo = {
  95. categoryLevelPath: '',
  96. categoryLevelPathId: [],
  97. rootCategoryLevelId: null
  98. };
  99. const pathName = [];
  100. this.pathList.map((item) => {
  101. pathName.unshift(item.name);
  102. PathInfo.categoryLevelPathId.unshift(item.id);
  103. PathInfo.rootCategoryLevelId = item.rootCategoryLevelId;
  104. });
  105. PathInfo.categoryLevelPath = pathName.join('-');
  106. PathInfo.categoryLevelPathId = PathInfo.categoryLevelPathId.join(',');
  107. console.log(PathInfo);
  108. this.$emit('chooseCategory', [
  109. this.current,
  110. this.title,
  111. PathInfo,
  112. ruleCode
  113. ]);
  114. this.handleClose();
  115. },
  116. // parents:用于返回的数组,childNode:要查询的节点,treeList:json树形数据
  117. findParent(parents, childNode, treeList) {
  118. for (let i = 0; i < treeList.length; i++) {
  119. // 父节点查询条件
  120. if (treeList[i].id === childNode.parentId) {
  121. // 如果找到结果,保存当前节点
  122. parents.push(treeList[i]);
  123. // 用当前节点再去原数据查找当前节点的父节点
  124. this.findParent(parents, treeList[i], this.treeList);
  125. break;
  126. } else {
  127. if (treeList[i].children instanceof Array) {
  128. // 没找到,遍历该节点的子节点
  129. this.findParent(parents, childNode, treeList[i].children);
  130. }
  131. }
  132. }
  133. return parents;
  134. }
  135. }
  136. };
  137. </script>
  138. <style lang="scss" scoped>
  139. .sys-organization-list {
  140. height: calc(100vh - 264px);
  141. box-sizing: border-box;
  142. border-width: 1px;
  143. border-style: solid;
  144. overflow: auto;
  145. }
  146. .sys-organization-list :deep(.el-tree-node__content) {
  147. height: 30px;
  148. & > .el-tree-node__expand-icon {
  149. margin-left: 10px;
  150. }
  151. }
  152. ::v-deep .el-tree-node__content:hover {
  153. background-color: #e6f7ff !important;
  154. }
  155. ::v-deep
  156. .el-tree--highlight-current
  157. .el-tree-node.is-current
  158. > .el-tree-node__content {
  159. background-color: #40a9ff !important;
  160. }
  161. </style>