Skip to content

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:

  1. format on the column returns plain text. Best for dates, money and numbers.
  2. html on the column returns an HTML string. It also works in the web component.
  3. 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 ​

SlotPropsDescription
cell-<key>row, value, index, columnThe content of a cell in the column <key>.
header-<key>columnThe header text of the column <key>.
expandrow, indexThe details of an expanded row.
toolbarrows, selectedExtra content in the bar above the table.
emptyShown when there are no rows.
loadingShown 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:

FieldTypeDescription
tagstringHTML shown inside the button, text or an icon.
styleobjectInline styles of the button.
callback(event, row) => voidCalled on click.

Clicking an action does not trigger selectRow.

For anything more than simple buttons, use the cell-actions slot instead.

Released under the Apache-2.0 License.