CrudSearch 搜索表单
配置搜索字段,点击搜索提交条件快照,重置恢复字段初值。在 CrudPage 中传入 searchConfig 即可自动联动,以下示例用于独立使用。
基础用法
vue
<template>
<CrudSearch
ref="searchRef"
:search-config="searchConfig"
@search="search"
@reset="resetSearch"
/>
</template>
<script setup lang="ts">
import type { CrudSearchConfig } from "@/components/Crud/types";
const searchConfig: CrudSearchConfig = {
fields: [
{
prop: "keywords",
label: "关键词",
type: "input",
attrs: { placeholder: "用户名或昵称", clearable: true },
},
{
prop: "status",
label: "状态",
type: "select",
initialValue: null,
options: [
{ label: "启用", value: 1 },
{ label: "禁用", value: 0 },
],
attrs: { clearable: true },
},
{
prop: "createTime",
label: "创建时间",
type: "date-picker",
initialValue: [],
attrs: {
type: "daterange",
valueFormat: "YYYY-MM-DD",
startPlaceholder: "开始日期",
endPlaceholder: "结束日期",
},
},
],
};
</script>输入在点击搜索后才应用;刷新、翻页和导出不读取未提交的输入。字段超过 showNumber 时自动出现展开/收起。
自定义控件
字段配置 type: "custom",通过插槽渲染业务控件:
typescript
const searchConfig: CrudSearchConfig = {
fields: [{ prop: "gender", label: "性别", type: "custom", initialValue: null }],
};vue
<CrudSearch :search-config="searchConfig" @search="search" @reset="resetSearch">
<template #gender="{ formData, prop, attrs }">
<DictSelect v-model="formData[prop]" code="gender" v-bind="attrs" />
</template>
</CrudSearch>插槽名默认为 prop,可用 slotName 指定。自定义控件需绑定 attrs 以应用宽度等属性。
异步选项
onInit 在组件创建时调用一次,支持返回 Promise;失败时搜索区显示错误并可重试:
typescript
{
prop: "roleIds",
label: "角色",
type: "select",
initialValue: [],
attrs: { multiple: true },
async onInit(field) {
field.options = await RoleAPI.getOptions();
},
}搜索和表单共用同一组选项时,可由页面请求一次,将 Ref 传给各字段的 options。
API
Attributes
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| search-config | 搜索配置 | CrudSearchConfig | — |
SearchConfig
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| fields | 搜索字段 | CrudField[] | [] |
| showNumber | 收起时显示的字段数 | number | 3 |
| isExpandable | 是否允许展开/收起 | boolean | true |
| grid | 自适应网格布局,right 将按钮靠右 | boolean | 'left' | 'right' | false |
| form | Element Plus Form 属性 | FormProps | — |
| cardAttrs | Element Plus Card 属性与样式 | CardProps & { style } | — |
| colon | 标签末尾添加冒号 | boolean | false |
网格布局
开启 grid 后字段宽度由网格控制,不要再给控件设置固定宽度。
CrudField
搜索与表单共用的字段配置,表单额外属性见 CrudForm。
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| prop | 键名 | string | — |
| label | 标签文本 | string | — |
| type | 控件类型 | 见下方控件类型 | — |
| tips | 标签旁的问号提示 | string | TooltipProps | — |
| attrs | 控件原生属性 | Record<string, any> | — |
| events | 控件原生事件 | Record<string, Function> | — |
| options | 选项,适用于 select / radio / checkbox | Array | Ref<Array> | — |
| rules | 校验规则 | FormItemRule[] | — |
| initialValue | 初始值,重置时恢复 | unknown | 按控件类型 |
| slotName | custom 控件的插槽名 | string | prop |
| hidden | 是否隐藏 | boolean | false |
| col | Element Plus Col 属性 | ColProps | — |
| onInit | 一次性初始化,支持 Promise | (field) => void | Promise | — |
未指定 initialValue 时:标签、级联和多选控件为 [],数字控件为 null,其他为 ""。初值深拷贝,实例间不共享。
搜索控件类型:input、select、input-number、cascader、tree-select、date-picker、time-picker、time-select、input-tag、custom-tag、custom。
Events
| 事件名 | 说明 | 回调参数 |
|---|---|---|
| search | 点击搜索,提交条件快照 | queryParams: CrudRecord |
| reset | 点击重置,提交恢复初值后的快照 | queryParams: CrudRecord |
Slots
| 插槽名 | 说明 | 参数 |
|---|---|---|
| {prop} 或 | custom 字段的控件 | { formData, prop, attrs } |
| label- | 字段标签 | { label, formData, prop } |
Exposes
| 名称 | 说明 | 类型 |
|---|---|---|
| getQueryParams | 返回当前搜索草稿的副本 | () => CrudRecord |
| reset | 恢复初值并返回快照,不触发事件 | () => CrudRecord |
| toggleVisible | 切换搜索区域显示 | () => void |
| initializeFields | 重新加载字段选项 | () => Promise<void> |
