Table.vue 5.2 KB

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