Row selection
selectable adds a checkbox to every row and one in the header that selects the whole page. Bind the selected rows with v-model:selected.
vue
<template>
<VTable
ref="table"
v-model:selected="selected"
:columns="columns"
:data="products"
selectable
rowKey="id"
clientSide
:itemsPerPage="5"
>
<template #toolbar>
<button type="button" @click="table?.clearSelection()">Clear</button>
</template>
</VTable>
<p class="demo-log">Selected ids: {{ selected.map((row) => row.id).join(", ") || "none" }}</p>
</template>
<script setup lang="ts">
import { ref } from "vue";
import { VTable, type Column, type Row } from "v-table-system";
import { products } from "./data";
const table = ref<InstanceType<typeof VTable>>();
const selected = ref<Row[]>([]);
const columns: Column[] = [
{ key: "id", label: "ID" },
{ key: "name", label: "Name" },
{ key: "category", label: "Category" },
];
</script>Set a rowKey
rowKey tells the table how to recognise a row. It is the name of a unique property, or a function that returns a unique value:
vue
<VTable selectable rowKey="id" ... />
<VTable selectable :rowKey="(row) => `${row.type}-${row.code}`" ... />Without it rows are compared by object identity. That is fine for a fixed array, but a selection is lost as soon as the rows are loaded again, which is what happens on every page change of a server-side table.
Events and methods
update:selected / selectionChange | Both fire with the array of selected rows whenever it changes. |
clearSelection() | Method on the component that empties the selection. |
#toolbar slot | A place for your own buttons next to the selection count. It receives rows and selected. |
Clicking a row
Separately from the checkboxes, every row click emits selectRow with the row. Use it to open a details page, for example.
vue
<VTable @selectRow="(row) => router.push(`/products/${row.id}`)" ... />