AutocompleteWithAPI.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. :isLoading="isLoading"
  14. :item-text="itemText"
  15. :slotText="slotText"
  16. :item-value="itemValue"
  17. :multiple="multiple"
  18. :chips="chips"
  19. prependIcon="mdi-magnify"
  20. :return-object="returnObject"
  21. @research="search"
  22. :no-filter="noFilter"
  23. @update="$emit('update', $event, field)"
  24. />
  25. </main>
  26. </template>
  27. <script setup lang="ts">
  28. import {Ref, ref, toRefs} from "@vue/reactivity";
  29. import UrlUtils from "~/services/utils/urlUtils";
  30. import {useFetch} from "#app";
  31. import {watch} from "@vue/runtime-core";
  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 = { method: 'GET', query: {key: 'id', value: ids.join(',')} }
  107. useFetch(async () => {
  108. isLoading.value = true
  109. const r: any = await $fetch(props.remoteUrl, options)
  110. isLoading.value = false
  111. remoteData.value = r.data
  112. items.value = r.data
  113. })
  114. }
  115. const search = async (research:string) => {
  116. isLoading.value = true
  117. const func: Function = props.searchFunction
  118. items.value = items.value.concat(await func(research, props.field))
  119. isLoading.value = false
  120. }
  121. const unwatch = watch(data,(d) => {
  122. items.value = props.multiple ? d : [d]
  123. })
  124. onUnmounted(() => {
  125. unwatch()
  126. })
  127. </script>