Skip to main content
Version: v1 (Current)

Vue composables: search and row selection

This guide covers two dashboard helpers under resources/js/Composables. They depend on the platform's Vue/browser context and have different export/signature contracts. They are not general portal API clients. For named routes and HTTP context, see routing; for the table that consumes them, see DataTable.

Import the default useSearch export and call useSearch(props, emit) inside component setup. The previous no-argument example cannot initialize the current implementation.

The helper returns searchProxy, previousSearch, searchBoxClosed and debouncedSearch. Initial searchProxy uses props.search || getParam('search'), so an empty/falsy supplied search can fall back to the browser URL query. URL utilities read window.location; this is not a server-rendering-safe initialization contract.

A watcher mirrors later props.search changes into the proxy. The helper does not watch the proxy to schedule a search automatically. The consuming DataTable installs that watcher and passes its own fetchData callback.

A small wrapper that exposes the same scheduling contract without performing a network request is:

<template>
<input v-model="searchProxy" aria-label="Search" />
</template>

<script setup>
import { watch } from 'vue'
import useSearch from '@/Composables/useSearch'

const props = defineProps({
search: { type: String, default: '' },
options: { type: Object, required: true },
})
const emit = defineEmits(['update:search', 'search-requested'])
const { searchProxy, debouncedSearch } = useSearch(props, emit)

watch(searchProxy, value => {
debouncedSearch(value, nextValue => emit('search-requested', nextValue))
})
</script>

The parent must supply options.searchRoute for the current callback gate, bind v-model:search if it wants the emitted search value and handle search-requested if it wants to fetch data. Supply the real intended route/configuration; this example's event is not a backend search implementation. Avoid also fetching from an unrelated watcher unless two requests are intended.

Timing and callback behavior​

debouncedSearch(value, callback) uses a 250 ms Lodash debounce. When it runs, it emits update:search first, checks props.options.searchRoute, calls the supplied callback and then updates previousSearch.

  • Provide an options object. If the property itself is absent, accessing props.options.searchRoute can throw after the update event.
  • If the route setting is falsy, it logs an error and returns after emitting; the callback does not run and previousSearch is not advanced.
  • A truthy setting permits the callback; it does not validate a URL, invoke a permission check or send a request itself.
  • The callback is invoked without awaiting a returned promise. previousSearch is not evidence of completed network delivery or a successful backend result. A synchronous callback throw prevents the following previousSearch assignment and is not caught here. A returned promise rejection is not caught or awaited here either; the callback/request owner must handle it.
  • The helper contains an internal performSearch function using props.searchRoute and Inertia, but it is neither returned nor called by the exposed debounce path. Do not describe props.searchRoute alone as automatic fetching.

The returned debounced function has Lodash's cancellation/flush methods, but this helper does not register an unmount cancellation hook. If a wrapper owns the debounce lifecycle, cancel pending work when that component is disposed as appropriate. This does not cancel an already-sent network request. Handle stale responses, request errors, loading and pagination/query state in the actual fetch owner.

Checkbox selection​

Use the named export useCheckboxGroup, passing a Vue ref whose value is an array of rows with unique id values:

import { ref } from 'vue'
import { useCheckboxGroup } from '@/Composables/useCheckboxGroup'

const rows = ref([{ id: 1 }, { id: 2 }])
const { selectedItems, toggleAllEnabled, isIndeterminate } =
useCheckboxGroup(rows)

selectedItems is a writable computed array of selected IDs. Bind individual row checkboxes to that array with the row ID as their value. Bind the header checkbox to toggleAllEnabled and its DOM indeterminate property to isIndeterminate using the consuming checkbox component's supported interface.

The full signature is useCheckboxGroup(items, props = {}, emit = () => {}). Initial props.selected arrays are used as the initial selection; a truthy scalar becomes a one-item array. Later changes to props.selected are not mirrored by a dedicated watcher. The helper watches selection changes and emits update:selected; it does not itself synchronize every parent prop update back into local state.

Visible rows and single selection​

Header state is derived from the current items.value, not a stored select-all flag. ID comparisons use String(id) to accommodate numeric/string representations, but returned IDs are not universally normalized to one type.

  • Header checked means the visible list is nonempty and every visible row is selected.
  • Indeterminate means at least one visible row is selected but not every visible row.
  • Selecting all replaces selection with the current visible row IDs. Clearing all replaces it with an empty array. These operations do not preserve an off-page selection union.
  • IDs already selected outside the visible list can remain in selectedItems until an operation replaces them; they do not make the current header checked.
  • An empty visible list yields unchecked/non-indeterminate header state, even if off-page IDs remain selected.

props.options.singleSelect is read at initialization. In that mode, assignments through selectedItems retain only the last ID. Header select-all also goes through that setter, so it does not select a multirow page in single-select mode. The helper does not promise reactive mode changes or normalize every initial selection to one item.

Do not label this helper's header “select all matching results” across server pagination. Bulk selection of a query population is a separate bulk-action contract. Selection state is also not authorization: the backend must validate every requested resource and action.

Verify and troubleshoot​

SymptomInspect
Search initialization throwsRequired props/emitter and browser URL context
Typing changes input but nothing emitsConsumer watcher and explicit debouncedSearch call
Update emits but fetch callback never runsoptions object, options.searchRoute and callback argument
Search value appears complete but request still pendingCallback promise/loading state; previousSearch is not network success
Old search runs after navigationOwning component cancellation and already-started request handling
Header looks wrong after page changeCurrent visible rows, ID uniqueness/types and off-page selection
Select-all loses earlier pagesReplacement semantics; implement a separate intentional cross-page selection model
Single-select header chooses one rowSetter keeps the last assigned ID; use appropriate single-row controls
Parent selection change does not appearInitialization-only selected prop and explicit consumer synchronization

Test debounce timing, missing route/configuration, callback failures, incoming search changes, empty/paginated lists, numeric/string IDs and the chosen selection mode. Keep UI behavior tests separate from backend search, pagination and denied-resource acceptance.