提交
This commit is contained in:
1 parent
f9676f8759
commit
725774ef0c
155 files changed
+50675
-5242
No files matched your search
@@ -0,0 +1,46 @@
|
||||
import { computed, ref, type ComputedRef, type Ref } from 'vue'
|
||||
|
||||
type Source<T> = Ref<T[]> | ComputedRef<T[]> | (() => T[])
|
||||
|
||||
interface UsePagedTableOptions {
|
||||
initialPage?: number
|
||||
initialPageSize?: number
|
||||
}
|
||||
|
||||
export function usePagedTable<T>(source: Source<T>, options: UsePagedTableOptions = {}) {
|
||||
const currentPage = ref(options.initialPage ?? 1)
|
||||
const pageSize = ref(options.initialPageSize ?? 10)
|
||||
|
||||
const sourceRows = computed(() => {
|
||||
if (typeof source === 'function') return source()
|
||||
return source.value
|
||||
})
|
||||
|
||||
const total = computed(() => sourceRows.value.length)
|
||||
|
||||
const pagedRows = computed(() => {
|
||||
const start = (currentPage.value - 1) * pageSize.value
|
||||
const end = start + pageSize.value
|
||||
return sourceRows.value.slice(start, end)
|
||||
})
|
||||
|
||||
const rangeText = computed(() => {
|
||||
const count = total.value
|
||||
const start = count === 0 ? 0 : (currentPage.value - 1) * pageSize.value + 1
|
||||
const end = Math.min(currentPage.value * pageSize.value, count)
|
||||
return `显示 ${start}-${end} / ${count}`
|
||||
})
|
||||
|
||||
function onSizeChange() {
|
||||
currentPage.value = 1
|
||||
}
|
||||
|
||||
return {
|
||||
currentPage,
|
||||
pageSize,
|
||||
total,
|
||||
pagedRows,
|
||||
rangeText,
|
||||
onSizeChange
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user