DefaultTableServerSide.vue 7.9 KB

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