Tree.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <template>
  2. <u-popup class="jnpf-tree-select-popup" mode="right" :popup="false" v-model="showPopup" length="auto"
  3. :z-index="uZIndex" width="100%" @close="close">
  4. <view class="jnpf-tree-select-body">
  5. <view class="jnpf-tree-select-title">
  6. <text class="icon-ym icon-ym-report-icon-preview-pagePre backIcon" @tap="close()"></text>
  7. <view class="title">部门选择</view>
  8. </view>
  9. <view class="jnpf-tree-select-search">
  10. <u-search :placeholder="$t('app.apply.pleaseKeyword')" v-model="keyword" height="72"
  11. :show-action="false" @change="handleSearch" bg-color="#f0f2f6" shape="square">
  12. </u-search>
  13. </view>
  14. <view class="jnpf-tree-selected">
  15. <view class="jnpf-tree-selected-head">
  16. <view>{{$t('component.jnpf.common.selected')}}</view>
  17. <view v-if="multiple" class="clear-btn" @click="setCheckAll">
  18. {{$t('component.jnpf.common.clearAll')}}
  19. </view>
  20. </view>
  21. <view class="jnpf-tree-selected-box">
  22. <scroll-view scroll-y="true" class="select-list">
  23. <u-tag closeable @close="delSelect(index)" v-for="(list,index) in selectList" :key="index"
  24. :text="list.fullName" class="u-selectTag" />
  25. </scroll-view>
  26. </view>
  27. </view>
  28. <view class="jnpf-tree-select-tree">
  29. <scroll-view :scroll-y="true" style="height: 100%">
  30. <ly-tree ref="tree" v-if="selectType === 'all'" :props="realProps" :node-key="realProps.value"
  31. :load="loadNode" lazy :tree-data="lazyOptions" show-node-icon :defaultExpandAll='false'
  32. @node-click="handleNodeClick" />
  33. <ly-tree v-else ref="tree" :node-key="realProps.value" :tree-data="options"
  34. @node-click="handleNodeClick" :props="realProps" show-node-icon
  35. :filter-node-method="filterNode" />
  36. </scroll-view>
  37. </view>
  38. <view class="jnpf-tree-select-actions">
  39. <u-button class="buttom-btn" @click="close()">{{$t('common.cancelText')}}</u-button>
  40. <u-button class="buttom-btn" type="primary"
  41. @click.stop="handleConfirm()">{{$t('common.okText')}}</u-button>
  42. </view>
  43. </view>
  44. </u-popup>
  45. </template>
  46. <script>
  47. const defaultProps = {
  48. label: 'fullName',
  49. value: 'id',
  50. icon: 'icon',
  51. children: 'children'
  52. }
  53. import {
  54. selectAsyncList
  55. } from '@/api/common.js'
  56. export default {
  57. props: {
  58. options: {
  59. type: Array,
  60. default () {
  61. return [];
  62. }
  63. },
  64. selectedData: {
  65. type: Array,
  66. default () {
  67. return [];
  68. }
  69. },
  70. ids: {
  71. default: ''
  72. },
  73. selectType: {
  74. default: ''
  75. },
  76. modelValue: {
  77. type: Boolean,
  78. default: false
  79. },
  80. zIndex: {
  81. type: [String, Number],
  82. default: 0
  83. },
  84. props: {
  85. type: Object,
  86. default: () => ({
  87. label: 'fullName',
  88. value: 'id',
  89. icon: 'icon',
  90. children: 'children',
  91. isLeaf: 'isLeaf'
  92. })
  93. },
  94. multiple: {
  95. type: Boolean,
  96. default: false
  97. }
  98. },
  99. data() {
  100. return {
  101. moving: false,
  102. selectList: [],
  103. selectListId: [],
  104. newListId: [],
  105. keyword: '',
  106. showPopup: false,
  107. lazyOptions: []
  108. };
  109. },
  110. watch: {
  111. // 在select弹起的时候,重新初始化所有数据
  112. modelValue: {
  113. handler(val) {
  114. this.showPopup = val
  115. if (val) setTimeout(() => this.init(), 10);
  116. },
  117. immediate: true
  118. },
  119. },
  120. computed: {
  121. uZIndex() {
  122. return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
  123. },
  124. realProps() {
  125. return {
  126. ...defaultProps,
  127. ...this.props
  128. }
  129. }
  130. },
  131. methods: {
  132. init() {
  133. this.keyword = ""
  134. this.selectListId = Array.isArray(this.ids) ? JSON.parse(JSON.stringify(this.ids.filter(item => item
  135. ?.trim() !== ''))) : []
  136. this.selectList = JSON.parse(JSON.stringify(this.selectedData))
  137. },
  138. loadNode(node, resolve) {
  139. let id = node.key === null ? 0 : node.key
  140. this.level = node.level
  141. let data = {
  142. keyword: '',
  143. currOrgId: 0
  144. }
  145. selectAsyncList(data, id).then(res => {
  146. resolve(res.data?.list)
  147. })
  148. },
  149. selectAsyncList() {
  150. let data = {
  151. keyword: this.keyword,
  152. currOrgId: 0
  153. }
  154. this.lazyOptions = []
  155. selectAsyncList(data, 0).then(res => {
  156. this.lazyOptions = res.data.list || []
  157. })
  158. },
  159. handleNodeClick(obj) {
  160. let data = obj.data
  161. if (data.type === 'company') return
  162. if (this.multiple) {
  163. this.selectListId.push(data.id)
  164. this.selectListId = [...new Set(this.selectListId)];
  165. } else {
  166. this.selectList = []
  167. this.selectListId = data.id
  168. }
  169. this.selectList.push({
  170. fullName: this.selectType === 'all' ? data.fullName : data.lastFullName,
  171. id: data.id
  172. })
  173. //数组对象去重--[...new Set(this.selectList.map(JSON.stringify))].map(JSON.parse)
  174. //数组去重--[...new Set(this.selectList)]
  175. this.selectList = [...new Set(this.selectList.map(JSON.stringify))].map(JSON.parse);
  176. },
  177. delSelect(index) {
  178. this.selectList.splice(index, 1);
  179. this.selectListId.splice(index, 1);
  180. },
  181. setCheckAll() {
  182. this.selectListId = [];
  183. this.selectList = [];
  184. this.$refs.tree.setCheckAll(false);
  185. },
  186. filterNode(value, data) {
  187. if (!value) return true;
  188. return data[this.realProps.label].indexOf(value) !== -1;
  189. },
  190. handleSearch(val) {
  191. this.keyword = val
  192. if (this.selectType === 'all') return this.$u.debounce(this.selectAsyncList, 600)
  193. this.$refs.tree && this.$refs.tree.filter(val)
  194. },
  195. handleConfirm() {
  196. this.$emit('confirm', this.selectList, this.selectListId);
  197. this.close();
  198. },
  199. close() {
  200. this.$emit('close', false);
  201. }
  202. }
  203. };
  204. </script>