index.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <view class="jnpf-group-select w-full">
  3. <selectBox v-model="innerValue" :placeholder="placeholder" @openSelect="openSelect" :select-open="selectShow"
  4. :disabled="disabled">
  5. </selectBox>
  6. <SelectPopup v-model="selectShow" :getOptions="getOptions" :multiple="multiple" :selectedData="selectedData"
  7. @close="handleClose" @confirm="handleConfirm" />
  8. </view>
  9. </template>
  10. <script>
  11. import SelectPopup from './SelectPopup';
  12. import selectBox from '@/components/selectBox'
  13. import {
  14. useBaseStore
  15. } from '@/store/modules/base'
  16. import {
  17. getGroupCondition
  18. } from '@/api/common'
  19. const baseStore = useBaseStore()
  20. export default {
  21. name: 'jnpf-group-select',
  22. components: {
  23. SelectPopup,
  24. selectBox
  25. },
  26. props: {
  27. modelValue: {
  28. default: ''
  29. },
  30. placeholder: {
  31. type: String,
  32. default: '请选择'
  33. },
  34. disabled: {
  35. type: Boolean,
  36. default: false
  37. },
  38. multiple: {
  39. type: Boolean,
  40. default: false
  41. },
  42. selectType: {
  43. type: String,
  44. default: 'all'
  45. },
  46. ableIds: {
  47. type: Array,
  48. default: () => []
  49. },
  50. },
  51. data() {
  52. return {
  53. selectShow: false,
  54. innerValue: '',
  55. selectedData: [],
  56. allList: [],
  57. }
  58. },
  59. watch: {
  60. modelValue: {
  61. handler() {
  62. this.setDefault()
  63. },
  64. immediate: true
  65. },
  66. allList: {
  67. handler() {
  68. this.setDefault()
  69. },
  70. deep: true,
  71. },
  72. },
  73. created() {
  74. this.initData()
  75. },
  76. methods: {
  77. async initData() {
  78. this.allList = await baseStore.getGroupList()
  79. },
  80. setDefault() {
  81. if (!this.modelValue || !this.modelValue.length) return this.setNullValue();
  82. let selectedData = [];
  83. let val = this.multiple ? this.modelValue : [this.modelValue];
  84. for (let i = 0; i < val.length; i++) {
  85. inner: for (let j = 0; j < this.allList.length; j++) {
  86. if (this.allList[j].id === val[i]) {
  87. selectedData.push(this.allList[j])
  88. break inner
  89. }
  90. }
  91. }
  92. this.selectedData = selectedData
  93. this.innerValue = this.selectedData.map(o => o.fullName).join();
  94. },
  95. getOptions() {
  96. return new Promise((resolve, reject) => {
  97. if (this.selectType === 'custom') {
  98. const query = {
  99. ids: this.ableIds
  100. }
  101. getGroupCondition(query).then(res => {
  102. resolve(res.data || [])
  103. }).catch(error => {
  104. reject(error)
  105. })
  106. } else {
  107. baseStore.getGroupList().then(res => {
  108. resolve(res || [])
  109. })
  110. }
  111. })
  112. },
  113. setNullValue() {
  114. this.innerValue = '';
  115. this.selectedData = [];
  116. },
  117. openSelect() {
  118. if (this.disabled) return
  119. this.selectShow = true
  120. },
  121. handleConfirm(selectedData, selectedIds) {
  122. if (!this.multiple) {
  123. this.$emit('update:modelValue', selectedIds[0])
  124. this.$emit('change', selectedIds[0], selectedData[0])
  125. } else {
  126. this.$emit('update:modelValue', selectedIds)
  127. this.$emit('change', selectedIds, selectedData)
  128. }
  129. },
  130. handleClose() {
  131. this.selectShow = false
  132. }
  133. }
  134. }
  135. </script>