Merge pull request #111 from cshaptx4869/patch-70

refactor(PageSearch): ♻️ 优化加强initFn函数
This commit is contained in:
Ray Hao
2024-05-23 16:09:54 +08:00
committed by GitHub
3 changed files with 24 additions and 43 deletions

View File

@@ -5,10 +5,7 @@
v-hasPerm="[`${searchConfig.pageName}:query`]" v-hasPerm="[`${searchConfig.pageName}:query`]"
> >
<el-form ref="queryFormRef" :model="queryParams" :inline="true"> <el-form ref="queryFormRef" :model="queryParams" :inline="true">
<template <template v-for="(item, index) in formItems" :key="item.prop">
v-for="(item, index) in searchConfig.formItems"
:key="item.prop"
>
<el-form-item <el-form-item
v-show="isExpand ? true : index < showNumber" v-show="isExpand ? true : index < showNumber"
:label="item.label" :label="item.label"
@@ -53,7 +50,7 @@
<el-button icon="refresh" @click="handleReset">重置</el-button> <el-button icon="refresh" @click="handleReset">重置</el-button>
<!-- 展开/收起 --> <!-- 展开/收起 -->
<el-link <el-link
v-if="isExpandable && searchConfig.formItems.length > showNumber" v-if="isExpandable && formItems.length > showNumber"
class="ml-2" class="ml-2"
type="primary" type="primary"
:underline="false" :underline="false"
@@ -69,7 +66,7 @@
<script setup lang="ts"> <script setup lang="ts">
import type { FormInstance } from "element-plus"; import type { FormInstance } from "element-plus";
import { reactive, ref, onMounted } from "vue"; import { reactive, ref } from "vue";
// 对象类型 // 对象类型
type IObject = Record<string, any>; type IObject = Record<string, any>;
@@ -91,14 +88,15 @@ export interface ISearchConfig {
initialValue?: any; initialValue?: any;
// 可选项(适用于select组件) // 可选项(适用于select组件)
options?: { label: string; value: any }[]; options?: { label: string; value: any }[];
// 初始化数据函数扩展适用初始化options // 初始化数据函数扩展
initFn?: Function; initFn?: (formItem: IObject) => void;
}>; }>;
// 是否开启展开和收缩 // 是否开启展开和收缩
isExpandable?: boolean; isExpandable?: boolean;
// 默认展示的表单项数量 // 默认展示的表单项数量
showNumber?: number; showNumber?: number;
} }
interface IProps { interface IProps {
searchConfig: ISearchConfig; searchConfig: ISearchConfig;
} }
@@ -109,8 +107,11 @@ const emit = defineEmits<{
resetClick: [queryParams: IObject]; resetClick: [queryParams: IObject];
}>(); }>();
const queryFormRef = ref<FormInstance>();
// 是否显示 // 是否显示
const visible = ref(true); const visible = ref(true);
// 响应式的formItems
const formItems = reactive(props.searchConfig.formItems);
// 是否可展开/收缩 // 是否可展开/收缩
const isExpandable = ref(props.searchConfig.isExpandable ?? true); const isExpandable = ref(props.searchConfig.isExpandable ?? true);
// 是否已展开 // 是否已展开
@@ -120,14 +121,13 @@ const showNumber = computed(() => {
if (isExpandable.value === true) { if (isExpandable.value === true) {
return props.searchConfig.showNumber ?? 3; return props.searchConfig.showNumber ?? 3;
} else { } else {
return props.searchConfig.formItems.length; return formItems.length;
} }
}); });
const queryFormRef = ref<FormInstance>();
// 搜索表单数据 // 搜索表单数据
const queryParams = reactive<IObject>({}); const queryParams = reactive<IObject>({});
for (const item of props.searchConfig.formItems) { for (const item of formItems) {
item.initFn && item.initFn(item);
queryParams[item.prop] = item.initialValue ?? ""; queryParams[item.prop] = item.initialValue ?? "";
} }
// 重置操作 // 重置操作
@@ -148,12 +148,6 @@ function toggleVisible() {
visible.value = !visible.value; visible.value = !visible.value;
} }
onMounted(() => {
props.searchConfig.formItems.forEach((item) => {
item.initFn && item.initFn();
});
});
// 暴露的属性和方法 // 暴露的属性和方法
defineExpose({ getQueryParams, toggleVisible }); defineExpose({ getQueryParams, toggleVisible });
</script> </script>

View File

@@ -1,3 +1,4 @@
import DeptAPI from "@/api/dept";
import type { ISearchConfig } from "@/components/PageSearch/index.vue"; import type { ISearchConfig } from "@/components/PageSearch/index.vue";
const searchConfig: ISearchConfig = { const searchConfig: ISearchConfig = {
@@ -21,22 +22,7 @@ const searchConfig: ISearchConfig = {
prop: "deptId", prop: "deptId",
attrs: { attrs: {
placeholder: "请选择", placeholder: "请选择",
data: [ data: [],
{
value: 1,
label: "有来技术",
children: [
{
value: 2,
label: "研发部门",
},
{
value: 3,
label: "测试部门",
},
],
},
],
filterable: true, filterable: true,
"check-strictly": true, "check-strictly": true,
"render-after-expand": false, "render-after-expand": false,
@@ -45,6 +31,11 @@ const searchConfig: ISearchConfig = {
width: "150px", width: "150px",
}, },
}, },
async initFn(formItem) {
formItem.attrs.data = await DeptAPI.getOptions();
// 注意:如果initFn函数不是箭头函数,this会指向此配置项对象,那么也就可以用this来替代形参formItem
// this.attrs!.data = await DeptAPI.getOptions();
},
}, },
{ {
type: "select", type: "select",
@@ -57,15 +48,10 @@ const searchConfig: ISearchConfig = {
width: "100px", width: "100px",
}, },
}, },
options: [], options: [
initFn() {
setTimeout(() => {
this.options = [
{ label: "启用", value: 1 }, { label: "启用", value: 1 },
{ label: "禁用", value: 0 }, { label: "禁用", value: 0 },
]; ],
}, 300);
},
}, },
{ {
type: "date-picker", type: "date-picker",

View File

@@ -57,6 +57,7 @@ import type { IObject, IOperatData } from "@/hooks/usePage";
import usePage from "@/hooks/usePage"; import usePage from "@/hooks/usePage";
import addModalConfig from "./config/add"; import addModalConfig from "./config/add";
import contentConfig from "./config/content"; import contentConfig from "./config/content";
// import contentConfig from "./config/content2";
import editModalConfig from "./config/edit"; import editModalConfig from "./config/edit";
import searchConfig from "./config/search"; import searchConfig from "./config/search";