index.vue 3.3 KB

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