Skip to content

usePagination

Pagination state for tables and lists. Provides currentPage, pageSize, totalItems, derived totalPages, offset, and a slice helper. Use with BvPagination or BvTable.

Basic Usage

vue
<template>
  <BvPagination
    v-model:current-page="currentPage"
    :total-items="totalItems"
    :page-size="pageSize"
  />
</template>

<script setup>
import { computed, ref, watch } from "vue";
import { BvPagination } from "@baklavue/ui";
import { usePagination } from "@baklavue/composables";

const allItems = ref([/* ... */]);

const {
  currentPage,
  pageSize,
  totalItems,
  totalPages,
  setPage,
  setPageSize,
  setTotalItems,
  slice,
} = usePagination({
  totalItems: 100,
  pageSize: 10,
});

// Sync totalItems when data changes
watch(allItems, (items) => setTotalItems(items.length), { immediate: true });

// Get items for current page
const pageItems = computed(() => slice(allItems.value));
</script>

With BvTable

vue
<template>
  <BvTable
    :columns="columns"
    :data="pageData"
    :pagination="{
      currentPage,
      totalItems,
      itemsPerPage: pageSize,
    }"
  />
</template>

<script setup>
import { computed, ref, watch } from "vue";
import { BvTable } from "@baklavue/ui";
import { usePagination } from "@baklavue/composables";

const allData = ref([/* ... */]);
const { currentPage, pageSize, totalItems, slice, setTotalItems } = usePagination({
  pageSize: 10,
});

watch(allData, (data) => setTotalItems(data.length), { immediate: true });

const pageData = computed(() => slice(allData.value));
</script>

API

Return Value

PropertyTypeDescription
currentPageRef<number>Current page (1-based)
pageSizeRef<number>Items per page
totalItemsRef<number>Total item count
totalPagesComputedRef<number>Total pages
offsetComputedRef<number>Offset for current page
setPage(page: number) => voidSet current page
setPageSize(size: number) => voidSet items per page
setTotalItems(total: number) => voidSet total items
slice<T>(arr: T[]) => T[]Slice array for current page

Options

typescript
interface UsePaginationOptions {
  totalItems?: number;   // Default: 0
  pageSize?: number;     // Default: 10
  initialPage?: number;  // Default: 1
}

TypeScript Support

typescript
import { usePagination, type UsePaginationOptions } from "@baklavue/composables";

const options: UsePaginationOptions = { totalItems: 50, pageSize: 5 };
const pagination = usePagination(options);