Table.vue 5.4 KB

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