diff --git a/docs/components/pagination.md b/docs/components/pagination.md index c4388f2..989472a 100644 --- a/docs/components/pagination.md +++ b/docs/components/pagination.md @@ -1,8 +1,12 @@ # Vue Pagination Component - Flowbite +## Default pagination + ```vue + + + +``` + + + +## Pagination with table layout + +```vue + + + + +``` + + diff --git a/docs/components/pagination/examples/PaginationExample.vue b/docs/components/pagination/examples/PaginationExample.vue index a559591..845de13 100644 --- a/docs/components/pagination/examples/PaginationExample.vue +++ b/docs/components/pagination/examples/PaginationExample.vue @@ -1,6 +1,6 @@ - + diff --git a/docs/components/pagination/examples/PaginationTableExample.vue b/docs/components/pagination/examples/PaginationTableExample.vue new file mode 100644 index 0000000..5660d0b --- /dev/null +++ b/docs/components/pagination/examples/PaginationTableExample.vue @@ -0,0 +1,16 @@ + + + + + + diff --git a/src/components/Pagination/Pagination.vue b/src/components/Pagination/Pagination.vue index 863b4fd..42f59a9 100644 --- a/src/components/Pagination/Pagination.vue +++ b/src/components/Pagination/Pagination.vue @@ -1,5 +1,13 @@ + + Showing + {{ startItemsCount }} + to + {{ endItemsCount }} + of + {{ computedTotalItems }} + - Previous + {{ previousLabel }} {{ index }} @@ -29,7 +37,7 @@ @click="increasePage" class="py-2 px-3 leading-tight text-gray-500 bg-white rounded-r-lg border border-gray-300 hover:bg-gray-100 hover:text-gray-700 dark:bg-gray-800 dark:border-gray-700 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white" > - Next + {{ nextLabel }} @@ -48,9 +56,17 @@ const props = defineProps({ default: 1, }, totalPages: { + type: Number, + default: 1, + }, + perPage: { type: Number, default: 10, }, + totalItems: { + type: Number, + required: false, + }, layout: { type: String as PropType, // 'navigation' | 'pagination' | 'table' default: 'pagination', @@ -63,9 +79,16 @@ const props = defineProps({ type: Number, default: 3, }, + previousLabel: { + type: String, + default: 'Previous', + }, + nextLabel: { + type: String, + default: 'Next', + }, }) - const setPage = (index: number) => { emit('update:modelValue', index) } @@ -76,19 +99,62 @@ const increasePage = () => { emit('update:modelValue', props.modelValue + 1) } +const computedTotalPages = computed(() => { + if (!props.totalItems) return props.totalPages + if (!props.perPage) return props.totalPages + return Math.ceil(props.totalItems / props.perPage) +}) + const isDecreaseDisabled = computed(() => props.modelValue <= 1) -const isIncreaseDisabled = computed(() => props.modelValue >= props.totalPages) +const isIncreaseDisabled = computed(() => props.modelValue >= computedTotalPages.value) const isSetPageDisabled = (index: number) => index === props.modelValue -const filteredPages = computed(() => { - if (props.modelValue <= props.sliceLength) return [...Array(Math.abs(props.modelValue - props.sliceLength) + props.modelValue + props.sliceLength + 1).keys()].map(num => num + 1) +const pagesToDisplay = computed(() => { + if (props.layout === 'navigation') return [] + if (props.layout === 'table') return [] + + if (computedTotalPages.value <= props.sliceLength * 2 + 1) { + const pages = [] + for (let page = 1; page <= computedTotalPages.value; page++) { + pages.push(page) + } + return pages + } + if (props.modelValue <= props.sliceLength) { + const pages = [] + const slicedLength = Math.abs(props.modelValue - props.sliceLength) + props.modelValue + props.sliceLength + 1 + for (let page = 1; page <= slicedLength; page++) { + pages.push(page) + } + return pages + } + if (props.modelValue >= computedTotalPages.value - props.sliceLength) { + const pages = [] + for (let page = Math.abs(computedTotalPages.value - props.sliceLength * 2); page <= computedTotalPages.value; page++) { + pages.push(page) + } + return pages + } const pages = [] - let startedPage = props.modelValue - props.sliceLength > 0 ? props.modelValue - props.sliceLength : 1 for (let page = startedPage; page < props.modelValue + props.sliceLength + 1; page++) { + if (page >= computedTotalPages.value) break pages.push(page) } return pages }) + + +const startItemsCount = computed(() => props.modelValue * props.perPage - props.perPage + 1) +const endItemsCount = computed(() => { + const count = props.modelValue * props.perPage + 1 + if (!props.totalItems) return count + if (count > props.totalItems) return props.totalItems + return count +}) +const computedTotalItems = computed(() => { + if (props.totalItems) return props.totalItems + return computedTotalPages.value * props.perPage +})