You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.1 KiB
41 lines
1.1 KiB
import {useDebounceFn, useThrottleFn} from "@vueuse/core";
|
|
|
|
export default function usePagination(current: number, limit: number, total: number) {
|
|
const currentRef = ref(current);
|
|
const pageSizeRef = ref(limit);
|
|
const totalRef = ref(total);
|
|
const pagesRef = ref(Math.ceil(totalRef.value / limit))
|
|
watchEffect(()=>{
|
|
pagesRef.value = Math.ceil(totalRef.value / limit)
|
|
})
|
|
|
|
function setCurrentPage(page: number) {
|
|
currentRef.value = page;
|
|
// if (isFunction(callback)) {
|
|
// callback()
|
|
// }
|
|
}
|
|
function setTotal(total: number) {
|
|
totalRef.value = total;
|
|
}
|
|
function setPageSize(pageSize: number):void {
|
|
pageSizeRef.value = pageSize;
|
|
}
|
|
function changePage(flag: 'next'|'prev'):void {
|
|
if(flag === 'next') {
|
|
if (currentRef.value >= pagesRef.value) return;
|
|
else {
|
|
setCurrentPage(currentRef.value + 1)
|
|
}
|
|
}else if(flag === 'prev') {
|
|
if (currentRef.value <= 1) return;
|
|
else {
|
|
currentRef.value --;
|
|
setCurrentPage(currentRef.value - 1)
|
|
}
|
|
}
|
|
}
|
|
|
|
return { setCurrentPage,setPageSize, changePage, current: currentRef, limit, total:totalRef, setTotal,pagesRef };
|
|
}
|