Table.vue 5.2 KB

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