index.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <template>
  2. <view class="jnpf-group-select">
  3. <selectBox v-model="innerValue" :placeholder="placeholder" @openSelect="openSelect" :select-open="selectShow">
  4. </selectBox>
  5. <Tree v-model="selectShow" :options="options" :multiple="multiple" :props="props" :selectedData="selectedData"
  6. :selectId="!multiple ? [modelValue] : modelValue" @close="handleClose" @confirm="handleConfirm" />
  7. </view>
  8. </template>
  9. <script>
  10. import Tree from './Tree.vue';
  11. import selectBox from '@/components/selectBox'
  12. import {
  13. getGroupCondition
  14. } from '@/api/common.js'
  15. import {
  16. useBaseStore
  17. } from '@/store/modules/base'
  18. const baseStore = useBaseStore()
  19. export default {
  20. name: 'jnpf-group-select',
  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. options: [],
  64. selectedData: [],
  65. allList: [],
  66. }
  67. },
  68. watch: {
  69. modelValue: {
  70. handler(val) {
  71. this.getOptions()
  72. },
  73. immediate: true
  74. }
  75. },
  76. methods: {
  77. getOptions() {
  78. this.selectType === 'custom' ? this.getGroupCondition() : this.getAllOptions()
  79. },
  80. setDefault() {
  81. if (!this.modelValue || !this.modelValue.length) return this.setNullValue();
  82. this.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. this.selectedData.push(this.allList[j])
  88. break inner
  89. }
  90. }
  91. }
  92. this.innerValue = this.selectedData.map(o => o.fullName).join();
  93. },
  94. async getAllOptions() {
  95. this.options = await baseStore.getGroupTree()
  96. this.allList = await this.treeToArray()
  97. this.setDefault()
  98. },
  99. getGroupCondition() {
  100. let query = {
  101. ids: this.ableIds
  102. }
  103. getGroupCondition(query).then(res => {
  104. this.options = res.data.list || [];
  105. this.allList = this.treeToArray()
  106. this.setDefault()
  107. })
  108. },
  109. setNullValue() {
  110. this.innerValue = '';
  111. this.selectedData = [];
  112. },
  113. treeToArray() {
  114. let options = JSON.parse(JSON.stringify(this.options))
  115. let list = []
  116. const loop = (options) => {
  117. for (let i = 0; i < options.length; i++) {
  118. const item = options[i]
  119. list.push(item)
  120. if (item.children && Array.isArray(item.children)) loop(item.children)
  121. }
  122. }
  123. loop(options)
  124. return list
  125. },
  126. openSelect() {
  127. if (this.disabled) return
  128. this.selectShow = true
  129. },
  130. handleConfirm(e, selectId) {
  131. if (!this.multiple) {
  132. this.$emit('update:modelValue', selectId[0])
  133. this.$emit('change', selectId[0], e[0])
  134. } else {
  135. this.$emit('update:modelValue', selectId)
  136. this.$emit('change', selectId, e)
  137. }
  138. },
  139. handleClose() {
  140. this.selectShow = false
  141. }
  142. }
  143. }
  144. </script>
  145. <style lang="scss" scoped>
  146. .jnpf-group-select {
  147. width: 100%;
  148. }
  149. </style>