AutocompleteWithAPI.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <!--
  2. Liste déroulante avec autocompletion (les données sont issues
  3. d'une api)
  4. @see https://vuetifyjs.com/en/components/autocompletes/#usage
  5. -->
  6. <template>
  7. <main>
  8. <UiInputAutocomplete
  9. :field="field"
  10. :label="label"
  11. :data="remoteData ? remoteData : data"
  12. :items="items"
  13. :is-loading="isLoading"
  14. :item-text="itemText"
  15. :slot-text="slotText"
  16. :item-value="itemValue"
  17. :multiple="multiple"
  18. :chips="chips"
  19. prepend-icon="mdi-magnify"
  20. :return-object="returnObject"
  21. :no-filter="noFilter"
  22. @research="search"
  23. @update="$emit('update', $event, field)"
  24. />
  25. </main>
  26. </template>
  27. <script setup lang="ts">
  28. import { ref, toRefs, watch } from 'vue'
  29. import type { Ref } from 'vue'
  30. import { useFetch } from '#app'
  31. import UrlUtils from '~/services/utils/urlUtils'
  32. const props = defineProps({
  33. label: {
  34. type: String,
  35. required: false,
  36. default: null,
  37. },
  38. field: {
  39. type: String,
  40. required: false,
  41. default: null,
  42. },
  43. searchFunction: {
  44. type: Function,
  45. required: true,
  46. },
  47. data: {
  48. type: [String, Number, Object, Array],
  49. required: false,
  50. default: null,
  51. },
  52. remoteUri: {
  53. type: [Array],
  54. required: false,
  55. default: null,
  56. },
  57. remoteUrl: {
  58. type: String,
  59. required: false,
  60. default: null,
  61. },
  62. readonly: {
  63. type: Boolean,
  64. required: false,
  65. },
  66. itemValue: {
  67. type: String,
  68. default: 'id',
  69. },
  70. itemTitle: {
  71. type: Array,
  72. required: true,
  73. },
  74. slotText: {
  75. type: Array,
  76. required: false,
  77. },
  78. returnObject: {
  79. type: Boolean,
  80. default: false,
  81. },
  82. noFilter: {
  83. type: Boolean,
  84. default: false,
  85. },
  86. multiple: {
  87. type: Boolean,
  88. default: false,
  89. },
  90. chips: {
  91. type: Boolean,
  92. default: false,
  93. },
  94. })
  95. const { data } = toRefs(props)
  96. const items = ref([])
  97. const remoteData: Ref<Array<string> | null> = ref(null)
  98. const isLoading = ref(false)
  99. if (props.data) {
  100. items.value = props.multiple ? (data.value ?? []) : [data.value]
  101. } else if (props.remoteUri) {
  102. const ids: Array<any> = []
  103. for (const uri of props.remoteUri) {
  104. ids.push(UrlUtils.extractIdFromUri(uri as string))
  105. }
  106. const options: FetchOptions = {
  107. method: 'GET',
  108. query: { key: 'id', value: ids.join(',') },
  109. }
  110. useFetch(async () => {
  111. isLoading.value = true
  112. const r: any = await $fetch(props.remoteUrl, options)
  113. isLoading.value = false
  114. remoteData.value = r.data
  115. items.value = r.data
  116. })
  117. }
  118. const search = async (research: string) => {
  119. isLoading.value = true
  120. const func: Function = props.searchFunction
  121. items.value = items.value.concat(await func(research, props.field))
  122. isLoading.value = false
  123. }
  124. const unwatch = watch(data, (d) => {
  125. items.value = props.multiple ? d : [d]
  126. })
  127. onUnmounted(() => {
  128. unwatch()
  129. })
  130. </script>