index.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <template>
  2. <view class="jnpf-select">
  3. <selectBox v-model="innerValue" :disabled='disabled' :placeholder="placeholder" @openSelect="openSelect"
  4. :select-open="selectShow" v-if="isFlow" >
  5. </selectBox>
  6. <MultSelect :list="list" :show="selectShow" :value-name="props.value" :label-name="props.label"
  7. :defaultValue="defaultValue" @confirm="selectConfirm" @close="close" :filterable="filterable"
  8. :multiple="multiple" />
  9. </view>
  10. </template>
  11. <script>
  12. import MultSelect from '@/components/MultSelect'
  13. import selectBox from '@/components/selectBox'
  14. export default {
  15. name: 'jnpf-select',
  16. components: {
  17. MultSelect,
  18. selectBox
  19. },
  20. props: {
  21. modelValue: {
  22. type: [String, Number, Array],
  23. },
  24. options: {
  25. type: Array,
  26. default: () => []
  27. },
  28. props: {
  29. type: Object,
  30. default: () => ({
  31. label: 'fullName',
  32. value: 'id'
  33. })
  34. },
  35. placeholder: {
  36. type: String,
  37. default: '请选择'
  38. },
  39. multiple: {
  40. type: Boolean,
  41. default: false
  42. },
  43. disabled: {
  44. type: Boolean,
  45. default: false
  46. },
  47. filterable: {
  48. type: Boolean,
  49. default: false
  50. },
  51. isFlow: {
  52. type: Boolean,
  53. default: true
  54. },
  55. },
  56. data() {
  57. return {
  58. innerValue: '',
  59. defaultValue: [],
  60. selectShow: false,
  61. }
  62. },
  63. watch: {
  64. modelValue: {
  65. immediate: true,
  66. handler(val) {
  67. this.setDefault()
  68. },
  69. },
  70. options: {
  71. immediate: true,
  72. handler(val) {
  73. this.setDefault()
  74. },
  75. }
  76. },
  77. computed: {
  78. list() {
  79. return this.options
  80. }
  81. },
  82. methods: {
  83. openSelect() {
  84. if (this.disabled) return
  85. this.selectShow = true
  86. },
  87. selectConfirm(e) {
  88. if (this.multiple) {
  89. this.innerValue = e.label
  90. this.defaultValue = e.value
  91. this.$emit('update:modelValue', e.value || [])
  92. this.$emit('change', e.value || [], e.list || [])
  93. } else {
  94. if (!e.length) return
  95. const selectData = e[0]
  96. this.innerValue = selectData[this.props.label]
  97. this.defaultValue = [e[0][this.props.value]]
  98. this.$emit('update:modelValue', selectData[this.props.value])
  99. this.$emit('change', selectData[this.props.value], selectData)
  100. }
  101. },
  102. setDefault() {
  103. if (!this.options.length) return this.innerValue = ''
  104. if (this.multiple) {
  105. this.innerValue = ''
  106. this.defaultValue = []
  107. if (!this.modelValue || !this.modelValue.length) return
  108. this.defaultValue = this.modelValue
  109. for (let i = 0; i < this.options.length; i++) {
  110. const item = this.options[i]
  111. for (let j = 0; j < this.modelValue.length; j++) {
  112. const it = this.modelValue[j]
  113. if (item[this.props.value] == it) {
  114. if (!this.innerValue) {
  115. this.innerValue += item[this.props.label]
  116. } else {
  117. this.innerValue += ',' + item[this.props.label]
  118. }
  119. }
  120. }
  121. }
  122. } else {
  123. this.innerValue = ''
  124. this.defaultValue = []
  125. if (!this.modelValue && this.modelValue !== 0) return
  126. for (let i = 0; i < this.options.length; i++) {
  127. if (this.options[i][this.props.value] == this.modelValue) {
  128. this.defaultValue = [this.modelValue]
  129. this.innerValue = this.options[i][this.props.label]
  130. return
  131. }
  132. }
  133. }
  134. },
  135. close() {
  136. this.selectShow = false
  137. }
  138. }
  139. }
  140. </script>
  141. <style lang="scss">
  142. .jnpf-select {
  143. width: 100%;
  144. .u-input__content__field-wrapper__field {
  145. overflow: auto;
  146. }
  147. }
  148. </style>