Skip to content

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 ​

vue
<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 (true when the configuration is needed), columns (only when the user picked columns) and requestParams. Response: { data, meta, conf?, aggregates? }.
  • any action id (via executeAction) — body: the same params plus selection (selected ids) and extra params. A response { action: 'redirect', url } navigates to url; 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 ​

PropTypeDefaultDescription
resourcestring--Required. Server resource ({resource}/datatables)
addButtonboolean | stringtrueShows the add button; a string sets its label
filtersobject{}Filter values ({ [column]: { operator, value } }), usually v-model:filters
requestParamsobject{}Extra params sent with every request; compared by value, changes reload the data
actionsHeaderboolean | objecttrueAdds an end-fixed actions column when an item.actions slot exists; an object customizes the header (id, title, width, ...)
extraHeadersArray[]Extra client-side columns
extraActionsArray[]Client-side actions added to the actions menu (see Actions)
loadConfiguration'default' | 'always''default'Load the configuration once, or with every request
hideTopActionsbooleanfalseHides the toolbar (add button, actions, search, columns, filters) and the above-table slot
hideAggregatesbooleanfalseHides the aggregates row
heightstring | numberundefinedTable height (passed to VDataTableServer)
fillboolean | number | stringfalseThe card sizes itself down to the bottom of the viewport: sticky header, only rows scroll. A number is the bottom gap in px
multiSortbooleantrueSort by multiple columns
persist'state' | 'local' | 'session' | falseapp config, else 'state'Where search, page, filters, items per page and sort are persisted
persistColumns'state' | 'local' | 'session' | falseapp config, else 'local'Where the picked columns are persisted (separately)
persistKeystringresource 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):

ts
// vuetify options
defaults: {
  WeDatatable: { fill: true, multiSort: false },
}

Models ​

BindingTypeDescription
v-model:filtersobjectFilter values
v-model:searchstringSearch text (debounced 500ms)
v-model:pagenumberCurrent page
v-model:items-per-pagenumberPage size (default 50, or we.datatables.itemsPerPage)
v-model:sort-byArraySort, Vuetify format
v-model:columnsstring[] | nullVisible optional columns; null = API defaults
v-model:selectionunknown[]Selected row ids
v-model:aggregatesobjectAggregates from the last response (read only)

Bound values are the initial state; once persisted, the persisted state wins on the next mount.

Events ​

EventPayloadDescription
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:aggregatesvalueModel updates

Slots ​

NamePropsDescription
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 + aggregatesCustom cell. Also rendered in the aggregates row (except item.actions), where aggregates is set; it is null when there are no aggregates
item.actionsVuetify item slot propsContent of the actions column
any otherVuetify slot propsPassed 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 ​

NameDescription
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
headersMapColumns with an advanced filter, keyed by column (filter definitions, datasets, ...)
totalItemsTotal 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:

vue
<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:

vue
<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:

vue
<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 ​

ModeStorage
'state'Nuxt useState (survives client navigation)
'local'localStorage
'session'sessionStorage
falsenot persisted
vue
<template>
  <we-datatable resource="orders" persist="local" persist-key="my-orders" />
  <we-datatable resource="tmp" :persist="false" :persist-columns="false" />
</template>

App config ​

ts
// 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 ​

vue
<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 ​

vue
<template>
  <v-container class="pb-0">
    <we-datatable resource="users" fill />
  </v-container>
</template>

Source Code ​