index.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <view class="jnpf-dep-select">
  3. <selectBox v-model="innerValue" :placeholder="placeholder" @openSelect="openSelect" :select-open="selectShow">
  4. </selectBox>
  5. <Tree v-if="selectShow" v-model="selectShow" :multiple="multiple" :props="props" :selectedData="selectedData"
  6. :options="options" :ids="multiple?modelValue:[modelValue]" title='岗位选择' @close="handleClose"
  7. @confirm="handleConfirm" />
  8. </view>
  9. </template>
  10. <script>
  11. import Tree from './Tree.vue';
  12. import selectBox from '@/components/selectBox'
  13. import {
  14. getPositionByPositionCondition
  15. } from '@/api/common.js'
  16. import {
  17. useBaseStore
  18. } from '@/store/modules/base'
  19. const baseStore = useBaseStore()
  20. export default {
  21. components: {
  22. Tree,
  23. selectBox
  24. },
  25. props: {
  26. modelValue: {
  27. default: ''
  28. },
  29. placeholder: {
  30. type: String,
  31. default: '请选择'
  32. },
  33. props: {
  34. type: Object,
  35. default: () => ({
  36. label: 'fullName',
  37. value: 'id',
  38. children: 'children',
  39. isLeaf: 'isLeaf'
  40. })
  41. },
  42. disabled: {
  43. type: Boolean,
  44. default: false
  45. },
  46. multiple: {
  47. type: Boolean,
  48. default: false
  49. },
  50. selectType: {
  51. type: String,
  52. default: 'all'
  53. },
  54. ableIds: {
  55. type: Array,
  56. default: () => []
  57. }
  58. },
  59. data() {
  60. return {
  61. selectShow: false,
  62. innerValue: '',
  63. selectedData: [],
  64. allList: [],
  65. options: []
  66. }
  67. },
  68. watch: {
  69. modelValue: {
  70. immediate: true,
  71. handler(val) {
  72. this.getOptions()
  73. if (!val || !val.length) {
  74. this.innerValue = ''
  75. this.selectedData = [];
  76. }
  77. }
  78. }
  79. },
  80. methods: {
  81. async getOptions() {
  82. this.options = await baseStore['getPositionTree']()
  83. this.allList = await this.treeToArray()
  84. if (this.selectType !== 'all') {
  85. this.options = []
  86. let query = {}
  87. query.ids = this.ableIds
  88. await getPositionByPositionCondition(query).then(res => {
  89. this.options = res.data.list
  90. })
  91. }
  92. if (!this.modelValue || !this.modelValue.length) return
  93. this.setDefault()
  94. },
  95. setDefault() {
  96. this.selectedData = [];
  97. let val = this.multiple ? this.modelValue : [this.modelValue];
  98. for (let i = 0; i < val.length; i++) {
  99. inner: for (let j = 0; j < this.allList.length; j++) {
  100. if (this.allList[j].id === val[i]) {
  101. this.selectedData.push(this.allList[j])
  102. break inner
  103. }
  104. }
  105. }
  106. let txt = ''
  107. for (let i = 0; i < this.selectedData.length; i++) {
  108. if (!!this.selectedData[i].lastFullName) {
  109. txt += (i ? ',' : '') + this.selectedData[i].lastFullName
  110. } else {
  111. txt += (i ? ',' : '') + this.selectedData[i].fullName
  112. }
  113. }
  114. this.innerValue = txt
  115. },
  116. async treeToArray() {
  117. let options = JSON.parse(JSON.stringify(this.options))
  118. let list = []
  119. const loop = (options) => {
  120. for (let i = 0; i < options.length; i++) {
  121. const item = options[i]
  122. list.push(item)
  123. if (item.hasChildren && item.children && Array.isArray(item.children)) {
  124. loop(item.children)
  125. }
  126. }
  127. }
  128. loop(options)
  129. return list
  130. },
  131. openSelect() {
  132. if (this.disabled) return
  133. this.selectShow = true
  134. },
  135. handleConfirm(e, selectId) {
  136. if (!this.multiple) {
  137. this.$emit('update:modelValue', selectId[0])
  138. this.$emit('change', selectId[0], e[0])
  139. } else {
  140. this.$emit('update:modelValue', selectId)
  141. this.$emit('change', selectId, e)
  142. }
  143. },
  144. handleClose() {
  145. this.selectShow = false
  146. }
  147. }
  148. }
  149. </script>
  150. <style lang="scss" scoped>
  151. .jnpf-dep-select {
  152. width: 100%;
  153. }
  154. </style>