Skip to content

useDataTable

Wire up a paginated data table backed by a BaseResponse<Paginated<T>> from @basmilius/http-client. Combines useLoaded and usePagination and re-fetches whenever the page, page size or one of the supplied dependencies changes.

Importing

ts
import { useDataTable } from '@basmilius/common';

Usage

vue
<script setup lang="ts">
    import { useDataTable, useService } from '@basmilius/common';
    import { OrderService, type Order } from '@/services/OrderService';

    const orders = useService(OrderService);

    const {
        displayEmpty,
        isLoading,
        items,
        page,
        perPage,
        total,
        reload,
        setPage,
        setPerPage
    } = useDataTable<Order>((offset, limit) => orders.list(offset, limit));
</script>

<template>
    <table v-if="!displayEmpty">
        <tr v-for="order in items" :key="order.id">
            <td>{{ order.reference }}</td>
        </tr>
    </table>
    <p v-else>No orders found.</p>
</template>

The fetcher receives the calculated offset and the active limit. Returning false skips the update — useful when a request was cancelled or aborted upstream. Throwing an UnresolvedDependencyException silently swallows the call, which pairs nicely with unrefAll.

Pass a dependencies array of additional reactive sources (filter refs, search queries, selected tags) to trigger a re-fetch when they change.

ts
const search = ref('');
useDataTable(fetcher, [search]);

Returned bindings

PropertyTypeDescription
displayEmptyRef<boolean>true when the first load returned no items
isLoadingComputedRef<boolean>Loading flag from useLoaded
itemsRef<T[]>Latest page of items
limitsRef<number[]>Available page-size options (5, 10, 25, 50, 100)
pageRef<number>One-based current page
perPageRef<number>Active page size
totalRef<number>Total item count from the latest response
reload()() => Promise<void>Re-runs the fetcher with the current page and size
setPage(num)(num: number) => voidImperatively change the page
setPerPage(num)(num: number) => voidImperatively change the page size
setTotal(num)(num: number) => voidOverride the total (rarely needed)

Type signature

ts
declare function useDataTable<T>(
    fetcher: (offset: number, limit: number) => Promise<BaseResponse<Paginated<T>> | false>,
    dependencies?: MultiWatchSources
): UseDataTable<T>;

See also