index.vue 4.6 KB

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