Table.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <!--
  2. A data table for the parameters page
  3. -->
  4. <template>
  5. <div class="container">
  6. <div class="d-flex flex-row mb-2">
  7. <h4 v-if="title" class="align-self-center">
  8. {{ title }}
  9. </h4>
  10. <div class="flex-grow-1" />
  11. <v-btn
  12. v-if="smAndUp && actions.includes(TABLE_ACTION.ADD)"
  13. prepend-icon="fa fa-plus"
  14. class="theme-neutral"
  15. @click="emit('addClicked')"
  16. >
  17. {{ i18n.t('add') }}
  18. </v-btn>
  19. </div>
  20. <v-table>
  21. <thead>
  22. <tr>
  23. <td v-for="col in columns">
  24. {{ col.label }}
  25. </td>
  26. <td>{{ i18n.t('actions') }}</td>
  27. </tr>
  28. </thead>
  29. <tbody v-if="items">
  30. <tr
  31. v-for="(item, i) in items"
  32. :key="i"
  33. >
  34. <td
  35. v-for="col in columnsDefinitions"
  36. class="cycle-editable-cell"
  37. >
  38. {{ item[col.property] }}
  39. </td>
  40. <td class="d-flex flex-row actions-cell">
  41. <slot name="actions" :item="item">
  42. <v-btn
  43. v-if="actions.includes(TABLE_ACTION.EDIT)"
  44. :flat="true"
  45. icon="fa fa-pen"
  46. class="mr-3"
  47. @click="emit('editClicked', item)"
  48. />
  49. <v-btn
  50. v-if="actions.includes(TABLE_ACTION.DELETE)"
  51. :flat="true"
  52. icon="fas fa-trash"
  53. @click="emit('deleteClicked', item)"
  54. />
  55. </slot>
  56. </td>
  57. </tr>
  58. </tbody>
  59. <tbody v-else>
  60. <tr class="theme-neutral">
  61. <td>
  62. <i>{{ i18n.t('nothing_to_show') }}</i>
  63. </td>
  64. <td></td>
  65. </tr>
  66. </tbody>
  67. </v-table>
  68. <div class="d-flex justify-end my-3" v-if="xs && actions.includes(TABLE_ACTION.ADD)">
  69. <v-btn
  70. prepend-icon="fa fa-plus"
  71. class="theme-neutral"
  72. @click="emit('addClicked')"
  73. >
  74. {{ i18n.t('add') }}
  75. </v-btn>
  76. </div>
  77. </div>
  78. </template>
  79. <script setup lang="ts">
  80. import {TABLE_ACTION} from '~/types/enum/enums';
  81. import type {ColumnDefinition} from '~/types/interfaces';
  82. import {useDisplay} from 'vuetify';
  83. const props = defineProps({
  84. /**
  85. * Array of objects to display in the table
  86. */
  87. items: {
  88. type: Array as PropType<Array<object>>,
  89. required: true,
  90. },
  91. /** Titre du tableau */
  92. title: {
  93. type: String,
  94. required: false,
  95. default: null
  96. },
  97. /**
  98. * If provided, define the columns to show.
  99. * Else, all the entity's props are shown.
  100. *
  101. * Ex: [
  102. * { property: 'id', label : 'Identifier'},
  103. * { property: 'name', label : 'Full name'},
  104. * ]
  105. */
  106. columnsDefinitions: {
  107. type: Array as PropType<Array<ColumnDefinition> | null>,
  108. required: false,
  109. default: null
  110. },
  111. /**
  112. * The property used as identifier (required by 'edition' link)
  113. */
  114. identifier: {
  115. type: String,
  116. required: false,
  117. default: 'id'
  118. },
  119. /**
  120. * List of the actions available for each record
  121. */
  122. actions: {
  123. type: Array as PropType<Array<TABLE_ACTION>>,
  124. required: false,
  125. default: [TABLE_ACTION.EDIT, TABLE_ACTION.DELETE, TABLE_ACTION.ADD]
  126. },
  127. /**
  128. * The URL for the edit / create pages
  129. * The resulting url will be constructed this way :
  130. *
  131. * Edition : {baseUrl}/{id}
  132. * Creation : {baseUrl}/new
  133. */
  134. actionsRoute: {
  135. type: String,
  136. required: false,
  137. default: '/parameters'
  138. }
  139. })
  140. const i18n = useI18n()
  141. const emit = defineEmits(['editClicked', 'deleteClicked', 'addClicked'])
  142. const { smAndUp, xs } = useDisplay()
  143. const getId = (item: object) => {
  144. return item[props.identifier]
  145. }
  146. const columns: ComputedRef<Array<ColumnDefinition>> = computed(() => {
  147. return props.columnsDefinitions.map(col => {
  148. return {
  149. property: col.property,
  150. label: col.label ?? i18n.t(col.property)
  151. }
  152. })
  153. })
  154. </script>
  155. <style scoped lang="scss">
  156. .v-table {
  157. width: 100%;
  158. thead {
  159. color: rgb(var(--v-theme-neutral-strong));
  160. font-weight: 600;
  161. td {
  162. border-bottom: thin solid rgba(var(--v-border-color), var(--v-border-opacity));
  163. }
  164. td:last-of-type {
  165. padding-left: 30px;
  166. }
  167. }
  168. th, td {
  169. padding: 10px;
  170. text-align: left;
  171. }
  172. td:last-of-type {
  173. width: 125px;
  174. }
  175. }
  176. :deep(.actions-cell .v-icon) {
  177. color: rgb(var(--v-theme-neutral-strong));
  178. font-size: 18px;
  179. }
  180. </style>