Vue3.0使用Element-ui制作searchForm组件

        大家好,我是一个8年前端开发经验的新博主(实战react方向较多),最近两年互联网行业动荡,工作不稳定,想积累点自己的项目实践经验以备不时之需。

       第二弹!分享自定义搜索表单组件<SearchForm/>,使用vue3.0二次封装过的搜索表单组件,喜欢的记得点个赞哦~

知识点:

1、二次封装searchForm,减少重复标签

2、搜索表单以对象形式渲染,根据type类型区分标签

3、使用moment转换日期类型数据

直接先看效果图:(前提先安装UI组件库:npm element-plus --save;时间置换依赖包:npm moment --save)

首先,直接看一下我们封装完成的SearchForm组件代码!

<template>
    <el-form 
      ref="formRef"
      class="search-form"
      :model="formValues"
      inline
    >
      <el-form-item
        v-for="item in itemList"
        :key="item.name"
        :prop="item.name"
        :label="item.label"
      >
        <template v-if="item.type === 'input'">
            <el-input
              :style="item.style || 'width: 220px'"
              :placeholder="item.placeholder || '请输入'+item.label"
              :disabled="item.disabled"
              v-model.trim="formValues[item.name]"
              clearable
            ></el-input>
        </template>
        <template v-if="item.type === 'select'">
            <el-select
              :style="item.style || 'width: 220px'"
              :placeholder="item.placeholder || '请选择'+item.label"
              :disabled="item.disabled"
              v-model.trim="formValues[item.name]"
              clearable
            >
                <el-option
                    v-for="option in item.option"
                    :key="item.name + option.value"
                    :value="option.value"
                    :label="option.label"
                    clearable
                />
            </el-select>
        </template>
        <template v-if="item.type === 'date'">
            <el-date-picker
                :style="item.style || 'width: 220px'"
                :placeholder="item.placeholder || '请选择'+item.label"
                :disabled="item.disabled"
                :type="item.dateType"
                v-model.trim="formValues[item.name]"
                clearbale
            />
        </template>
        <template v-if="item.type === 'daterange'">
            <el-date-picker
                :style="item.style || 'width: 220px'"
                :range-separator="item.rangeSeparator"
                :disabled="item.disabled"
                :type="item.dateType"
                :format="item.format"
                v-model.trim="formValues[item.name]"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                clearable
            />
        </template>
      </el-form-item>
      <el-form-item>
        <el-button @click="searchForm(formRef)" type="primary" icon="Search" :loading="loading">查询</el-button>
        <el-button @click="resetForm(formRef)" type="primary" plain icon="Refresh" v-if="props.isReset">重置</el-button>
      </el-form-item>
    </el-form>
</template>

<script setup>
  import { ref, onMounted, watch } from 'vue';
  import moment from 'moment'

  let formValues = ref({});
  const formRef = ref();
  const emit = defineEmits(['handleSearch','handleReset']);
  const props = defineProps({
    itemList: {
        type: Array,
        default: {
          type: 'input', // 如type为'date',可另外接收dateType属性作为日期选择器类型:'date'|'week|'month'|'year'|'dates'(日期多选)
          label: '标题',
          name: 'keyword',
          placeholder: '请输入关键字',
          hidden: false,
        }
    },
    initData: {  // 搜索表单初始值
       type: Object,
       default: {}
    },
    isReset: {  // 是否显示重置按钮
        type: Boolean,
        default: false
    },
    loading:{
        type: Boolean,
        default: false
    },
    onFinish: {   // 父组件搜索事件
      type: Function,
      default: () => {},
    },
  })
  
  onMounted(() => {
    if(props.initData){
        formValues.value = props.initData
    }
  })
  
  const searchForm = (formEl) => {
    if (!formEl) return
    const fields = JSON.parse(JSON.stringify(formValues.value));
    props.itemList.forEach((item) => {
        if(item.type === 'date' && fields[item.name]){
            fields[item.name] = moment(fields[item.name]).format(item.format || 'YYYY-MM-DD')
        }else if(item.type === 'daterange' && fields[item.name]){
            fields[item.name] = [
                moment(fields[item.name][0]).format(item.format || 'YYYY-MM-DD'),
                moment(fields[item.name][1]).format(item.format || 'YYYY-MM-DD'),
            ]
        }
    })
    emit('handleSearch', fields)
  }
  const resetForm = (formEl) => {
    if (!formEl) return
    formEl.resetFields();
    formValues.value = {};
    emit('handleSearch', JSON.parse(JSON.stringify(formValues.value)))
  }
  
</script>

然后就是组件的调用了

<template>
    <div class="content-box">
        <SearchForm 
            :itemList="searchConfig"
            :initData="initData"
            :isReset="true"
            :loading="loading"
            @handleSearch="handleSearch"
        />
    </div>
</template>
<script setup>
import { ref, onMounted, h } from 'vue';
import SearchForm from '@/components/SearchForm/index.vue';

const loading = ref(false)
const initData = ref({})
const searchValues = ref({})
const searchConfig = ref([
    {
        label: '标题:',
        name: 'title',
        type: 'input',
    },

    {
        label: '创建人:',
        name: 'senderName',
        type: 'input',
    },
    {
        label: '栏目名称:',
        name: 'columnName',
        type: 'input',
    },
    {
        label: '类型:',
        name: 'conType',
        type: 'select',
        option: [
        {
            value: '2',
            label: '图片新闻',
        },
        {
            value: '1',
            label: '通知公告',
        },
        ],
    },
    {
        label: '创建时间:',
        name: 'createTime',
        placeholder: '创建时间',
        type: 'daterange',
        dateType: 'daterange'
    },
    {
        label: '实际发布时间:',
        name: 'sendTime',
        placeholder: '实际发布时间',
        type: 'daterange',
        dateType: 'daterange'
    },
])

// 查询
const handleSearch = (fieldsValue) => {
    searchValues.value = fieldsValue;
}


</script>

代码部分结束!希望对你有所帮助!喜欢的记得点个赞哦~

  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
基于引用和引用中的描述,我可以告诉你,Vue和Element-UI可以用于开发办公自动化系统,其中包括审批功能。 在基于Vue和Element-UI的开发中,你可以使用Element-UI中提供的el-form和el-table等组件来构建审批功能所需要的表单和表格。通过el-form,你可以创建审批表单,包括输入框、下拉框、日期选择等各种表单元素。而el-table可以用于展示审批的数据列表,并支持数据的排序、筛选和分页等功能。 另外,你还可以根据具体需求,结合Vue和Element-UI提供的其他组件和功能,来实现更多复杂的审批场景。例如,你可以使用Element-UI的el-dialog组件来创建审批流程的弹窗,通过el-button组件来触发审批操作。 总之,Vue和Element-UI提供了丰富的组件和功能,可以帮助你实现审批功能的开发。你可以根据具体需求,灵活运用这些组件和功能来完成你的审批系统设计。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [elsa:elsa(eleme simple admin)基于 element-ui 封装 el-form,el-table 等组件,用于快速开发后台管理...](https://download.csdn.net/download/weixin_42134240/18508160)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [java毕设:办公自动化系统-springboot+vue+element-ui 前后端分离](https://download.csdn.net/download/zy_dreamer/87798570)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [计算机程序设计语言课程设计(VUE.js)及实践项目的例子.txt](https://download.csdn.net/download/weixin_44609920/88236928)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值