index.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <!--
  2. 机构下拉树组件
  3. @Author: linqian
  4. @Date: 2021-08-02 13:49
  5. -->
  6. <template>
  7. <el-select
  8. ref="selectOrgTree"
  9. class="dg-select"
  10. v-model="selectedNodeLabel"
  11. v-bind="$attrs"
  12. v-on="$listeners"
  13. @filter-change="handleFilterOrg"
  14. @clearSelect="handleClearSelect"
  15. :placeholder="placeholder"
  16. :filterable="filterable"
  17. :loadData="treeData"
  18. :loading="loading"
  19. clearable
  20. style="width: 300px"
  21. @scroll-bottom="handleScrollBottom"
  22. >
  23. <dg-tree
  24. ref="tree"
  25. :key="key"
  26. radioType="all"
  27. :props="props"
  28. v-bind="treeProps"
  29. empty-text=""
  30. :lazy="lazy"
  31. :data="treeData"
  32. :render-after-expand="false"
  33. v-model="treeValue"
  34. @node-click="handleNodeClick"
  35. ></dg-tree>
  36. </el-select>
  37. </template>
  38. <script>
  39. import ElSelect from './select';
  40. import * as commonApi from '@/api/common';
  41. export default {
  42. components: {
  43. ElSelect
  44. },
  45. props: {
  46. nodeKey: {
  47. type: String,
  48. default: 'code'
  49. },
  50. apiName: {
  51. type: String,
  52. default: 'getOrgTree'
  53. },
  54. placeholder: {
  55. type: String,
  56. default: '请选择单位名称'
  57. },
  58. // 目前所知道的值有:APP, 0
  59. type: {
  60. type: String
  61. },
  62. // 是否支持查询
  63. filterable: {
  64. type: Boolean,
  65. default: true
  66. },
  67. // 是否默认选中根节点
  68. defaultSelectRoot: {
  69. type: Boolean,
  70. default: false
  71. }
  72. },
  73. data() {
  74. return {
  75. props: {
  76. value: this.nodeKey,
  77. label: 'name',
  78. children: 'children',
  79. isLeaf(data) {
  80. return data.isParent !== true;
  81. }
  82. },
  83. treeProps: {
  84. nodeKey: this.nodeKey,
  85. load: this.loadNode
  86. },
  87. searchForm: {
  88. mtType: 'app',
  89. name: '',
  90. pageNum: 0,
  91. pageSize: 50
  92. },
  93. treeValue: '',
  94. data: [],
  95. lazy: true,
  96. treeData: [],
  97. key: 0,
  98. selectedNodeLabel: '',
  99. loading: true
  100. };
  101. },
  102. computed: {
  103. hasSlots() {
  104. return this.$slots && this.$slots.default && this.$slots.default.length > 0;
  105. }
  106. },
  107. watch: {
  108. treeValue(val) {
  109. if (val) {
  110. const { data } = this.$refs.tree.getNode(val);
  111. this.$refs.selectOrgTree.blur();
  112. this.selectedNodeLabel = data.name;
  113. this.$refs.selectOrgTree.oldValue = data.name;
  114. }
  115. }
  116. },
  117. methods: {
  118. handleNodeClick(data, node, tree) {
  119. this.$emit('orgTreeValue', data[this.nodeKey], this.nodeKey);
  120. this.$emit('orgTreeNode', data);
  121. },
  122. loadNode(node, resolve) {
  123. this.getTree(node.level === 0 ? void 0 : node.data.id, resolve, node.level);
  124. },
  125. // 组织机构
  126. getTree(id, resolve, level) {
  127. const { params, apiName } = this;
  128. commonApi[apiName]({ id, type: this.type })
  129. .then((res) => {
  130. // 默认选中根节点
  131. if (this.defaultSelectRoot && level === 0) {
  132. this.treeValue = res[0][this.nodeKey];
  133. this.selectedNodeLabel = res[0].name;
  134. this.$emit('orgRootValue', res[0][this.nodeKey], this.nodeKey);
  135. }
  136. return resolve(res || []);
  137. })
  138. .catch(() => resolve([]));
  139. },
  140. handleChange(val) {
  141. this.$emit('submitTreeValue', val);
  142. },
  143. searchOrgTree() {},
  144. handleFilterOrg(val) {
  145. this.searchForm.name = $.trim(val);
  146. this.searchForm.pageNum = 0;
  147. if (this.searchForm.name) {
  148. if (this.lazy === true) {
  149. this.key++;
  150. this.lazy = false;
  151. }
  152. this.getSearchTree();
  153. } else {
  154. if (this.lazy === false) {
  155. this.key++;
  156. this.lazy = true;
  157. }
  158. }
  159. },
  160. handleClearSelect() {
  161. this.treeValue = '';
  162. this.selectedNodeLabel = '';
  163. this.$refs.tree.setCurrentKey(null);
  164. this.$refs.tree.setRadioKeys([]);
  165. this.$emit('orgTreeValue', '', this.nodeKey);
  166. },
  167. handleScrollBottom() {
  168. if (!this.lazy && !this.isLast) {
  169. this.searchForm.pageNum++;
  170. this.getSearchTree();
  171. }
  172. },
  173. // 查询
  174. getSearchTree() {
  175. this.loading = true;
  176. commonApi.treeMatch(this.searchForm).then((res) => {
  177. let { content, totalElements } = res.data;
  178. const _content = content.map((item) => {
  179. return {
  180. ...item,
  181. isParent: true,
  182. name: item.fullName
  183. };
  184. });
  185. if (this.searchForm.pageNum == 0) {
  186. this.treeData = [];
  187. }
  188. this.treeData = [...this.treeData, ..._content];
  189. this.isLast = this.treeData.length == totalElements;
  190. this.loading = false;
  191. });
  192. }
  193. }
  194. };
  195. </script>
  196. <style lang="scss" scoped>
  197. </style>