-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useOffsetPagination): new function (#1104)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
- Loading branch information
1 parent
fbca4a6
commit b4b7e8e
Showing
7 changed files
with
406 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { defineComponent, reactive } from 'vue-demi' | ||
import { useOffsetPagination } from '@vueuse/core' | ||
import type { UseOffsetPaginationOptions } from '.' | ||
|
||
export const UseOffsetPagination = defineComponent<UseOffsetPaginationOptions>({ | ||
name: 'UseOffsetPagination', | ||
props: [ | ||
'total', | ||
'page', | ||
'pageSize', | ||
'onPageChange', | ||
'onPageSizeChange', | ||
'onPageCountChange', | ||
] as unknown as undefined, | ||
emits: [ | ||
'page-change', | ||
'page-size-change', | ||
'page-count-change', | ||
], | ||
setup(props, { slots, emit }) { | ||
const data = reactive(useOffsetPagination({ | ||
...props, | ||
onPageChange(...args) { | ||
props.onPageChange?.(...args) | ||
emit('page-change', ...args) | ||
}, | ||
onPageSizeChange(...args) { | ||
props.onPageSizeChange?.(...args) | ||
emit('page-size-change', ...args) | ||
}, | ||
onPageCountChange(...args) { | ||
props.onPageCountChange?.(...args) | ||
emit('page-count-change', ...args) | ||
}, | ||
})) | ||
|
||
return () => { | ||
if (slots.default) | ||
return slots.default(data) | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<script setup lang="ts"> | ||
import type { Ref } from 'vue-demi' | ||
import { ref } from 'vue-demi' | ||
import { useOffsetPagination } from '.' | ||
interface User { | ||
id: number | ||
name: string | ||
} | ||
const database = ref([]) as Ref<User[]> | ||
for (let i = 0; i < 80; i++) | ||
database.value.push({ id: i, name: `user ${i}` }) | ||
function fetch(page: number, pageSize: number) { | ||
return new Promise<User[]>((resolve, reject) => { | ||
const start = (page - 1) * pageSize | ||
const end = start + pageSize | ||
setTimeout(() => { | ||
resolve(database.value.slice(start, end)) | ||
}, 100) | ||
}) | ||
} | ||
const data: Ref<User[]> = ref([]) | ||
const page = ref(1) | ||
const pageSize = ref(10) | ||
fetchData({ | ||
currentPage: page.value, | ||
currentPageSize: pageSize.value, | ||
}) | ||
function fetchData({ currentPage, currentPageSize }: { currentPage: number; currentPageSize: number }) { | ||
fetch(currentPage, currentPageSize).then((responseData) => { | ||
data.value = responseData | ||
}) | ||
} | ||
const { | ||
currentPage, | ||
currentPageSize, | ||
pageCount, | ||
isFirstPage, | ||
isLastPage, | ||
prev, | ||
next, | ||
} = useOffsetPagination({ | ||
total: database.value.length, | ||
page: 1, | ||
pageSize, | ||
onPageChange: fetchData, | ||
onPageSizeChange: fetchData, | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div class="gap-x-4 gap-y-2 grid-cols-2 inline-grid items-center"> | ||
<div opacity="50"> | ||
total: | ||
</div> | ||
<div>{{ database.length }}</div> | ||
<div opacity="50"> | ||
pageCount: | ||
</div> | ||
<div>{{ pageCount }}</div> | ||
<div opacity="50"> | ||
currentPageSize: | ||
</div> | ||
<div>{{ currentPageSize }}</div> | ||
<div opacity="50"> | ||
currentPage: | ||
</div> | ||
<div>{{ currentPage }}</div> | ||
<div opacity="50"> | ||
isFirstPage: | ||
</div> | ||
<div>{{ isFirstPage }}</div> | ||
<div opacity="50"> | ||
isLastPage: | ||
</div> | ||
<div>{{ isLastPage }}</div> | ||
</div> | ||
<div class="my-4"> | ||
<button :disabled="isFirstPage" @click="prev"> | ||
prev | ||
</button> | ||
<button | ||
v-for="item in pageCount" | ||
:key="item" | ||
:disabled="currentPage === item" | ||
@click="currentPage = item" | ||
> | ||
{{ item }} | ||
</button> | ||
<button :disabled="isLastPage" @click="next"> | ||
next | ||
</button> | ||
</div> | ||
|
||
<table> | ||
<thead> | ||
<tr> | ||
<td>id</td> | ||
<td>name</td> | ||
</tr> | ||
</thead> | ||
<tr v-for="d in data" :key="d.id"> | ||
<td>{{ d.id }}</td> | ||
<td>{{ d.name }}</td> | ||
</tr> | ||
</table> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
--- | ||
category: Utilities | ||
--- | ||
|
||
# useOffsetPagination | ||
|
||
Reactive offset pagination. | ||
|
||
## Usage | ||
|
||
```ts | ||
import { useOffsetPagination } from '@vueuse/core' | ||
|
||
function fetchData({ currentPage, currentPageSize }: { currentPage: number; currentPageSize: number }) { | ||
fetch(currentPage, currentPageSize).then((responseData) => { | ||
data.value = responseData | ||
}) | ||
} | ||
|
||
const { | ||
currentPage, | ||
currentPageSize, | ||
pageCount, | ||
isFirstPage, | ||
isLastPage, | ||
prev, | ||
next, | ||
} = useOffsetPagination({ | ||
total: database.value.length, | ||
page: 1, | ||
pageSize, | ||
onPageChange: fetchData, | ||
onPageSizeChange: fetchData, | ||
}) | ||
``` | ||
|
||
## Component | ||
|
||
```html | ||
<UseOffsetPagination | ||
v-slot="{ | ||
currentPage, | ||
currentPageSize, | ||
next, | ||
prev, | ||
pageCount, | ||
isFirstPage, | ||
isLastPage | ||
}" | ||
:total="database.length" | ||
@page-change="fetchData" | ||
@page-size-change="fetchData" | ||
> | ||
<div class="gap-x-4 gap-y-2 grid-cols-2 inline-grid items-center"> | ||
<div opacity="50"> | ||
total: | ||
</div> | ||
<div>{{ database.length }}</div> | ||
<div opacity="50"> | ||
pageCount: | ||
</div> | ||
<div>{{ pageCount }}</div> | ||
<div opacity="50"> | ||
currentPageSize: | ||
</div> | ||
<div>{{ currentPageSize }}</div> | ||
<div opacity="50"> | ||
currentPage: | ||
</div> | ||
<div>{{ currentPage }}</div> | ||
<div opacity="50"> | ||
isFirstPage: | ||
</div> | ||
<div>{{ isFirstPage }}</div> | ||
<div opacity="50"> | ||
isLastPage: | ||
</div> | ||
<div>{{ isLastPage }}</div> | ||
</div> | ||
<div> | ||
<button :disabled="isFirstPage" @click="prev"> | ||
prev | ||
</button> | ||
<button :disabled="isLastPage" @click="next"> | ||
next | ||
</button> | ||
</div> | ||
</UseOffsetPagination> | ||
``` | ||
|
||
Component event supported props event callback and event listener. | ||
|
||
event listener: | ||
```html | ||
<UseOffsetPagination | ||
v-slot="{ | ||
currentPage, | ||
currentPageSize, | ||
next, | ||
prev, | ||
pageCount, | ||
isFirstPage, | ||
isLastPage | ||
}" | ||
:total="database.length" | ||
@page-change="fetchData" | ||
@page-size-change="fetchData" | ||
@page-count-change="onPageCountChange" | ||
> | ||
<!-- your code --> | ||
</UseOffsetPagination> | ||
``` | ||
|
||
or props event callback: | ||
|
||
```html | ||
<UseOffsetPagination | ||
v-slot="{ | ||
currentPage, | ||
currentPageSize, | ||
next, | ||
prev, | ||
pageCount, | ||
isFirstPage, | ||
isLastPage | ||
}" | ||
:total="database.length" | ||
:on-page-change="fetchData" | ||
:on-page-size-change="fetchData" | ||
:on-page-count-change="onPageCountChange" | ||
> | ||
<!-- your code --> | ||
</UseOffsetPagination> | ||
``` |
Oops, something went wrong.