Skip to content

Commit cd00cef

Browse files
committed
Grid pagination fix
1 parent 8ddcfca commit cd00cef

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vue3-hive-ui-kit",
33
"private": false,
4-
"version": "0.7.23",
4+
"version": "0.7.24",
55
"type": "module",
66
"description": "UI kit for Vue 3",
77
"files": [

src/components/hive-grid/hive-grid.vue

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<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';
33
import useHiveGrid from './hooks/use-hive-grid';
44
import { CommonProps } from '@/common/types/props';
55
import {
@@ -105,28 +105,26 @@ const { items, sort, deleteRow, itemsLength, addRow, isLoading } = useHiveGrid({
105105
extensionFields: props.extensionFields,
106106
} as GridConfig);
107107
108-
const arrayOfSplittedItems: Ref<Record<string, any>[][]> = ref([]);
108+
const pagginatedItems: Ref<Record<string, any>[][]> = ref([]);
109109
const currentPage = ref(props.page);
110110
const prevPage = (page?: number) => {
111111
if (currentPage.value === 1) return;
112112
page === undefined ? currentPage.value-- : (currentPage.value = page);
113113
};
114114
115115
const nextPage = (page?: number) => {
116-
if (currentPage.value === arrayOfSplittedItems.value.length) return;
116+
if (currentPage.value === pagginatedItems.value.length) return;
117117
page === undefined ? currentPage.value++ : (currentPage.value = page);
118118
};
119119
120-
const pagination = (items: WritableComputedRef<object[]>) => {
121-
arrayOfSplittedItems.value.length = 0;
120+
const paginateItems = (items: WritableComputedRef<object[]>) => {
121+
pagginatedItems.value.length = 0;
122122
if (props.itemsOnPage === 0) return;
123123
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));
127125
}
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;
130128
}
131129
};
132130
@@ -153,16 +151,16 @@ watch(currentQuery, () => {
153151
});
154152
155153
onMounted(() => {
156-
pagination(items);
154+
paginateItems(items);
157155
});
158156
159157
watch(items, () => {
160-
pagination(items);
158+
paginateItems(items);
161159
if (itemsLength.value <= 1) currentPage.value = 1;
162160
});
163161
164162
watch(itemsLength, () => {
165-
pagination(items);
163+
paginateItems(items);
166164
});
167165
168166
const slots = useSlots();
@@ -173,6 +171,13 @@ const rowClicked = (row: Record<string, unknown>, rowRef: ComponentPublicInstanc
173171
};
174172
175173
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+
});
176181
</script>
177182

178183
<template>
@@ -189,7 +194,7 @@ defineExpose({ items, grid });
189194
/>
190195
<tbody>
191196
<hive-grid-row
192-
v-for="(item, index) in arrayOfSplittedItems.length === 0 ? items : arrayOfSplittedItems[currentPage - 1]"
197+
v-for="(item, index) in currentPageItems"
193198
:key="(item as any).id"
194199
:index="index"
195200
:columns="columns"
@@ -376,7 +381,7 @@ defineExpose({ items, grid });
376381
</hive-grid-row>
377382
</tbody>
378383
</table>
379-
<div v-if="arrayOfSplittedItems.length > 1" class="pagination-item">
384+
<div v-if="pagginatedItems.length > 1" class="pagination-item">
380385
<svg
381386
@click="prevPage(1)"
382387
xmlns="http://www.w3.org/2000/svg"
@@ -423,7 +428,7 @@ defineExpose({ items, grid });
423428
viewBox="0 0 30 30"
424429
version="1.1"
425430
class="pagination-item_svg"
426-
:class="{ disabled: currentPage === arrayOfSplittedItems.length }"
431+
:class="{ disabled: currentPage === pagginatedItems.length }"
427432
>
428433
<g id="surface1">
429434
<path
@@ -433,15 +438,15 @@ defineExpose({ items, grid });
433438
</g>
434439
</svg>
435440
<svg
436-
@click="nextPage(arrayOfSplittedItems.length)"
441+
@click="nextPage(pagginatedItems.length)"
437442
xmlns="http://www.w3.org/2000/svg"
438443
xmlns:xlink="http://www.w3.org/1999/xlink"
439444
width="30px"
440445
height="30px"
441446
viewBox="0 0 30 30"
442447
version="1.1"
443448
class="pagination-item_svg"
444-
:class="{ disabled: currentPage === arrayOfSplittedItems.length }"
449+
:class="{ disabled: currentPage === pagginatedItems.length }"
445450
>
446451
<g id="surface1">
447452
<path

0 commit comments

Comments
 (0)