Table.vue 5.3 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, 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 in items" :key="item.id">
  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 { useDisplay } from 'vuetify'
  92. import { TABLE_ACTION } from '~/types/enum/enums'
  93. import type { ColumnDefinition } from '~/types/interfaces'
  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
  179. rgba(var(--v-border-color), var(--v-border-opacity));
  180. }
  181. td:last-of-type {
  182. padding-left: 30px;
  183. }
  184. }
  185. :deep(tr:hover) {
  186. .fa-ellipsis-vertical {
  187. color: rgb(var(--v-theme-on-neutral-soft));
  188. font-size: 20px;
  189. }
  190. }
  191. th,
  192. td {
  193. padding: 10px;
  194. text-align: left;
  195. }
  196. td:last-of-type {
  197. width: 125px;
  198. }
  199. }
  200. .action-menu {
  201. margin: 0 auto;
  202. .v-list {
  203. top: 26px;
  204. left: 2px;
  205. padding: 0;
  206. }
  207. .v-icon {
  208. opacity: 0.7;
  209. font-size: 16px;
  210. margin-right: 12px;
  211. }
  212. }
  213. :deep(.actions-cell .v-icon) {
  214. color: rgb(var(--v-theme-on-neutral));
  215. font-size: 18px;
  216. }
  217. </style>