48 lines
978 B
JavaScript
48 lines
978 B
JavaScript
import { ref, computed } from 'vue'
|
|
|
|
export function usePagination(items, pageSize = 10) {
|
|
const currentPage = ref(1)
|
|
const itemsPerPage = ref(pageSize)
|
|
|
|
const totalPages = computed(() => Math.ceil(items.value.length / itemsPerPage.value))
|
|
|
|
const paginatedItems = computed(() => {
|
|
const start = (currentPage.value - 1) * itemsPerPage.value
|
|
const end = start + itemsPerPage.value
|
|
return items.value.slice(start, end)
|
|
})
|
|
|
|
const goToPage = (page) => {
|
|
if (page >= 1 && page <= totalPages.value) {
|
|
currentPage.value = page
|
|
}
|
|
}
|
|
|
|
const nextPage = () => {
|
|
if (currentPage.value < totalPages.value) {
|
|
currentPage.value++
|
|
}
|
|
}
|
|
|
|
const prevPage = () => {
|
|
if (currentPage.value > 1) {
|
|
currentPage.value--
|
|
}
|
|
}
|
|
|
|
const resetPage = () => {
|
|
currentPage.value = 1
|
|
}
|
|
|
|
return {
|
|
currentPage,
|
|
itemsPerPage,
|
|
totalPages,
|
|
paginatedItems,
|
|
goToPage,
|
|
nextPage,
|
|
prevPage,
|
|
resetPage
|
|
}
|
|
}
|