DataTable.vue 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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
  15. v-for="(header, index) in headersWithItem"
  16. :key="index"
  17. #[header.item]="slotProps"
  18. >
  19. <slot :name="header.item" v-bind="slotProps">
  20. {{ slotProps.item[header.value] }}
  21. </slot>
  22. </template>
  23. <template #item.actions="{ item }">
  24. <v-icon small class="mr-2" @click="editItem(item)"> mdi-pencil </v-icon>
  25. <v-icon small @click="deleteItem(item)"> mdi-delete </v-icon>
  26. </template>
  27. </v-data-table>
  28. </v-col>
  29. </template>
  30. <script setup lang="ts">
  31. import { ref, toRefs } from 'vue'
  32. import type { Ref } from 'vue'
  33. import { useEntityFetch } from '~/composables/data/useEntityFetch'
  34. import type ApiResource from '~/models/ApiResource'
  35. import type { AnyJson } from '~/types/data'
  36. const props = defineProps({
  37. parent: {
  38. type: Object,
  39. required: true,
  40. },
  41. model: {
  42. type: Object,
  43. required: true,
  44. },
  45. headers: {
  46. type: Array,
  47. required: true,
  48. },
  49. })
  50. const { parent, model, headers } = toRefs(props)
  51. interface TableHeader {
  52. value: string
  53. item?: string
  54. [key: string]: string | undefined
  55. }
  56. const headersWithItem = computed(() => {
  57. return headers.value.map((header: TableHeader) => {
  58. header.item = 'item.' + header.value
  59. return header
  60. })
  61. })
  62. const totalEntries: Ref<number> = ref(0)
  63. const entries: Ref<Array<AnyJson>> = ref(Array<AnyJson>())
  64. const { fetchCollection } = useEntityFetch()
  65. const { data: collection, pending } = await fetchCollection(
  66. model.value as typeof ApiResource,
  67. parent.value as ApiResource,
  68. )
  69. const itemId: Ref<number> = ref(0)
  70. const editItem = (item: AnyJson) => {
  71. itemId.value = item.id
  72. }
  73. </script>