Skip to content

Server-side data ​

For large data sets, keep the work on the server. Leave clientSide out, pass the rows of the current page, and reload them when the table emits an event.

vue
<template>
  <VTable
    v-model:currentPage="page"
    :columns="columns"
    :data="rows"
    :loading="loading"
    :totalItems="total"
    :itemsPerPage="perPage"
    isShowSearch
    :searchDebounce="500"
    @changePage="load"
    @Search="onSearch"
    @SortBy="onSort"
  />
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import { VTable, type Column, type Row, type SortPayload } from "v-table-system";
import { products } from "./data";

const columns: Column[] = [
  { key: "id", label: "ID", sortable: true },
  { key: "name", label: "Name", sortable: true },
  { key: "price", label: "Price", sortable: true },
];

const perPage = 5;
const page = ref(1);
const rows = ref<Row[]>([]);
const total = ref(0);
const loading = ref(false);
let search = "";
let sort: SortPayload | undefined;

// Stands in for your API, for example GET /products?page=2&search=mouse&sort=price
function fakeApi(): Promise<{ items: Row[]; total: number }> {
  let items = products.filter((product) => product.name.toLowerCase().includes(search.toLowerCase()));
  if (sort) {
    const { key, direction } = sort;
    items = [...items].sort((a: Row, b: Row) => (a[key] > b[key] ? 1 : -1) * (direction === "asc" ? 1 : -1));
  }
  const start = (page.value - 1) * perPage;
  return new Promise((done) => setTimeout(() => done({ items: items.slice(start, start + perPage), total: items.length }), 400));
}

async function load() {
  loading.value = true;
  const result = await fakeApi();
  rows.value = result.items;
  total.value = result.total;
  loading.value = false;
}

function onSearch(value: string) {
  search = value;
  page.value = 1;
  load();
}

function onSort(payload: SortPayload) {
  sort = payload;
  load();
}

onMounted(load);
</script>

Checklist ​

  1. data holds only the current page.
  2. totalItems and itemsPerPage tell the table how many pages there are.
  3. loading shows a spinner while a request is running.
  4. On changePage, Search, SortBy and changeItemsPerPage, call your API again.
  5. On Search and changeItemsPerPage, go back to page 1.
  6. With row selection, set a rowKey so the selection survives a reload.

What each event gives you ​

EventPayloadTypical query parameter
changePage2?page=2
changeItemsPerPage25?per_page=25
Search"mouse" or { keySearch, inputValue }?search=mouse
SortBy{ key, direction, title }?sort=price&order=desc

Released under the Apache-2.0 License.