DefaultTableServerSide.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. # DefaultTableServerSide.vue
  2. <template>
  3. <q-table
  4. v-model:fullscreen="fullscreen"
  5. v-model:pagination="pagination"
  6. row-key="id"
  7. flat
  8. class="softpar-table"
  9. :pagination-label="getPaginationLabel"
  10. :rows="rows"
  11. :rows-per-page-label="$t('common.ui.table.rows_per_page')"
  12. :columns="columns"
  13. :visible-columns="visibleColumns"
  14. :filter="pagination.filter"
  15. :grid="$q.screen.lt.sm"
  16. :loading="loading"
  17. v-bind="$attrs"
  18. @row-click="onRowClick"
  19. >
  20. <template #top>
  21. <div
  22. v-if="showSearchField || showColumnsSelect || addItem"
  23. class="flex full-width justify-between items-center q-mb-md q-pl-sm"
  24. style="gap: 1rem"
  25. >
  26. <q-input
  27. v-if="showSearchField"
  28. v-model="pagination.filter"
  29. outlined
  30. dense
  31. debounce="500"
  32. :placeholder="$t('common.actions.search')"
  33. clearable
  34. autofocus
  35. >
  36. <template #append>
  37. <q-icon name="mdi-magnify" />
  38. </template>
  39. </q-input>
  40. <q-select
  41. v-if="showColumnsSelect"
  42. v-model="visibleColumns"
  43. class="q-ml-md"
  44. multiple
  45. dense
  46. outlined
  47. options-outlined
  48. :display-value="$q.lang.table.columns"
  49. emit-value
  50. map-options
  51. :options="mapColumns"
  52. style="min-width: 150px"
  53. options-selected-class="text-bold"
  54. />
  55. <q-space />
  56. <q-btn
  57. v-if="addItem"
  58. color="primary"
  59. padding="12px 16px"
  60. :outline="outlineAdd"
  61. :label="labelAdd"
  62. @click="onAddItem"
  63. />
  64. </div>
  65. </template>
  66. <template #body-cell-actions="{ row }">
  67. <q-td v-if="deleteFunction">
  68. <q-item-section>
  69. <q-btn
  70. color="negative"
  71. flat
  72. dense
  73. icon="mdi-delete"
  74. style="width: 45px"
  75. class="q-ml-auto q-mr-sm"
  76. @click.prevent.stop="onDelete(row.id)"
  77. />
  78. </q-item-section>
  79. </q-td>
  80. </template>
  81. <template v-if="!hideNoDataLabel" #no-data>
  82. <div class="q-my-md row justify-center full-width">
  83. <q-spinner v-if="loading" color="primary" size="30px" />
  84. <div v-else class="q-pa-md body2">
  85. {{ $t("http.errors.no_records_found") }}
  86. </div>
  87. </div>
  88. </template>
  89. <template #bottom="scope">
  90. <div class="flex full-width justify-end">
  91. <div class="flex items-center">
  92. {{ $t("common.ui.table.rows_per_page") }}
  93. <q-select
  94. v-model="pagination.rowsPerPage"
  95. class="q-mx-sm"
  96. dense
  97. borderless
  98. :options="rowsPerPageOptions"
  99. >
  100. <template #option="selectData">
  101. <q-item v-bind="selectData.itemProps">
  102. <q-item-section>
  103. <q-item-label>{{
  104. selectData.opt == 0 ? $t("common.ui.misc.all") : selectData.opt
  105. }}</q-item-label>
  106. </q-item-section>
  107. </q-item>
  108. </template>
  109. </q-select>
  110. </div>
  111. <div class="flex items-center">
  112. {{ pagination.from + "-" + pagination.to }} {{ $t("common.ui.table.of") }}
  113. {{ pagination.rowsNumber }}
  114. </div>
  115. <div class="flex items-center">
  116. <q-btn
  117. icon="mdi-chevron-left"
  118. color="grey-8"
  119. round
  120. dense
  121. flat
  122. :disable="scope.isFirstPage"
  123. @click="prevPage"
  124. />
  125. <q-btn
  126. icon="mdi-chevron-right"
  127. color="grey-8"
  128. round
  129. dense
  130. flat
  131. :disable="scope.isLastPage"
  132. @click="nextPage"
  133. />
  134. </div>
  135. </div>
  136. </template>
  137. <template v-for="name in $slots" #[name]="data">
  138. <slot :name="name" v-bind="data"></slot>
  139. </template>
  140. </q-table>
  141. </template>
  142. <script setup>
  143. import { ref, computed, onMounted, toRaw, watch } from "vue";
  144. import { useI18n } from "vue-i18n";
  145. import { useRouter } from "vue-router";
  146. const emit = defineEmits([
  147. "onRowClick",
  148. "onAddItem",
  149. "noRows",
  150. "togglePrincipal",
  151. ]);
  152. const {
  153. columns,
  154. apiCall,
  155. outlineAdd,
  156. openItem,
  157. openItemRoute,
  158. addItem,
  159. addItemRoute,
  160. rowsPerPage,
  161. showSearchField,
  162. hideNoDataLabel,
  163. deleteFunction,
  164. } = defineProps({
  165. columns: {
  166. type: Array,
  167. required: true,
  168. },
  169. apiCall: {
  170. type: Function,
  171. required: true,
  172. },
  173. labelAdd: {
  174. type: String,
  175. default: "Adicionar",
  176. },
  177. outlineAdd: {
  178. type: Boolean,
  179. default: false,
  180. },
  181. openItem: {
  182. type: Boolean,
  183. default: false,
  184. },
  185. openItemRoute: {
  186. type: String,
  187. default: "",
  188. },
  189. addItem: {
  190. type: Boolean,
  191. default: true,
  192. },
  193. addItemRoute: {
  194. type: String,
  195. default: "",
  196. },
  197. rowsPerPage: {
  198. type: Number,
  199. default: 5,
  200. },
  201. showColumnsSelect: {
  202. type: Boolean,
  203. default: false,
  204. },
  205. showSearchField: {
  206. type: Boolean,
  207. default: true,
  208. },
  209. noApiRoute: {
  210. type: Boolean,
  211. default: false,
  212. },
  213. hideNoDataLabel: {
  214. type: Boolean,
  215. default: false,
  216. },
  217. deleteFunction: {
  218. type: Function,
  219. default: null,
  220. },
  221. });
  222. const { t } = useI18n();
  223. const router = useRouter();
  224. const rows = ref([]);
  225. const loading = ref(true);
  226. const fullscreen = ref(false);
  227. const rowsPerPageOptions = [5, 10];
  228. const pagination = ref({
  229. filter: undefined,
  230. page: 1,
  231. rowsPerPage: rowsPerPage,
  232. rowsNumber: 0,
  233. from: 0,
  234. to: 0,
  235. });
  236. const mapColumns = computed(() => {
  237. return columns.reduce((accm, column) => {
  238. if (!column.required) {
  239. accm.push({
  240. label: column.label.toUpperCase(),
  241. value: column.name,
  242. });
  243. }
  244. return accm;
  245. }, []);
  246. });
  247. const visibleColumns = ref(mapColumns.value.map((column) => column.value));
  248. const onRowClick = (evt, row, index) => {
  249. const item = toRaw(row);
  250. if (openItem) {
  251. if (openItemRoute) {
  252. router.push({ name: openItemRoute, params: { id: item.id } });
  253. } else {
  254. emit("onRowClick", { evt, row, index });
  255. }
  256. }
  257. };
  258. const onAddItem = () => {
  259. if (addItem) {
  260. if (addItemRoute) {
  261. router.push({ name: addItemRoute });
  262. } else {
  263. emit("onAddItem");
  264. }
  265. }
  266. };
  267. const onDelete = async (id) => {
  268. if (deleteFunction) {
  269. loading.value = true;
  270. try {
  271. await deleteFunction(id);
  272. await onRequest();
  273. } catch (error) {
  274. console.error(error);
  275. } finally {
  276. loading.value = false;
  277. }
  278. }
  279. };
  280. const prevPage = () => {
  281. pagination.value.page--;
  282. onRequest();
  283. };
  284. const nextPage = () => {
  285. pagination.value.page++;
  286. onRequest();
  287. };
  288. const getPaginationLabel = (from, to, total) => {
  289. return `${from}-${to} ${t?.("common.ui.table.of") ?? "of"} ${total}`;
  290. };
  291. let isFetching = false;
  292. const onRequest = async () => {
  293. if (isFetching) return;
  294. isFetching = true;
  295. loading.value = true;
  296. try {
  297. const response = await apiCall({
  298. page: pagination.value.page,
  299. perPage: pagination.value.rowsPerPage,
  300. filter: pagination.value.filter,
  301. });
  302. rows.value = response.data.result.data;
  303. pagination.value.rowsNumber = response.data.result.total;
  304. pagination.value.from = response.data.result.from;
  305. pagination.value.to = response.data.result.to;
  306. if (rows.value.length === 0) {
  307. emit("noRows");
  308. }
  309. } catch (error) {
  310. console.error("Error fetching data:", error);
  311. } finally {
  312. loading.value = false;
  313. isFetching = false;
  314. }
  315. };
  316. watch(
  317. () => apiCall,
  318. () => onRequest(),
  319. );
  320. watch(
  321. pagination,
  322. async (newVal, oldVal) => {
  323. if (!oldVal || loading.value) return;
  324. if (
  325. newVal.rowsPerPage !== oldVal.rowsPerPage ||
  326. newVal.filter !== oldVal.filter ||
  327. newVal.page !== oldVal.page
  328. ) {
  329. await onRequest();
  330. }
  331. },
  332. { deep: true },
  333. );
  334. onMounted(async () => {
  335. await onRequest();
  336. });
  337. defineExpose({
  338. refresh: onRequest,
  339. });
  340. </script>
  341. <style lang="scss">
  342. @import "src/css/table.scss";
  343. </style>