DataTable.vue 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <!--
  2. Tableau interactif conçu pour l'affichage d'une collection d'entités
  3. @see https://vuetifyjs.com/en/components/data-tables/
  4. -->
  5. <template>
  6. <v-col cols="12" sm="12">
  7. <v-data-table
  8. :headers="headersWithItem"
  9. :items="collection.items"
  10. :server-items-length="collection.pagination.totalItems"
  11. :loading="$fetchState.pending"
  12. class="elevation-1"
  13. >
  14. <template v-for="(header, index) in headersWithItem" :key="index" #[header.item]="props">
  15. <slot :name="header.item" v-bind="props">
  16. {{ props.item[header.value] }}
  17. </slot>
  18. </template>
  19. <template #item.actions="{ item }">
  20. <v-icon small class="mr-2" @click="editItem(item)"> mdi-pencil </v-icon>
  21. <v-icon small @click="deleteItem(item)"> mdi-delete </v-icon>
  22. </template>
  23. </v-data-table>
  24. </v-col>
  25. </template>
  26. <script setup lang="ts">
  27. import { ref, toRefs } from 'vue'
  28. import type { Ref } from 'vue'
  29. import { useEntityFetch } from '~/composables/data/useEntityFetch'
  30. import type ApiResource from '~/models/ApiResource'
  31. import type { AnyJson } from '~/types/data'
  32. const props = defineProps({
  33. parent: {
  34. type: Object,
  35. required: true,
  36. },
  37. model: {
  38. type: Object,
  39. required: true,
  40. },
  41. headers: {
  42. type: Array,
  43. required: true,
  44. },
  45. })
  46. const { parent, model, headers } = toRefs(props)
  47. const headersWithItem = computed(() => {
  48. return headers.value.map((header: any) => {
  49. header.item = 'item.' + header.value
  50. return header
  51. })
  52. })
  53. const totalEntries: Ref<number> = ref(0)
  54. const entries: Ref<Array<AnyJson>> = ref(Array<AnyJson>())
  55. const { fetchCollection } = useEntityFetch()
  56. const { data: collection, pending } = await fetchCollection(
  57. model.value as typeof ApiResource,
  58. parent.value as ApiResource,
  59. )
  60. const itemId: Ref<number> = ref(0)
  61. const editItem = (item: AnyJson) => {
  62. itemId.value = item.id
  63. }
  64. </script>