Skip to content

useDataTable

js
import { useDataTable } from '@aaix/laravel-islands-datagrid/vue';

const table = useDataTable(dataUrl, options);

Options

OptionTypeDefaultPurpose
defaultsobject{}Required. Every state key with its default value. Defines what is sent to the server and what appears in the URL.
initialobject{}Server-provided starting state, merged over defaults.
clientOnlystring[][]Keys kept in state and URL but never sent to the server.
filterKeysstring[][]Keys counted by activeFilterCount and cleared by resetFilters().
filterParamsstring[][]Keys transported as filter[key]=value instead of a bare parameter — and written to the URL that way.
searchKeystring'q'State key driven by onSearchInput.
searchDelaynumber350Debounce for onSearchInput, in milliseconds.
httpobjectbuilt-in fetch clientHTTP client. Anything exposing get(url, { params }) that resolves to { data }.

Returned state

NameTypeDescription
statereactiveThe live table state. Read it in templates, mutate it for domain rules.
rowsref<array>data.rows from the last successful response.
metaref<object>data.meta from the last successful response.
payloadref<object>The whole data object — tab counts, groups, aggregates.
loadingref<boolean>A request is in flight.
errorref<boolean>The last request failed.
activeFilterCountcomputed<number>How many filterKeys differ from their default.

Returned methods

MethodDescription
fetchData()Performs the request. Call once on mount.
reload({ resetPage, push })Syncs the URL and refetches. Pass resetPage: true after changing a filter, push: false to replace the history entry instead of adding one.
syncUrl({ push })Writes the state into the address bar without fetching — how a client-only change (columns, card size) becomes deep-linkable. Replaces the entry unless push: true.
onSearchInput(value)Writes to searchKey and reloads after the debounce.
clearSearch()Clears searchKey immediately, without waiting for the debounce.
setFilter(key, value)Sets a key and reloads from page one.
resetFilters(keys?)Restores filterKeys (or the given keys) to their defaults. Reloads only if something changed.
setSort(field)Sorts by field; toggles asc/desc when already sorted by it.
goToPage(page)Changes the page and reloads.
setPerPage(perPage)Changes page size and reloads from page one.

Behaviour worth knowing

Stale responses are discarded. Every request carries an id; a response whose id is no longer current is dropped. Typing quickly can never leave an older result on screen.

The URL carries only what differs. A key equal to its default is omitted, so a pristine table has a clean URL and initial can restore any state from a shared link. Booleans are written as 1 and 0.

Errors keep the shell. On failure rows is emptied and error is set; the toolbar and pagination stay mounted, so a retry never remounts the page.

Released under the MIT License.