index.vue 2.2 KB

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