DataTable.vue 2.0 KB

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