WeDatatable
Server-driven data table built on Vuetify's VDataTableServer. Columns, filters, actions, selection and pagination are described by the API (the configuration), so a table usually needs only a resource. It adds search, an advanced filters panel, an optional column picker, bulk actions, an aggregates row, auto-formatted cells and state persistence.
The data logic lives in useDataTable; the fill sizing in useAutoFillHeight.
Usage
<template>
<we-datatable resource="users" />
<we-datatable
v-model:filters="filters"
resource="orders"
:request-params="{ customer_id: 5 }"
@new="create"
@click:row="edit"
/>
</template>Other attributes are passed to VDataTableServer (density, hover, item-props, ...).
API contract
Every request is a POST to {resource}/datatables with an action:
read(on every load) — body:search,sorts([{ id, desc }]),filters([{ id, operator, value }], complete filters only),page,limit,conf(truewhen the configuration is needed),columns(only when the user picked columns) andrequestParams. Response:{ data, meta, conf?, aggregates? }.- any action id (via
executeAction) — body: the same params plusselection(selected ids) and extra params. A response{ action: 'redirect', url }navigates tourl; otherwise the table reloads.
The configuration (conf) is loaded on the first request, when resource changes, or always with load-configuration="always". It provides datatable.headers, fields, datatable.pagination, datatable.selection.id (enables row selection) and actions.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
resource | string | -- | Required. Server resource ({resource}/datatables) |
addButton | boolean | string | true | Shows the add button; a string sets its label |
filters | object | {} | Filter values ({ [column]: { operator, value } }), usually v-model:filters |
requestParams | object | {} | Extra params sent with every request; compared by value, changes reload the data |
actionsHeader | boolean | object | true | Adds an end-fixed actions column when an item.actions slot exists; an object customizes the header (id, title, width, ...) |
extraHeaders | Array | [] | Extra client-side columns |
extraActions | Array | [] | Client-side actions added to the actions menu (see Actions) |
loadConfiguration | 'default' | 'always' | 'default' | Load the configuration once, or with every request |
hideTopActions | boolean | false | Hides the toolbar (add button, actions, search, columns, filters) and the above-table slot |
hideAggregates | boolean | false | Hides the aggregates row |
height | string | number | undefined | Table height (passed to VDataTableServer) |
fill | boolean | number | string | false | The card sizes itself down to the bottom of the viewport: sticky header, only rows scroll. A number is the bottom gap in px |
multiSort | boolean | true | Sort by multiple columns |
persist | 'state' | 'local' | 'session' | false | app config, else 'state' | Where search, page, filters, items per page and sort are persisted |
persistColumns | 'state' | 'local' | 'session' | false | app config, else 'local' | Where the picked columns are persisted (separately) |
persistKey | string | resource or resource-<requestParams.configuration> | Storage key suffix (we_dt_<key>, we_dt_<key>_columns) |
Props can be set globally with the Vuetify defaults key WeDatatable (explicit prop › global default › prop default):
// vuetify options
defaults: {
WeDatatable: { fill: true, multiSort: false },
}Models
| Binding | Type | Description |
|---|---|---|
v-model:filters | object | Filter values |
v-model:search | string | Search text (debounced 500ms) |
v-model:page | number | Current page |
v-model:items-per-page | number | Page size (default 50, or we.datatables.itemsPerPage) |
v-model:sort-by | Array | Sort, Vuetify format |
v-model:columns | string[] | null | Visible optional columns; null = API defaults |
v-model:selection | unknown[] | Selected row ids |
v-model:aggregates | object | Aggregates from the last response (read only) |
Bound values are the initial state; once persisted, the persisted state wins on the next mount.
Events
| Event | Payload | Description |
|---|---|---|
new | -- | Add button clicked |
action | { id, ...action, selection? } | Add button ({ id: 'new' }) or an extra action selected |
click:row | (item, args, event) | Row clicked; unlike Vuetify, the row item comes first |
update:filters, update:search, update:page, update:items-per-page, update:sort-by, update:columns, update:selection, update:aggregates | value | Model updates |
Slots
| Name | Props | Description |
|---|---|---|
action-buttons | -- | Replaces the add button |
above-table | { headersMap } | Content between the toolbar and the table (e.g. external filters) |
item.<key> | Vuetify item slot props + aggregates | Custom cell. Also rendered in the aggregates row (except item.actions), where aggregates is set; it is null when there are no aggregates |
item.actions | Vuetify item slot props | Content of the actions column |
| any other | Vuetify slot props | Passed through to VDataTableServer |
Columns without a slot are rendered with a formatter when their type has one (date, datetime, boolean, dataset, or custom): see Datatable formatters.
Exposed
| Name | Description |
|---|---|
reloadData(reloadConfig = false, keepSelection = false) | Reloads the current page |
clearSelection() | Clears the selection |
executeAction(action, extraParams = {}, reset = true, keepSelection = false) | Runs a server action (see API contract). With reset the table reloads and the selection is cleared, unless keepSelection |
headersMap | Columns with an advanced filter, keyed by column (filter definitions, datasets, ...) |
totalItems | Total number of rows |
Filters
The filter button opens a panel with the columns whose configuration has an advanced filter (filter.advanced). Each filter has an operator and a value; the button is highlighted when at least one filter is complete. The panel's Reset clears filters, search, sort, page and selection.
For in / notin filters you can paste a list of values (one per line, or a column of cells copied from Excel) into a field: each value gets its own field, trimmed and de-duplicated.
Filters can also be driven from outside with the we-dt-* filter components bound to v-model:filters, reusing the server datasets through headersMap:
<template>
<we-dt-select-filter
v-model="usersFilters.user_tags"
label="Tag"
:items="dtUsers?.headersMap.user_tags?.filter.type.dataset"
/>
<we-datatable ref="dtUsers" v-model:filters="usersFilters" resource="users" />
</template>
<script setup lang="ts">
const dtUsers = useTemplateRef('dtUsers')
const usersFilters = ref({ user_tags: null })
</script>See we-dt-select-filter, we-dt-select-resource-filter, we-dt-daterange-filter, we-dt-cluster-filter, we-dt-checkbox-filter.
Column picker
Headers flagged optional in the configuration can be shown/hidden from a column menu (visible by default when optional_visible). The choice is in v-model:columns (null while it matches the defaults) and is sent to the API as columns. Hiding a column removes its filter.
Actions
Server actions (conf.actions) appear in the actions menu: those with bulk when nothing is selected (label bulk_label), those with multiple when rows are selected. With confirm and confirm_message a confirmation dialog is shown first; then executeAction(id) runs.
Actions flagged extra (server side, or passed in extra-actions and not defined by the server) are not executed: the table emits action with the current selection, so you can handle them:
<template>
<we-datatable
ref="dt"
v-model:selection="selection"
resource="posts"
:extra-actions="[{ id: 'export', label: 'Export', bulk: true, multiple: true }]"
@action="onAction"
/>
</template>
<script setup lang="ts">
const dt = useTemplateRef('dt')
const selection = ref([])
async function onAction({ id }) {
if (id === 'export') {
const format = await askFormat()
await dt.value?.executeAction('export', { format }, true, true) // keep the selection
}
}
</script>Aggregates
When the response contains aggregates ({ [column]: { [name]: value } }), a row with the first aggregate of each column is appended to the table (hide it with hide-aggregates). The whole object is available through v-model:aggregates and the aggregates prop of the item.* slots:
<template>
<we-datatable v-model:aggregates="aggregates" resource="payments">
<template #[`item.amount`]="{ item, aggregates: agg }">
<b v-if="agg">{{ item.amount }}</b>
<span v-else>{{ item.amount }}</span>
</template>
</we-datatable>
</template>Persistence
| Mode | Storage |
|---|---|
'state' | Nuxt useState (survives client navigation) |
'local' | localStorage |
'session' | sessionStorage |
false | not persisted |
<template>
<we-datatable resource="orders" persist="local" persist-key="my-orders" />
<we-datatable resource="tmp" :persist="false" :persist-columns="false" />
</template>App config
// app.config.ts
export default defineAppConfig({
we: {
datatables: {
persist: 'state', // default persist mode
persistColumns: 'local', // default persistColumns mode
itemsPerPage: 25, // default page size
formatters: {}, // custom cell formatters, see /components/dt-formatters
},
},
})Examples
Custom columns and actions
<template>
<we-datatable
resource="orders"
:extra-headers="[{ key: 'preview', title: 'Preview', width: '120px' }]"
:actions-header="{ title: 'Operations' }"
>
<template #[`item.status`]="{ item }">
<v-chip :color="item.status === 'active' ? 'success' : 'error'" size="small">
{{ item.status }}
</v-chip>
</template>
<template #[`item.preview`]="{ item }">
<v-img :src="item.thumbnail" width="80" />
</template>
<template #[`item.actions`]="{ item }">
<v-btn icon="$edit" size="small" variant="text" @click="edit(item)" />
</template>
</we-datatable>
</template>Fill the viewport
<template>
<v-container class="pb-0">
<we-datatable resource="users" fill />
</v-container>
</template>