1
1
<script setup>
2
- import { computed , ref , watch } from ' vue' ;
2
+ import { ref , watch } from ' vue' ;
3
3
4
4
const props = defineProps ({
5
5
data: {
@@ -18,30 +18,30 @@ const props = defineProps({
18
18
19
19
const currentPage = ref (1 );
20
20
21
- const totalItems = computed (( ) => props .data .length ) ;
22
- const totalPages = computed (( ) => Math .ceil (totalItems . value / props .itemsPerPage ) );
21
+ const getTotalItems = ( ) => props .data .length ;
22
+ const getTotalPages = ( ) => Math .ceil (getTotalItems () / props .itemsPerPage );
23
23
24
- const startIndex = computed (( ) => (currentPage .value - 1 ) * props .itemsPerPage ) ;
25
- const endIndex = computed (( ) => Math .min (startIndex . value + props .itemsPerPage , totalItems . value ));
24
+ const getStartIndex = ( ) => (currentPage .value - 1 ) * props .itemsPerPage ;
25
+ const getEndIndex = ( ) => Math .min (getStartIndex () + props .itemsPerPage , getTotalItems ( ));
26
26
27
27
const getNestedValue = (obj , path ) => {
28
28
return path .split (' .' ).reduce ((current , key ) =>
29
29
current ? current[key] : undefined , obj
30
30
);
31
31
};
32
32
33
- const paginatedData = computed ( () => {
34
- return props .data .slice (startIndex . value , endIndex . value );
35
- }) ;
33
+ const getPaginatedData = () => {
34
+ return props .data .slice (getStartIndex (), getEndIndex () );
35
+ };
36
36
37
- const displayedPages = computed ( () => {
37
+ const getDisplayedPages = () => {
38
38
const delta = 2 ;
39
39
const range = [];
40
40
const rangeWithDots = [];
41
41
let l;
42
42
43
- for (let i = 1 ; i <= totalPages . value ; i++ ) {
44
- if (i === 1 || i === totalPages . value ||
43
+ for (let i = 1 ; i <= getTotalPages () ; i++ ) {
44
+ if (i === 1 || i === getTotalPages () ||
45
45
(i >= currentPage .value - delta && i <= currentPage .value + delta)) {
46
46
range .push (i);
47
47
}
@@ -60,7 +60,7 @@ const displayedPages = computed(() => {
60
60
});
61
61
62
62
return rangeWithDots;
63
- }) ;
63
+ };
64
64
65
65
const previousPage = () => {
66
66
if (currentPage .value > 1 ) {
@@ -69,7 +69,7 @@ const previousPage = () => {
69
69
};
70
70
71
71
const nextPage = () => {
72
- if (currentPage .value < totalPages . value ) {
72
+ if (currentPage .value < getTotalPages () ) {
73
73
currentPage .value ++ ;
74
74
}
75
75
};
@@ -98,7 +98,7 @@ watch(() => props.data, () => {
98
98
</tr >
99
99
</thead >
100
100
<tbody class =" bg-white" >
101
- <tr v-for =" item in paginatedData "
101
+ <tr v-for =" item in getPaginatedData() "
102
102
:key =" item.id"
103
103
class =" hover:bg-gray-50 border-b border-gray-200" >
104
104
<td v-for =" column in columns"
@@ -120,21 +120,21 @@ watch(() => props.data, () => {
120
120
Anterior
121
121
</button >
122
122
<button @click =" nextPage"
123
- :disabled =" currentPage >= totalPages "
123
+ :disabled =" currentPage >= getTotalPages() "
124
124
class =" ml-3 relative inline-flex items-center px-4 py-2 border border-gray-300 text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50"
125
- :class =" { 'opacity-50 cursor-not-allowed': currentPage >= totalPages }" >
125
+ :class =" { 'opacity-50 cursor-not-allowed': currentPage >= getTotalPages() }" >
126
126
Siguiente
127
127
</button >
128
128
</div >
129
129
<div class =" hidden sm:flex-1 sm:flex sm:items-center sm:justify-between" >
130
130
<div >
131
131
<p class =" text-sm text-gray-700" >
132
132
Mostrando
133
- <span class =" font-medium" >{{ startIndex + 1 }}</span >
133
+ <span class =" font-medium" >{{ getStartIndex() + 1 }}</span >
134
134
a
135
- <span class =" font-medium" >{{ endIndex }}</span >
135
+ <span class =" font-medium" >{{ getEndIndex() }}</span >
136
136
de
137
- <span class =" font-medium" >{{ totalItems }}</span >
137
+ <span class =" font-medium" >{{ getTotalItems() }}</span >
138
138
resultados
139
139
</p >
140
140
</div >
@@ -149,7 +149,7 @@ watch(() => props.data, () => {
149
149
<path fill-rule =" evenodd" d =" M12.707 5.293a1 1 0 010 1.414L9.414 10l3.293 3.293a1 1 0 01-1.414 1.414l-4-4a1 1 0 010-1.414l4-4a1 1 0 011.414 0z" clip-rule =" evenodd" />
150
150
</svg >
151
151
</button >
152
- <button v-for =" page in displayedPages "
152
+ <button v-for =" page in getDisplayedPages() "
153
153
:key =" page"
154
154
@click =" goToPage(page)"
155
155
:class =" [
@@ -161,9 +161,9 @@ watch(() => props.data, () => {
161
161
{{ page }}
162
162
</button >
163
163
<button @click =" nextPage"
164
- :disabled =" currentPage >= totalPages "
164
+ :disabled =" currentPage >= getTotalPages() "
165
165
class =" relative inline-flex items-center px-2 py-2 rounded-r-md border border-gray-300 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50"
166
- :class =" { 'opacity-50 cursor-not-allowed': currentPage >= totalPages }" >
166
+ :class =" { 'opacity-50 cursor-not-allowed': currentPage >= getTotalPages() }" >
167
167
<span class =" sr-only" >Siguiente</span >
168
168
<svg class =" h-5 w-5" xmlns =" http://www.w3.org/2000/svg" viewBox =" 0 0 20 20" fill =" currentColor" >
169
169
<path fill-rule =" evenodd" d =" M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clip-rule =" evenodd" />
0 commit comments