index.vue 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <template>
  2. <div class="el-form-item--col">
  3. <el-select ref="selectRef" v-model="selectedValue" :empty-values="[null, undefined, 0]" :disabled="disabled"
  4. :loading="loading" :placeholder="placeholder" :remote-method="remoteMethod" @change="onChange" remote
  5. filterable clearable>
  6. <el-option v-for="item in dataList" :key="item.userid" :label="item.accountname" :value="item.userid">
  7. <div style="display: flex;justify-content: space-between;">
  8. <span>{{ item.userid }}</span>
  9. <span>{{ item.accountname }}</span>
  10. </div>
  11. </el-option>
  12. </el-select>
  13. <!-- <el-button icon="Plus" link @click="onPlus" /> -->
  14. </div>
  15. </template>
  16. <script lang="ts" setup>
  17. import { shallowRef, computed, toRaw } from 'vue'
  18. import { ElMessage } from 'element-plus'
  19. import { useRequest } from '@/hooks/request'
  20. import { queryOrganSelect } from '@/services/api/common'
  21. const props = defineProps({
  22. modelValue: [Number, String],
  23. usertype: String,
  24. disabled: Boolean,
  25. placeholder: {
  26. type: String,
  27. default: '请输入'
  28. }
  29. })
  30. const emit = defineEmits(['update:modelValue', 'change'])
  31. const selectRef = shallowRef()
  32. const selectedValue = computed({
  33. get: () => props.modelValue,
  34. set: (val) => emit('update:modelValue', val)
  35. })
  36. const { dataList, loading, runAsync } = useRequest(queryOrganSelect, {
  37. manual: true
  38. })
  39. const onPlus = () => {
  40. if (!loading.value) {
  41. loading.value = true
  42. runAsync({
  43. usertype: props.usertype
  44. }).then((res) => {
  45. if (res.data.length) {
  46. selectRef.value.toggleMenu()
  47. } else {
  48. ElMessage.warning('暂无数据')
  49. }
  50. }).catch((err) => {
  51. ElMessage.error(err)
  52. }).finally(() => {
  53. loading.value = false
  54. })
  55. }
  56. }
  57. const remoteMethod = (query: string) => {
  58. if (query) {
  59. loading.value = true
  60. runAsync({
  61. usertype: props.usertype,
  62. searchcode: query
  63. }).finally(() => {
  64. loading.value = false
  65. })
  66. }
  67. }
  68. const onChange = (id?: number) => {
  69. const item = dataList.value.find((e) => e.userid === id)
  70. dataList.value = []
  71. emit('change', toRaw(item))
  72. }
  73. </script>
  74. <style lang="less">
  75. @import './index.less';
  76. </style>