chenyc
2025-07-14 05c827fea632f004821cc746ba73880769fab7cd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<template>
    <div class="table-search-container" v-if="props.search.length > 0">
        <el-form ref="tableSearchRef" :model="state.form" size="default" label-width="100px" class="table-form">
            <el-row>
                <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20" v-for="(val, key) in search" :key="key" v-show="key === 0 || state.isToggle">
                    <template v-if="val.type !== ''">
                        <el-form-item
                            :label="val.label"
                            :prop="val.prop"
                            :rules="[{ required: val.required, message: `${val.label}不能为空`, trigger: val.type === 'input' ? 'blur' : 'change' }]"
                        >
                            <el-input v-model="state.form[val.prop]" :placeholder="val.placeholder" clearable v-if="val.type === 'input'" style="width: 100%" />
                            <el-date-picker
                                v-model="state.form[val.prop]"
                                type="date"
                                :placeholder="val.placeholder"
                                v-else-if="val.type === 'date'"
                                style="width: 100%"
                            />
                            <el-select v-model="state.form[val.prop]" :placeholder="val.placeholder" v-else-if="val.type === 'select'" style="width: 100%">
                                <el-option v-for="item in val.options" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                            </el-select>
                        </el-form-item>
                    </template>
                </el-col>
                <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20">
                    <el-form-item class="table-form-btn" :label-width="search.length <= 1 ? '10px' : '100px'">
                        <template #label v-if="search.length > 1">
                            <div class="table-form-btn-toggle ml10" @click="state.isToggle = !state.isToggle">
                                <span>{{ state.isToggle ? '收筛选' : '展筛选' }}</span>
                                <SvgIcon :name="state.isToggle ? 'ele-ArrowUp' : 'ele-ArrowDown'" />
                            </div>
                        </template>
                        <div>
                            <el-button size="default" type="primary" @click="onSearch(tableSearchRef)">查询 </el-button>
                            <el-button size="default" type="info" class="ml10" @click="onReset(tableSearchRef)"> 重置 </el-button>
                        </div>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
    </div>
</template>
 
<script setup lang="ts" name="makeTableDemoSearch">
import { reactive, ref, onMounted } from 'vue';
import type { FormInstance } from 'element-plus';
 
// 定义父组件传过来的值
const props = defineProps({
    // 搜索表单
    search: {
        type: Array<TableSearchType>,
        default: () => [],
    },
});
 
// 定义子组件向父组件传值/事件
const emit = defineEmits(['search']);
 
// 定义变量内容
const tableSearchRef = ref<FormInstance>();
const state = reactive({
    form: {},
    isToggle: false,
});
 
// 查询
const onSearch = (formEl: FormInstance | undefined) => {
    if (!formEl) return;
    formEl.validate((valid: boolean) => {
        if (valid) {
            emit('search', state.form);
        } else {
            return false;
        }
    });
};
// 重置
const onReset = (formEl: FormInstance | undefined) => {
    if (!formEl) return;
    formEl.resetFields();
    emit('search', state.form);
};
// 初始化 form 字段,取自父组件 search.prop
const initFormField = () => {
    if (props.search.length <= 0) return false;
    props.search.forEach((v) => (state.form[v.prop] = ''));
};
// 页面加载时
onMounted(() => {
    initFormField();
});
</script>
 
<style scoped lang="scss">
.table-search-container {
    display: flex;
    .table-form {
        flex: 1;
        .table-form-btn-toggle {
            white-space: nowrap;
            user-select: none;
            display: flex;
            align-items: center;
            color: var(--el-color-primary);
        }
    }
}
</style>