1
1
<script setup lang="ts">
2
- import { ref , type Ref , WritableComputedRef , watch , onMounted , getCurrentInstance , useSlots } from ' vue' ;
2
+ import { ref , type Ref , WritableComputedRef , watch , onMounted , getCurrentInstance , useSlots , computed } from ' vue' ;
3
3
import useHiveGrid from ' ./hooks/use-hive-grid' ;
4
4
import { CommonProps } from ' @/common/types/props' ;
5
5
import {
@@ -105,28 +105,26 @@ const { items, sort, deleteRow, itemsLength, addRow, isLoading } = useHiveGrid({
105
105
extensionFields: props .extensionFields ,
106
106
} as GridConfig );
107
107
108
- const arrayOfSplittedItems : Ref <Record <string , any >[][]> = ref ([]);
108
+ const pagginatedItems : Ref <Record <string , any >[][]> = ref ([]);
109
109
const currentPage = ref (props .page );
110
110
const prevPage = (page ? : number ) => {
111
111
if (currentPage .value === 1 ) return ;
112
112
page === undefined ? currentPage .value -- : (currentPage .value = page );
113
113
};
114
114
115
115
const nextPage = (page ? : number ) => {
116
- if (currentPage .value === arrayOfSplittedItems .value .length ) return ;
116
+ if (currentPage .value === pagginatedItems .value .length ) return ;
117
117
page === undefined ? currentPage .value ++ : (currentPage .value = page );
118
118
};
119
119
120
- const pagination = (items : WritableComputedRef <object []>) => {
121
- arrayOfSplittedItems .value .length = 0 ;
120
+ const paginateItems = (items : WritableComputedRef <object []>) => {
121
+ pagginatedItems .value .length = 0 ;
122
122
if (props .itemsOnPage === 0 ) return ;
123
123
for (let i = 0 ; i < items .value .length / props .itemsOnPage ; i ++ ) {
124
- arrayOfSplittedItems .value .push (
125
- items .value .slice (i * props .itemsOnPage , i * props .itemsOnPage + props .itemsOnPage ),
126
- );
124
+ pagginatedItems .value .push (items .value .slice (i * props .itemsOnPage , i * props .itemsOnPage + props .itemsOnPage ));
127
125
}
128
- if (arrayOfSplittedItems .value .length < currentPage .value ) {
129
- currentPage .value = arrayOfSplittedItems .value .length ;
126
+ if (pagginatedItems .value .length < currentPage .value ) {
127
+ currentPage .value = pagginatedItems .value .length ;
130
128
}
131
129
};
132
130
@@ -153,16 +151,16 @@ watch(currentQuery, () => {
153
151
});
154
152
155
153
onMounted (() => {
156
- pagination (items );
154
+ paginateItems (items );
157
155
});
158
156
159
157
watch (items , () => {
160
- pagination (items );
158
+ paginateItems (items );
161
159
if (itemsLength .value <= 1 ) currentPage .value = 1 ;
162
160
});
163
161
164
162
watch (itemsLength , () => {
165
- pagination (items );
163
+ paginateItems (items );
166
164
});
167
165
168
166
const slots = useSlots ();
@@ -173,6 +171,13 @@ const rowClicked = (row: Record<string, unknown>, rowRef: ComponentPublicInstanc
173
171
};
174
172
175
173
defineExpose ({ items , grid });
174
+
175
+ const currentPageItems = computed (() => {
176
+ if (pagginatedItems .value .length === 0 ) return items ;
177
+ if (currentPage .value <= 0 ) return pagginatedItems .value [0 ];
178
+ if (currentPage .value > pagginatedItems .value .length ) return pagginatedItems .value [pagginatedItems .value .length - 1 ];
179
+ return pagginatedItems .value [currentPage .value - 1 ];
180
+ });
176
181
</script >
177
182
178
183
<template >
@@ -189,7 +194,7 @@ defineExpose({ items, grid });
189
194
/>
190
195
<tbody >
191
196
<hive-grid-row
192
- v-for =" (item, index) in arrayOfSplittedItems.length === 0 ? items : arrayOfSplittedItems[currentPage - 1] "
197
+ v-for =" (item, index) in currentPageItems "
193
198
:key =" (item as any).id"
194
199
:index =" index"
195
200
:columns =" columns"
@@ -376,7 +381,7 @@ defineExpose({ items, grid });
376
381
</hive-grid-row >
377
382
</tbody >
378
383
</table >
379
- <div v-if =" arrayOfSplittedItems .length > 1" class =" pagination-item" >
384
+ <div v-if =" pagginatedItems .length > 1" class =" pagination-item" >
380
385
<svg
381
386
@click =" prevPage(1)"
382
387
xmlns =" http://www.w3.org/2000/svg"
@@ -423,7 +428,7 @@ defineExpose({ items, grid });
423
428
viewBox =" 0 0 30 30"
424
429
version =" 1.1"
425
430
class =" pagination-item_svg"
426
- :class =" { disabled: currentPage === arrayOfSplittedItems .length }"
431
+ :class =" { disabled: currentPage === pagginatedItems .length }"
427
432
>
428
433
<g id =" surface1" >
429
434
<path
@@ -433,15 +438,15 @@ defineExpose({ items, grid });
433
438
</g >
434
439
</svg >
435
440
<svg
436
- @click =" nextPage(arrayOfSplittedItems .length)"
441
+ @click =" nextPage(pagginatedItems .length)"
437
442
xmlns =" http://www.w3.org/2000/svg"
438
443
xmlns:xlink =" http://www.w3.org/1999/xlink"
439
444
width =" 30px"
440
445
height =" 30px"
441
446
viewBox =" 0 0 30 30"
442
447
version =" 1.1"
443
448
class =" pagination-item_svg"
444
- :class =" { disabled: currentPage === arrayOfSplittedItems .length }"
449
+ :class =" { disabled: currentPage === pagginatedItems .length }"
445
450
>
446
451
<g id =" surface1" >
447
452
<path
0 commit comments