searchPeople.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <el-select
  3. v-model="searchValue"
  4. filterable
  5. :clearable="clearable"
  6. remote
  7. :multiple="multiple"
  8. :remote-method="remoteMethod"
  9. :loading="loading"
  10. :size="size"
  11. :placeholder="placeholder"
  12. :disabled="disabled"
  13. @change="changeSelect"
  14. >
  15. <el-option
  16. v-for="item in options"
  17. :key="item.idap"
  18. :label="item.name"
  19. :value="item.idap"
  20. @click.native="$emit('memberData', item)"
  21. >
  22. <div class="item-style">
  23. <div class="item-detail">{{ item.deptName }}</div>
  24. <div style="min-width:80px">{{ item.name }}</div>
  25. <div class="item-detail">{{ item.idap }}</div>
  26. </div>
  27. </el-option>
  28. </el-select>
  29. </template>
  30. <script>
  31. import { memberQueryMemberInfoByIDAPorName } from '@/api/projectIndex'
  32. export default {
  33. props: {
  34. value: {
  35. type: [String, Array, Object],
  36. default: '',
  37. required: false
  38. },
  39. placeholder: {
  40. type: String,
  41. default: '请输入姓名或邮箱前缀',
  42. required: false
  43. },
  44. size: {
  45. type: String,
  46. default: 'small',
  47. required: false
  48. },
  49. multiple: {
  50. type: Boolean,
  51. default: false,
  52. required: false
  53. },
  54. disabled: {
  55. type: Boolean,
  56. default: false,
  57. required: false
  58. },
  59. clearable: { // 是否支持清空
  60. type: Boolean,
  61. default: true,
  62. required: false
  63. }
  64. },
  65. data() {
  66. return {
  67. searchValue: this.value,
  68. loading: false,
  69. memberObj: {},
  70. options: [],
  71. firstGetArr: true // 是否第一次获取数组
  72. }
  73. },
  74. watch: {
  75. value: {
  76. handler(newV, oldV) {
  77. if (this.searchValue === newV) {
  78. return
  79. }
  80. if (newV === null) {
  81. this.multiple ? this.searchValue = [] : this.searchValue = ''
  82. } else {
  83. this.searchValue = newV
  84. }
  85. const type = Object.prototype.toString.call(this.searchValue)
  86. if (type.indexOf('Array') < 0) {
  87. this.remoteMethod(this.searchValue)
  88. } else if (type.indexOf('Array') > 0 && this.searchValue.length > 0 && this.firstGetArr) {
  89. this.initMore(this.searchValue)
  90. this.firstGetArr = false
  91. }
  92. },
  93. immediate: true
  94. }
  95. },
  96. mounted() {
  97. this.initMore(this.searchValue)
  98. },
  99. methods: {
  100. remoteMethod(query) {
  101. query !== '' ? this.getMember(query) : this.options = []
  102. },
  103. initMore(arr) { // 当多人时候,对数组每一个人员进行搜索
  104. for (const item of arr) {
  105. this.getMember(item, true)
  106. }
  107. },
  108. async getMember(query, initMore = false) {
  109. const res = await memberQueryMemberInfoByIDAPorName({ memberIDAP: query })
  110. if (res.data === null) {
  111. return
  112. }
  113. this.loading = false
  114. const weakMap = new Map()
  115. for (const item of res.data) {
  116. if (!weakMap.has(item.idap)) {
  117. weakMap.set(item.idap, item)
  118. }
  119. }
  120. initMore ? this.options = [...this.options, ...weakMap.values()] : this.options = [...weakMap.values()]
  121. },
  122. changeSelect(e) {
  123. if (this.searchValue === [] || this.searchValue === '') {
  124. this.searchValue = null
  125. }
  126. this.$emit('update:value', this.searchValue)
  127. if (this.multiple) {
  128. this.$emit('change', this.searchValue)
  129. } else {
  130. const confirm = this.options.find(item => item.idap === e)
  131. this.$emit('change', confirm)
  132. }
  133. }
  134. }
  135. }
  136. </script>
  137. <style scoped lang="scss">
  138. .item-style {
  139. display: flex;
  140. justify-content: flex-start;
  141. .item-detail {
  142. min-width:100px;
  143. color: #8492a6;
  144. font-size: 13px;
  145. overflow:hidden
  146. }
  147. }
  148. </style>