DefaultTable.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <template>
  2. <q-table
  3. v-model:fullscreen="fullscreen"
  4. flat
  5. :pagination="{ rowsPerPage }"
  6. :pagination-label="getPaginationLabel"
  7. row-key="id"
  8. :rows="rows"
  9. :rows-per-page-label="$t('common.ui.table.rows_per_page')"
  10. :columns="columns"
  11. :visible-columns="visibleColumns"
  12. :filter="filter"
  13. :grid="$q.screen.lt.sm"
  14. :loading="loading"
  15. class="softpar-table q-pa-sm"
  16. @row-click="onRowClick"
  17. >
  18. <template #top>
  19. <div
  20. class="flex full-width justify-between align-center q-mb-md q-pl-sm"
  21. style="gap: 1rem"
  22. >
  23. <q-input
  24. v-if="showSearchField"
  25. v-model="filter"
  26. debounce="250"
  27. :placeholder="$t('common.actions.search')"
  28. clearable
  29. autofocus
  30. class=""
  31. color="primary"
  32. >
  33. <template #append>
  34. <q-icon name="mdi-magnify" />
  35. </template>
  36. </q-input>
  37. <q-select
  38. v-if="showColumnsSelect"
  39. v-model="visibleColumns"
  40. multiple
  41. options-outlined
  42. :display-value="$q.lang.table.columns"
  43. emit-value
  44. map-options
  45. :options="mapColumns"
  46. style="width: 150px"
  47. options-selected-class="text-bold"
  48. />
  49. <q-space />
  50. <q-btn
  51. v-if="addItem"
  52. color="primary"
  53. padding="10px 16px"
  54. :outline="outlineAdd"
  55. :label="$t('common.actions.add')"
  56. @click="onAddItem"
  57. >
  58. </q-btn>
  59. </div>
  60. </template>
  61. <template #body-cell-actions="{ row }">
  62. <q-td v-if="deleteFunction">
  63. <q-item-section>
  64. <q-btn
  65. color="negative"
  66. flat
  67. dense
  68. icon="mdi-delete"
  69. style="width: 45px"
  70. class="q-ml-auto q-mr-sm"
  71. @click.prevent.stop="onDelete(row.id)"
  72. />
  73. </q-item-section>
  74. </q-td>
  75. </template>
  76. <template #loading>
  77. <q-inner-loading showing color="primary" />
  78. </template>
  79. <template v-if="!hideNoDataLabel" #no-data>
  80. <div v-if="!loading" class="q-my-md row justify-center full-width">
  81. <div class="q-pa-md body2">
  82. {{ $t("http.errors.no_records_found") }}
  83. </div>
  84. </div>
  85. </template>
  86. <template v-for="name in $slots" #[name]="data">
  87. <slot :name="name" v-bind="data"></slot>
  88. </template>
  89. </q-table>
  90. </template>
  91. <script setup>
  92. import { ref, onMounted, toRaw, watch } from "vue";
  93. import { useRouter } from "vue-router";
  94. const emit = defineEmits(["onRowClick", "onAddItem", "noRows"]);
  95. const {
  96. columns,
  97. apiCall,
  98. outlineAdd,
  99. openItem,
  100. openItemRoute,
  101. addItem,
  102. addItemRoute,
  103. rowsPerPage,
  104. showSearchField,
  105. noApiCall,
  106. hideNoDataLabel,
  107. deleteFunction,
  108. } = defineProps({
  109. columns: {
  110. type: Array,
  111. required: true,
  112. },
  113. apiCall: {
  114. type: Function,
  115. required: true,
  116. },
  117. outlineAdd: {
  118. type: Boolean,
  119. default: false,
  120. },
  121. openItem: {
  122. type: Boolean,
  123. default: false,
  124. },
  125. openItemRoute: {
  126. type: String,
  127. default: "",
  128. },
  129. addItem: {
  130. type: Boolean,
  131. default: true,
  132. },
  133. addItemRoute: {
  134. type: String,
  135. default: "",
  136. },
  137. rowsPerPage: {
  138. type: Number,
  139. default: 10,
  140. },
  141. showSearchField: {
  142. type: Boolean,
  143. default: true,
  144. },
  145. showColumnsSelect: {
  146. type: Boolean,
  147. default: true,
  148. },
  149. noApiCall: {
  150. type: Boolean,
  151. default: false,
  152. },
  153. hideNoDataLabel: {
  154. type: Boolean,
  155. default: false,
  156. },
  157. deleteFunction: {
  158. type: Function,
  159. default: null,
  160. },
  161. });
  162. const router = useRouter();
  163. const rows = ref([]);
  164. const filter = ref("");
  165. const loading = ref(true);
  166. const fullscreen = ref(false);
  167. const showInativos = ref(false);
  168. const inativos = ref([]);
  169. const getPaginationLabel = (from, to, last) => {
  170. return `${from}-${to} de ${last}`;
  171. };
  172. watch(showInativos, () => {
  173. if (showInativos.value) {
  174. rows.value = rows.value.concat(inativos.value);
  175. } else {
  176. inativos.value = rows.value.filter(
  177. (row) => row.status === false || row.ativo === false,
  178. );
  179. rows.value = rows.value.filter((row) => row.ativo);
  180. }
  181. });
  182. watch(
  183. () => apiCall,
  184. async () => {
  185. await onRequest();
  186. },
  187. );
  188. const mapColumns = columns.reduce((accm, column) => {
  189. if (!column.required) {
  190. accm.push({
  191. label: column.label,
  192. value: column.name,
  193. });
  194. }
  195. return accm;
  196. }, []);
  197. const visibleColumns = ref(mapColumns.map((column) => column.value));
  198. const onRowClick = (evt, row, index) => {
  199. const item = toRaw(row);
  200. if (openItem) {
  201. if (openItemRoute) {
  202. router.push({ name: openItemRoute, params: { id: item.id } });
  203. } else {
  204. emit("onRowClick", { evt, row, index });
  205. }
  206. }
  207. };
  208. const onAddItem = () => {
  209. if (addItem) {
  210. if (addItemRoute) {
  211. router.push({ name: addItemRoute });
  212. } else {
  213. emit("onAddItem");
  214. }
  215. }
  216. };
  217. const onDelete = async (id) => {
  218. if (deleteFunction) {
  219. loading.value = true;
  220. try {
  221. await deleteFunction(id);
  222. await onRequest();
  223. } catch (error) {
  224. console.error(error);
  225. } finally {
  226. loading.value = false;
  227. }
  228. }
  229. };
  230. const onRequest = async () => {
  231. if (noApiCall) {
  232. loading.value = false;
  233. return;
  234. }
  235. loading.value = true;
  236. const response = await apiCall();
  237. rows.value.splice(0, rows.value.length, ...response);
  238. if (rows.value.length == 0) {
  239. emit("noRows");
  240. }
  241. loading.value = false;
  242. };
  243. onMounted(async () => {
  244. await onRequest({
  245. filter: undefined,
  246. });
  247. });
  248. defineExpose({
  249. refresh: onRequest,
  250. });
  251. </script>
  252. <style lang="scss">
  253. @import "src/css/table.scss";
  254. </style>