Element-plus封装搜索组件

在这里插入图片描述
在这里插入图片描述

<template>
    <div>
        <el-form ref="searchRef" :size="props.size" :model="searchVal" :label-width="props.labelWidth">
            <el-row :gutter="20">       
                <el-col :xs="24" :sm="12" :md="12" :lg="props.span" v-for="(item, index) in props.option" :key="index" class="mb20">
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" :lg="props.span">         
                    <el-form-item>            
                        <el-button type="primary" @click="search">搜索</el-button>          
                        <el-button @click="reset(searchRef)">重置</el-button>          
                    </el-form-item>       
                </el-col>
            </el-row>
        </el-form>
    </div>
</template>

<script lang='ts' setup>
    import {
    onMounted, reactive, ref } from 'vue'
    import type {
    ElForm } from 'element-plus'
    import {
    ElMessage } from 'element-plus'
    import {
    formatDate } from '@/utils/formatTime'
    import type {
    Option } from '@/components/type.d.ts'

    type FormInstance = InstanceType<typeof ElForm>
        const searchRef = ref<FormInstance>()
    // 搜索值
    let searchVal: any = reactive({
   })
    // 搜索
    const search = () => {
   
        for (let i in searchVal) {
   
            if (searchVal[i] === undefined || searchVal[i] === '') {
   
                delete searchVal[i]
            }
        }
        emit('search', searchVal)
    }
    // 重置
    const reset = (formEl: FormInstance | undefined) => {
   
        if (!formEl) return
        formEl.resetFields()
        emit('search', searchVal)
    }
    interface Props {
   
        labelWidth?: number | string
        size?: 'small' | 'default' | 'large'
        span?: number
        option: Option[]
        defaultValue?: any
    }
    const props = withDefaults(defineProps<Props>(), {
   
        size: 'default',
        span: 8,
        labelWidth: 100
    })
</script>

在这里插入图片描述

//选择框的option类型
interface SelectOption {
   
  label: string
  value: any
}
//选择框和文本输入框的option类型
interface InputOptions {
   
  prop: string
  option: SelectOption[]
}

export interface Option {
   
  label: string //标签名称
  prop: string //表单项字段名
  endProp?: string //日期范围的结束日期字段名
  placeholder?: string //输入框占位文本
  type?: 'input' | 'select' | 'cascader' | 'date' | 'datetime' | 'daterange' | 'inputSelect' //表单项类型
  optionLabel?: string //选择框的label字段
  optionValue?: string //选择框的value字段
  option?: any[] //选择框option
  inputOptions?: InputOptions //选择框和文本输入框的option
  max?: number //级联选择器的最大选择数量
  children?: string //级联选择器children字段名
  multiple?: boolean //级联选择器是否多选
  value?: string //级联选择器返回值字段名
  itemLabel?: string //级联选择器选项名称
  checkStrictly?: boolean //是否可选择父节点和子节点
  emitPath?: boolean // 是否返回由该节点所在的各级菜单的值所组成的数组
  show?: boolean //是否显示选中值的完整路径
  filterable?: boolean //选择框是否可搜索
}

在这里插入图片描述

<template>
    <div>
        <el-form ref="searchRef" :size="props.size" :model="searchVal" :label-width="props.labelWidth">
            <el-row :gutter="20">
                <el-col :xs="24" :sm="12" :md="12" :lg="props.span" v-for="(item, index) in props.option" :key="index" class="mb20">
                    <el-form-item v-if="item.type == 'input' || !item.type" @keyup.enter="search" :prop="item.prop" :label="item.label">
                        <el-input v-model="searchVal[item.prop]" :placeholder="item.placeholder"></el-input>
                    </el-form-item>
                    <el-form-item v-
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JackieChan_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值