SelectPopup.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <template>
  2. <u-popup class="jnpf-tree-select-popup" mode="right" v-model="showPopup" width="100%" @close="close">
  3. <view class="jnpf-tree-select-body">
  4. <view class="jnpf-tree-select-title">
  5. <text class="icon-ym icon-ym-report-icon-preview-pagePre backIcon" @tap="close()"></text>
  6. <view class="title">岗位选择</view>
  7. </view>
  8. <view class="jnpf-tree-select-search">
  9. <u-search :placeholder="$t('app.apply.pleaseKeyword')" v-model="keyword" height="72"
  10. :show-action="false" @change="handleSearch" bg-color="#f0f2f6" shape="square">
  11. </u-search>
  12. </view>
  13. <view class="jnpf-tree-selected">
  14. <view class="jnpf-tree-selected-head">
  15. <view>{{$t('component.jnpf.common.selected')}}({{selectedList.length||0}})</view>
  16. <view v-if="multiple" class="clear-btn" @click="setCheckAll">
  17. {{$t('component.jnpf.common.clearAll')}}
  18. </view>
  19. </view>
  20. <view class="jnpf-tree-selected-box">
  21. <scroll-view scroll-y="true" class="select-list">
  22. <u-tag closeable @close="delSelect(index)" v-for="(item,index) in selectedList" :key="index"
  23. :text="item.orgNameTree" class="u-selectTag" />
  24. </scroll-view>
  25. </view>
  26. </view>
  27. <view class="jnpf-tree-selected-line"></view>
  28. <view class="jnpf-tree-selected-tabs">
  29. <view class="tab-item" :class="{'tab-item-active':activeKey==='1'}" @click="toggloActive('1')">岗位
  30. </view>
  31. <view class="tab-item" :class="{'tab-item-active':activeKey==='2'}" @click="toggloActive('2')"
  32. v-if="selectType === 'all'">
  33. 当前岗位
  34. </view>
  35. </view>
  36. <view class="jnpf-tree-select-tree">
  37. <scroll-view :scroll-y="true" style="height: 100%" v-if="activeKey==='1' && !hasPage">
  38. <ly-tree v-if="selectType !== 'all'" :tree-data="options" :node-key="realProps.value"
  39. default-expand-all :props="realProps" :filter-node-method="filterNode"
  40. child-visible-for-filter-node show-node-icon @node-click="handleTreeNodeClick"
  41. :show-checkbox="multiple" :show-radio="!multiple" :expandOnClickNode="false"
  42. :checkOnClickNode="true" :expandOnCheckNode="false" checkStrictly ref="tree">
  43. </ly-tree>
  44. <ly-tree ref="tree" v-if="selectType === 'all'" :props="realProps" :node-key="realProps.value"
  45. :load="loadNode" lazy :tree-data="lazyOptions" show-node-icon :defaultExpandAll='false'
  46. @node-click="handleTreeNodeClick" :show-checkbox="multiple" :show-radio="!multiple"
  47. :expandOnClickNode="false" :checkOnClickNode="true" :expandOnCheckNode="false" checkStrictly />
  48. </scroll-view>
  49. <scroll-view :scroll-y="true" style="height: 100%" :refresher-enabled="false" :refresher-threshold="100"
  50. :scroll-with-animation='true' :refresher-triggered="triggered" @scrolltolower="handleScrollToLower"
  51. v-else>
  52. <view class="jnpf-selcet-list" v-if="list.length">
  53. <view class="jnpf-selcet-cell" v-for="item in list" :key="item.id"
  54. @click.stop="handleNodeClick(item)">
  55. <view class="jnpf-selcet-cell-action">
  56. <lyCheckbox :type="multiple ? 'checkbox' : 'radio'"
  57. :checked="selectedIds.includes(item.id)" />
  58. </view>
  59. <view class="jnpf-selcet-cell-name">
  60. {{item.orgNameTree}}
  61. </view>
  62. </view>
  63. </view>
  64. <Empty class="h-full" v-else />
  65. </scroll-view>
  66. </view>
  67. <!-- 底部按钮 -->
  68. <view class="jnpf-tree-select-actions">
  69. <u-button class="buttom-btn" @click="close()">{{$t('common.cancelText')}}</u-button>
  70. <u-button class="buttom-btn" type="primary"
  71. @click.stop="handleConfirm()">{{$t('common.okText')}}</u-button>
  72. </view>
  73. </view>
  74. </u-popup>
  75. </template>
  76. <script>
  77. import {
  78. getPositionByCondition,
  79. getOrgAndPosSelector,
  80. getPositionSelector
  81. } from '@/api/common'
  82. import lyCheckbox from '@/components/ly-tree/components/ly-checkbox.vue';
  83. import Empty from '../Empty/index.vue'
  84. const defaultProps = {
  85. label: 'fullName',
  86. value: 'id',
  87. icon: 'icon',
  88. children: 'children'
  89. }
  90. export default {
  91. props: {
  92. selectedData: {
  93. type: Array,
  94. default: () => []
  95. },
  96. ableIds: {
  97. type: Array,
  98. default: () => []
  99. },
  100. selectType: {
  101. type: String,
  102. default: 'all'
  103. },
  104. // 通过双向绑定控制组件的弹出与收起
  105. modelValue: {
  106. type: Boolean,
  107. default: false
  108. },
  109. // 弹出的z-index值
  110. zIndex: {
  111. type: [String, Number],
  112. default: 0
  113. },
  114. props: {
  115. type: Object,
  116. default: () => ({
  117. label: 'fullName',
  118. value: 'id',
  119. icon: 'icon',
  120. children: 'children',
  121. isLeaf: 'isLeaf'
  122. })
  123. },
  124. multiple: {
  125. type: Boolean,
  126. default: false
  127. }
  128. },
  129. components: {
  130. lyCheckbox,
  131. Empty
  132. },
  133. data() {
  134. return {
  135. moving: false,
  136. selectedList: [],
  137. selectedIds: [],
  138. keyword: '',
  139. showPopup: false,
  140. options: [],
  141. lazyOptions: [],
  142. activeKey: '1',
  143. hasPage: false,
  144. pagination: {
  145. hasPage: 1,
  146. currentPage: 1,
  147. pageSize: 20
  148. },
  149. triggered: false,
  150. finish: false,
  151. list: [],
  152. };
  153. },
  154. watch: {
  155. // 在select弹起的时候,重新初始化所有数据
  156. modelValue: {
  157. handler(val) {
  158. this.showPopup = val
  159. if (!val) this.activeKey = ''
  160. if (val) setTimeout(() => this.init(), 10);
  161. },
  162. immediate: true
  163. },
  164. selectedList: {
  165. handler(val) {
  166. this.selectedIds = val.map((o) => o.id);
  167. this.$refs.tree && this.$refs.tree.setCheckedKeys(this.selectedIds)
  168. },
  169. deep: true
  170. },
  171. },
  172. computed: {
  173. uZIndex() {
  174. // 如果用户有传递z-index值,优先使用
  175. return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
  176. },
  177. realProps() {
  178. return {
  179. ...defaultProps,
  180. ...this.props
  181. }
  182. },
  183. getCurrPosList() {
  184. const userInfo = uni.getStorageSync('userInfo') || {}
  185. const list = (userInfo.positionList || []).map((o) => ({
  186. ...o,
  187. orgNameTree: o.treeName
  188. }))
  189. return list
  190. }
  191. },
  192. methods: {
  193. init() {
  194. this.keyword = ""
  195. this.hasPage = 0
  196. this.activeKey = '1'
  197. this.finish = false
  198. this.pagination.currentPage = 1
  199. this.$nextTick(() => {
  200. this.triggered = true
  201. })
  202. this.selectedList = JSON.parse(JSON.stringify(this.selectedData))
  203. if (this.selectType !== 'all') this.getConditionOptions()
  204. },
  205. getConditionOptions() {
  206. if (!this.ableIds.length) return
  207. const query = {
  208. ids: this.ableIds
  209. }
  210. getPositionByCondition(query).then(res => {
  211. this.options = res.data.list || []
  212. this.$refs.tree && this.$refs.tree.setCheckedKeys(this.selectedIds)
  213. })
  214. },
  215. filterNode(value, data) {
  216. if (!value) return true;
  217. return data[this.realProps.label].indexOf(value) !== -1;
  218. },
  219. loadNode(node, resolve) {
  220. const id = node.key || '0'
  221. const type = node?.data?.type || 'organize'
  222. const data = {
  223. id,
  224. type
  225. }
  226. getOrgAndPosSelector(data).then(res => {
  227. const list = (res.data?.list || []).map(o => ({
  228. ...o,
  229. disabled: o.type !== 'position'
  230. }))
  231. resolve(list)
  232. this.$refs.tree && this.$refs.tree.setCheckedKeys(this.selectedIds)
  233. })
  234. },
  235. handleScrollToLower() {
  236. if (this.finish || this.activeKey === '2') return
  237. this.getTreeData()
  238. },
  239. getTreeData() {
  240. let data = {
  241. keyword: this.keyword,
  242. parentId: '0',
  243. ...this.pagination
  244. }
  245. getPositionSelector(data).then(res => {
  246. const list = res.data.list || []
  247. if (list.length < this.pagination.pageSize) this.finish = true;
  248. this.list = this.list.concat(list);
  249. this.pagination.currentPage++
  250. })
  251. },
  252. onTreeCheck(item) {
  253. this.handleTreeNodeClick(item);
  254. },
  255. handleTreeNodeClick(item) {
  256. const data = item.data
  257. if (this.selectType === 'all' && data.type !== 'position') return
  258. this.handleNodeClick(data)
  259. },
  260. handleNodeClick(data) {
  261. const index = this.selectedList.findIndex((o) => o.id === data.id);
  262. if (index !== -1) return this.selectedList.splice(index, 1);
  263. this.multiple ? this.selectedList.push(data) : (this.selectedList = [data]);
  264. },
  265. delSelect(index) {
  266. this.selectedList.splice(index, 1);
  267. },
  268. setCheckAll() {
  269. this.selectedIds = []
  270. this.selectedList = []
  271. },
  272. handleConfirm() {
  273. this.$emit('confirm', this.selectedList, this.selectedIds);
  274. this.close();
  275. },
  276. close() {
  277. this.$emit('close', false);
  278. },
  279. toggloActive(key) {
  280. if (this.activeKey === key) return
  281. this.keyword = ''
  282. this.$nextTick(() => {
  283. this.activeKey = key
  284. if (this.activeKey === '2') this.list = this.getCurrPosList
  285. })
  286. },
  287. handleSearch(val) {
  288. this.keyword = val
  289. if (this.selectType !== 'all') return this.$refs.tree && this.$refs.tree.filter(val)
  290. this.hasPage = !!val
  291. this.list = []
  292. if (this.activeKey != '1') this.activeKey = '1'
  293. this.$nextTick(() => {
  294. if (this.keyword) {
  295. this.pagination.currentPage = 1
  296. this.finish = false
  297. this.$u.debounce(this.getTreeData, 300)
  298. }
  299. })
  300. },
  301. }
  302. };
  303. </script>