Custom cells and actions
vue
<template>
<VTable :columns="columns" :data="rows" clientSide :itemsPerPage="5" @selectRow="message = `Row clicked: ${$event.name}`">
<!-- A slot per column: cell-<key> -->
<template #cell-status="{ value }">
<span :style="{ color: value === 'active' ? '#00ab55' : '#e2a03f', fontWeight: 600 }">● {{ value }}</span>
</template>
<!-- The header has one too: header-<key> -->
<template #header-price>Price (USD)</template>
<template #empty>Nothing to show yet</template>
</VTable>
<p class="demo-log">{{ message }}</p>
</template>
<script setup lang="ts">
import { ref } from "vue";
import { VTable, type Column } from "v-table-system";
import { products } from "./data";
const message = ref("Click a row or a button");
const columns: Column[] = [
{ key: "name", label: "Name" },
{ key: "price", label: "Price", format: (value) => `$${value}` },
{ key: "status", label: "Status" },
{ key: "actions", label: "Actions" },
];
// A row property called "actions" is rendered as buttons
const rows = products.map((product) => ({
...product,
actions: [
{
tag: "Edit",
style: { color: "#4361ee", fontWeight: "600" },
callback: () => (message.value = `Edit ${product.name}`),
},
{
tag: "Delete",
style: { color: "#e7515a", fontWeight: "600" },
callback: () => (message.value = `Delete ${product.name}`),
},
],
}));
</script>Ways to change a cell
There are three, from the simplest to the most flexible:
formaton the column returns plain text. Best for dates, money and numbers.htmlon the column returns an HTML string. It also works in the web component.- A
cell-<key>slot renders anything, including your own components.
ts
const columns: Column[] = [
{ key: "price", format: (value) => `$${value.toFixed(2)}` },
{ key: "status", html: (value) => `<span class="badge">${value}</span>` },
];WARNING
html output and action tags are inserted as HTML. Never pass user input to them without escaping it.
Slots
| Slot | Props | Description |
|---|---|---|
cell-<key> | row, value, index, column | The content of a cell in the column <key>. |
header-<key> | column | The header text of the column <key>. |
expand | row, index | The details of an expanded row. |
toolbar | rows, selected | Extra content in the bar above the table. |
empty | Shown when there are no rows. | |
loading | Shown instead of the spinner while loading is true. |
Row actions
A row property named actions holding an array is rendered as a list of buttons. Each action has:
| Field | Type | Description |
|---|---|---|
tag | string | HTML shown inside the button, text or an icon. |
style | object | Inline styles of the button. |
callback | (event, row) => void | Called on click. |
Clicking an action does not trigger selectRow.
For anything more than simple buttons, use the cell-actions slot instead.