index.vue 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <template>
  2. <app-remote-select v-model="selectedValue" :options="dataList"
  3. :option-props="{ label: 'goodsName', value: 'goodsId' }" :remote-method="remoteMethod" @change="onChange"
  4. @reset="onReset">
  5. <template #default="{ option }">
  6. <div style="display: flex;justify-content: space-between;">
  7. <span>{{ option.goodsCode }}</span>
  8. <span>{{ option.goodsName }}</span>
  9. </div>
  10. </template>
  11. </app-remote-select>
  12. </template>
  13. <script lang="ts" setup>
  14. import { computed, toRaw } from 'vue'
  15. import { useRequest } from '@/hooks/request'
  16. import { queryGoodsSelect } from '@/services/api/common'
  17. import AppRemoteSelect from '@pc/components/base/remote-select/index.vue'
  18. const props = defineProps({
  19. modelValue: [Number, String],
  20. marketId: Number,
  21. status: String
  22. })
  23. const emit = defineEmits(['update:modelValue', 'change'])
  24. const selectedValue = computed({
  25. get: () => props.modelValue,
  26. set: (val) => emit('update:modelValue', val)
  27. })
  28. const { dataList, loading, run } = useRequest(queryGoodsSelect, {
  29. params: {
  30. goodsStatusIds: props.status
  31. }
  32. })
  33. const onReset = () => {
  34. if (!loading.value) {
  35. selectedValue.value = undefined
  36. run({
  37. marketId: props.marketId
  38. })
  39. }
  40. }
  41. const remoteMethod = (query: string) => {
  42. if (!loading.value) {
  43. if (query) {
  44. run({
  45. marketId: props.marketId,
  46. searchCode: query
  47. })
  48. } else if (!dataList.value.length) {
  49. onReset()
  50. }
  51. }
  52. }
  53. const onChange = (value?: number) => {
  54. const item = dataList.value.find((e) => e.goodsId === value)
  55. emit('change', toRaw(item))
  56. }
  57. </script>