Query submission and reset
Generate query controls from a schema or column configuration with the responsive QueryFilter or the popup-based LightFilter. Both use the state protocol described on this page and support validation before submission. See Schema query.
useTableQuery gives an existing form a draft, a submitted snapshot, field transforms and the expand / collapse state. Inputs, date ranges and layout keep using the Ant Design Vue or Element Plus your project already integrates; the composable does not generate a form from columns.
This page lists the complete useTableQuery API. When using VTableQuery, call submit() / reset() through the component ref from query buttons and application code to apply validation and preserve field processing. See Component methods and query state methods for the comparison and a calling example.
The query interaction is aligned with the Ant Design ProTable search form: input changes do not query on their own, and every click on search or reset sends a request; field transforms correspond to search.transform, whole-request transforms keep using useTableRequest.beforeSearchSubmit, and the expanded state corresponds to collapsed / onCollapse. Here the form state is split out into a Vue composable, while the controls and form validation are left to the host UI library.
The example below uses native inputs so that the docs site does not depend on a whole extra UI library. In a real project you can replace input with a-input / el-input and the range field with the host DatePicker; just bind the same draft and submit methods. The two presets do not need different data protocols.
Draft and submitted params
Bind the controls to draft. Editing the draft, toggling collapsed and calling resetFields() never send a request. Only submit() / reset() invoke a callback, and each call invokes exactly one callback.
const table = useTableRequest<Row, Params>({
request,
manualRequest: true,
})
const query = useTableQuery<FormValues, Params>({
defaultValues: { keyword: '', range: undefined },
fieldTransforms: {
range: (range) => range && { start: range[0], end: range[1] },
},
transform: (params) => ({ ...params, keyword: params.keyword?.trim() }),
onSubmit: (params) => table.submit(params),
onReset: (params) => table.reset(params),
})submitted holds a snapshot of the form values from the most recent successful transform, and params holds the snapshot of the corresponding request fields; both are published before onSubmit / onReset is called and are undefined until the first successful transform. Plain objects, arrays and Dates are snapshotted independently between the draft, the default values and the callback arguments. Do not use custom class instances with internal mutable state as form values.
A failed submit does not roll the draft back automatically. When a transform throws, the previous submitted / params are kept; when the callback throws or the Promise it returns rejects, the snapshots already published for this call are not rolled back, so read the error from error and retry. submit() / reset() return Promise<boolean>: true when the current call completes successfully, and false when a transform or the callback fails, when the call is superseded by a newer submit or when the scope has already been disposed. The failure of a stale callback does not overwrite the error state of a newer submit.
onSubmit / onReset receive only the transformed params and the form snapshot; they are not given an AbortSignal. Cancelling a standalone request callback is managed by the application; once connected to useTableRequest, the request manager handles the cancellation when a new request supersedes an old one.
useTableRequest itself reports network errors through error and settles its Promise normally, so in the combined setup you should still read the request result from table.error; the query's return value only reflects how the query transforms and the callback ran.
Field param transforms
- Fields without a
fieldTransformsentry keep their original key and value. - Fields with a transform have their original key removed; the function can return several request fields, or return
undefinedto omit the field. - All field transforms are merged in the key order of the config and override untransformed fields of the same name; later transforms override earlier results.
- Finally the overall
transform(params, values)is called, which suits uniform processing such as trimming and removing empty values.
Field transforms run only on submit and reset. Collapsing the form changes only the layout state, and the fields in the collapsed area still take part in the submit; whether to clear or omit them is decided by the application's field transforms.
Once connected to useTableRequest, its param merge order is still defaultParams, the params of this submit, then the extra params, after which beforeSearchSubmit runs. The pagination fields are written by the request manager. Avoid putting the same param in both the reactive extra params and the draft; otherwise a change to the extra param still triggers a request under the request manager's protocol.
Reset conventions
| Action | Draft | Submitted params | Request and table state |
|---|---|---|---|
resetFields() | Restored to the initial defaults | Kept | No request |
submit() | Kept | Transformed from the current draft | Calls onSubmit; when wired to table.submit, returns to page 1 and keeps sorting and filters |
reset() | Restored to the initial defaults | Transformed from the defaults | Calls onReset, or onSubmit when it is not provided; when wired to table.reset(params), returns to page 1 and clears sorting and filters |
reset() does not change the expanded state, row selection, tree node expansion or column preferences; the application handles those per scenario. defaultValues is snapshotted at creation, so later external changes do not alter the reset baseline.
useTableQuery itself does not send a first request. To load the default query initially, use manualRequest: true and call query.submit() once after initialization; avoid having the request manager load automatically while you also submit the default values manually.
When created inside a component setup or an effectScope, disposing the scope stops the watcher on the expanded state, clears the submitting state and ignores the results of stale callbacks; submits and resets after that return false. The query protocol is not responsible for cancelling application requests made inside the callbacks; once connected to useTableRequest, the request manager cancels them when the same scope is disposed. Do not create query state that has to be released with the page at the top level of a module, where there is no scope.
API
TValues is the form value type and TParams is the request param type, which defaults to TValues.
useTableQuery options
| Option | Type | Default | Description |
|---|---|---|---|
defaultValues | TValues | Required | Form defaults and the reset baseline; a snapshot is taken on creation. |
fieldTransforms | TableQueryFieldTransforms<TValues, TParams> | - | Per-field transforms, each returning a fragment of the request params. |
transform | (params: Partial<TParams>, values: Readonly<TValues>) => Partial<TParams> | - | Transform of the whole params after the field transforms; runs once per submit or reset. |
onSubmit | (params: Partial<TParams>, values: Readonly<TValues>) => void | Promise<void> | - | Called on submit with the transformed params and the form snapshot of that submission. |
onReset | (params: Partial<TParams>, values: Readonly<TValues>) => void | Promise<void> | onSubmit | Called on reset; onSubmit is called when it is not provided. |
collapsed | Ref<boolean> | - | Shared collapsed state; takes precedence over defaultCollapsed when passed. |
defaultCollapsed | boolean | true | Initial collapsed state. |
onCollapse | (collapsed: boolean) => void | - | Called when the collapsed state changes. |
useTableQuery return value
| Property or method | Type | Description |
|---|---|---|
draft | Ref<TValues> | Form draft; bind it to the host controls. |
submitted | ComputedRef<Readonly<TValues> | undefined> | Form values of the last successful transform. |
params | ComputedRef<Readonly<Partial<TParams>> | undefined> | Request params of the last submission. |
collapsed | Ref<boolean> | Collapsed state. |
submitting | ComputedRef<boolean> | A submit or reset callback is running. |
error | ShallowRef<Error | null> | Error from the last transform or callback. |
submit | (options?: TableQuerySubmitOptions<TValues>) => Promise<boolean> | Transforms the draft and calls onSubmit. Returns false when the transform or callback fails, or when a newer submission supersedes it. options.omitFields omits fields from the request params. |
reset | (options?: TableQuerySubmitOptions<TValues>) => Promise<boolean> | Restores the defaults and submits once, calling onReset. |
resetFields | () => void | Restores the draft only; the submitted state is unchanged and no request is made. |
toggleCollapsed | () => void | Toggles the collapsed state without triggering a query. |
When the application builds its own form, call the host form's validate first, then query.submit() once it succeeds. When using the host validation, custom validate or preserve settings of VTableQuery, call submit() through the component ref; the component completes those steps before calling the query state method.
Type Declarations
View type declarations
export declare function useTableQuery<TValues extends object, TParams extends object = TValues>(
options: UseTableQueryOptions<TValues, TParams>,
): UseTableQueryReturn<TValues, TParams>
/** A transformed field replaces its original key and may expand into several request fields. */
export type TableQueryFieldTransforms<TValues extends object, TParams extends object> = {
[K in keyof TValues]?: (
value: TValues[K],
values: Readonly<TValues>,
) => Partial<TParams> | undefined
}
/** Omit hidden fields from request parameters without changing the form draft or submitted values. */
export interface TableQuerySubmitOptions<TValues extends object> {
omitFields?: readonly (keyof TValues)[]
}
export interface UseTableQueryOptions<TValues extends object, TParams extends object = TValues> {
/** A snapshot is taken once. Use ordinary objects, arrays and Date values for form state. */
defaultValues: TValues
fieldTransforms?: TableQueryFieldTransforms<TValues, TParams>
/** Runs after field transforms, once per submit/reset, never on draft edits. */
transform?: (params: Partial<TParams>, values: Readonly<TValues>) => Partial<TParams>
onSubmit?: (params: Partial<TParams>, values: Readonly<TValues>) => void | Promise<void>
/** Defaults to onSubmit. Connect this to request.reset(params) to also clear table state. */
onReset?: (params: Partial<TParams>, values: Readonly<TValues>) => void | Promise<void>
/** Optional shared ref for a controlled collapse state. */
collapsed?: Ref<boolean>
defaultCollapsed?: boolean
onCollapse?: (collapsed: boolean) => void
}
export interface UseTableQueryReturn<TValues extends object, TParams extends object = TValues> {
draft: Ref<TValues>
/** Last successfully transformed values, published before the callback and retained if it fails. */
submitted: ComputedRef<Readonly<TValues> | undefined>
params: ComputedRef<Readonly<Partial<TParams>> | undefined>
collapsed: Ref<boolean>
submitting: ComputedRef<boolean>
error: ShallowRef<Error | null>
/** Returns false for a transform/callback failure or an obsolete submission. */
submit: (options?: TableQuerySubmitOptions<TValues>) => Promise<boolean>
/** Restore draft defaults and submit them through onReset (or onSubmit), once. */
reset: (options?: TableQuerySubmitOptions<TValues>) => Promise<boolean>
/** Restore draft defaults without changing submitted state or requesting data. */
resetFields: () => void
toggleCollapsed: () => void
}