DataTable.vue 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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]="slotProps">
  15. <slot :name="header.item" v-bind="slotProps">
  16. {{ slotProps.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. interface TableHeader {
  48. value: string;
  49. item?: string;
  50. [key: string]: string | undefined;
  51. }
  52. const headersWithItem = computed(() => {
  53. return headers.value.map((header: TableHeader) => {
  54. header.item = 'item.' + header.value
  55. return header
  56. })
  57. })
  58. const totalEntries: Ref<number> = ref(0)
  59. const entries: Ref<Array<AnyJson>> = ref(Array<AnyJson>())
  60. const { fetchCollection } = useEntityFetch()
  61. const { data: collection, pending } = await fetchCollection(
  62. model.value as typeof ApiResource,
  63. parent.value as ApiResource,
  64. )
  65. const itemId: Ref<number> = ref(0)
  66. const editItem = (item: AnyJson) => {
  67. itemId.value = item.id
  68. }
  69. </script>