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:
| Field | Description |
|---|---|
search | Search string from the table options |
sorts | [{ id, desc }] from sortBy |
filters | [{ id, value, operator }] — only complete filters (e.g. both ends of a between) |
conf | true when the configuration must be (re)sent: first load, resource change, or loadConfiguration: 'always' |
page, limit | From page / itemsPerPage |
columns | Chosen optional columns, sent only when columns is not null |
...requestParams | Spread 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
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,
})<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
| Option | Type | Description |
|---|---|---|
resource | Ref<string> | Backend resource; the endpoint is ${resource}/datatables. A change reloads with configuration |
filters | Ref<Record<string, { value, operator }>> | Filters by id; incomplete filters are not sent |
requestParams | Ref<object> | Extra body params for every call |
loadConfiguration | Ref<'always' | 'default'> | 'always' asks for conf on every read |
columns | Ref<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
| Property | Type | Description |
|---|---|---|
items | Ref<T[]> | Current rows |
totalItems | Ref<number> | Total rows (meta.total or data.length) |
headers | ComputedRef<Header[]> | Visible headers: non-optional ones not hidden, plus optional ones in visibleColumns. Each has title, key, the header conf and field |
pagination | ComputedRef | conf.datatable.pagination |
aggregates | Ref<object> | aggregates from the response ({} if missing or not an object) |
loading | Ref<boolean> | A read is in progress |
error | Ref<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) => void | Repeats the last load |
availableFilters | ComputedRef<Header[]> | Headers with filter.advanced, excluding hidden optional columns |
optionalHeaders | ComputedRef<Header[]> | Headers with optional |
defaultColumns | ComputedRef<string[]> | Keys of optional headers with optional_visible |
visibleColumns | ComputedRef<string[]> | columns if set, otherwise defaultColumns |
actions | ComputedRef<any[]> | conf.actions |
selectionKey | ComputedRef<string | null> | conf.datatable.selection.id — row key for selection, null = no selection |
selectedItems | Ref<unknown[]> | Selected rows; cleared after each read unless keepSelection |
clearSelection | () => void | Empties selectedItems |
executeAction | (action, extraParams = {}, reset = true, keepSelection = false) => Promise<any> | Runs a server action (see below) |
loadingExecuteAction | Ref<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. redirectresponse: setswindow.locationtourl.- Otherwise, if
reset: reloads the table and, unlesskeepSelection, clears the selection. UsekeepSelection: trueto apply several bulk actions in a row to the same rows. - Errors are not caught:
useApishows its toast and the promise rejects.
await executeAction('export', { format: 'xlsx' }, false) // no reload
await executeAction('archive', {}, true, true) // reload, keep selected rows