Skip to content

useDataTable ​

The headless logic behind WeDatatable: it talks to the backend datatable endpoint, keeps configuration, items, totals, aggregates and selection, and runs server actions. Use it to build a custom table UI on the same backend contract.

Backend contract ​

Every call is a POST {resource}/datatables with an action field in the body.

Read (action: 'read') body:

FieldDescription
searchSearch string from the table options
sorts[{ id, desc }] from sortBy
filters[{ id, value, operator }] — only complete filters (e.g. both ends of a between)
conftrue when the configuration must be (re)sent: first load, resource change, or loadConfiguration: 'always'
page, limitFrom page / itemsPerPage
columnsChosen optional columns, sent only when columns is not null
...requestParamsSpread last

Response: { data, meta?, conf?, aggregates? }. conf has datatable.headers, datatable.pagination, datatable.selection.id, fields and actions. meta.total is used when pagination is on, otherwise data.length.

Actions (executeAction) send the same filters, the last table options, selection, columns, requestParams and your extra params. A response { action: 'redirect', url } navigates to url; anything else reloads the table.

Usage ​

ts
const resource = ref('holidays')
const filters = ref({ date: { operator: 'between', value: ['2026-01-01', '2026-12-31'] } })
const requestParams = ref({})
const loadConfiguration = ref<'always' | 'default'>('default')

const { items, headers, totalItems, loading, load, reloadData } = useDataTable({
  resource,
  filters,
  requestParams,
  loadConfiguration,
})
vue
<v-data-table-server
  :items="items"
  :headers="headers"
  :items-length="totalItems"
  :loading="loading"
  @update:options="load"
/>

Nothing is loaded until load is called the first time (usually by @update:options).

Options ​

OptionTypeDescription
resourceRef<string>Backend resource; the endpoint is ${resource}/datatables. A change reloads with configuration
filtersRef<Record<string, { value, operator }>>Filters by id; incomplete filters are not sent
requestParamsRef<object>Extra body params for every call
loadConfigurationRef<'always' | 'default'>'always' asks for conf on every read
columnsRef<string[] | null>Optional columns chosen by the user; null = API defaults

Changes to filters, requestParams and columns are compared by value (JSON) and trigger reloadData(), so passing a new object with the same content does not refetch.

Return Value ​

PropertyTypeDescription
itemsRef<T[]>Current rows
totalItemsRef<number>Total rows (meta.total or data.length)
headersComputedRef<Header[]>Visible headers: non-optional ones not hidden, plus optional ones in visibleColumns. Each has title, key, the header conf and field
paginationComputedRefconf.datatable.pagination
aggregatesRef<object>aggregates from the response ({} if missing or not an object)
loadingRef<boolean>A read is in progress
errorRef<unknown>Error of the last read (reads use notify: false)
load(options, reloadConfig = false, keepSelection = false) => Promise<{ data, meta } | undefined>Reads with Vuetify table options (page, itemsPerPage, sortBy, search); aborts the previous read
reloadData(reloadConfig = false, keepSelection = false) => voidRepeats the last load
availableFiltersComputedRef<Header[]>Headers with filter.advanced, excluding hidden optional columns
optionalHeadersComputedRef<Header[]>Headers with optional
defaultColumnsComputedRef<string[]>Keys of optional headers with optional_visible
visibleColumnsComputedRef<string[]>columns if set, otherwise defaultColumns
actionsComputedRef<any[]>conf.actions
selectionKeyComputedRef<string | null>conf.datatable.selection.id — row key for selection, null = no selection
selectedItemsRef<unknown[]>Selected rows; cleared after each read unless keepSelection
clearSelection() => voidEmpties selectedItems
executeAction(action, extraParams = {}, reset = true, keepSelection = false) => Promise<any>Runs a server action (see below)
loadingExecuteActionRef<boolean>An action is running

executeAction(action, extraParams, reset, keepSelection) ​

  • POSTs { action, filters, ...lastOptions, selection: selectedItems, columns?, ...requestParams, ...extraParams } and resolves with the full response.
  • redirect response: sets window.location to url.
  • Otherwise, if reset: reloads the table and, unless keepSelection, clears the selection. Use keepSelection: true to apply several bulk actions in a row to the same rows.
  • Errors are not caught: useApi shows its toast and the promise rejects.
ts
await executeAction('export', { format: 'xlsx' }, false) // no reload
await executeAction('archive', {}, true, true) // reload, keep selected rows

Source Code ​