表单组件二次封装(vue-elementui、搜索条件)

搜索条件组件

(SearchForm\index.vue放在目录的components下):


<template>
  <div class="ces-search">
    <el-form class="input" :size="size" inline :label-width="labelWidth">
      <el-form-item v-for="item in searchForm" :key="item.prop" :label="item.label">
        
        <el-input v-if="item.type==='Input'" v-model="searchData[item.prop]" size="mini"  clearable @clear="clearParams('searchData[item.prop]')"/>
        
        <el-select v-if="item.type==='Select'" v-model="searchData[item.prop]" size="mini" filterable clearable @change="item.change(searchData[item.prop])">
          <el-option v-for="(op, index) in item.options" :key="index" :label="op.label" :value="op.value" />
        el-select>
        
        <el-radio-group v-if="item.type==='Radio'" v-model="searchData[item.prop]">
          <el-radio v-for="ra in item.radios" :key="ra.value" :label="ra.value">{{ ra.label }}el-radio>
        el-radio-group>
        
        <el-radio-group v-if="item.type==='RadioButton'" v-model="searchData[item.prop]" @change="item.change && item.change(searchData[item.prop])">
          <el-radio-button v-for= "ra in item.radios" :key="ra.value" :label="ra.value">{{ ra.label }}el-radio-button>
        el-radio-group>
        
        <el-checkbox-group v-if="item.type==='Checkbox'" v-model="searchData[item.prop]">
          <el-checkbox v-for="ch in item.checkboxs" :key="ch.value" :label="ch.value">{{ ch.label }}el-checkbox>
        el-checkbox-group>
        
        <el-date-picker
          v-if="item.type==='year'"
          v-model="searchData[item.prop]"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="year"
          placeholder="选择年"
          @change="item.change(searchData[item.prop])">
        el-date-picker>
        
        <el-date-picker v-if="item.type==='Date'" v-model="searchData[item.prop]" />
        
        <el-time-select v-if="item.type==='Time'" v-model="searchData[item.prop]" type="" />
        
        <el-date-picker v-if="item.type==='DateTime'" v-model="searchData[item.prop]" type="datetime" :disabled="item.disable && item.disable(searchData[item.prop])" />
        
        <el-date-picker
          v-if="item.type==='daterange'"
          v-model="searchData[item.prop]"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
        
        <el-date-picker
          v-if="item.type==='monthrange'"
          v-model="searchData[item.prop]"
          type="monthrange"
          range-separator="-"
          start-placeholder="开始月份"
          end-placeholder="结束月份">
        el-date-picker>
        
        <el-date-picker
          v-if="item.type==='datetimerange'"
          v-model="searchData[item.prop]"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator=""
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="item.change(searchData[item.prop])"
        >
        el-date-picker>
        
        
        
        <el-switch v-if="item.type==='Switch'" v-model="searchData[item.prop]" />
      el-form-item>
    el-form>
    <el-form v-if="isHandle" inline class="button">
      <el-form-item v-for="item in searchHandle" :key="item.label">
        <el-button :type="item.type" :size="item.size || size" @click="item.handle()">{{ item.label }}el-button>
      el-form-item>
    el-form>
  div>
template>

<script>
export default {
  props: {
    isHandle: {
      type: Boolean,
      default: true
    },
    labelWidth: {
      type: String,
      default: '100px'
    },
    size: {
      type: String,
      default: 'mini'
    },
    searchForm: {
      type: Array,
      // eslint-disable-next-line vue/require-valid-default-prop
      default: []
    },
    searchHandle: {
      type: Array,
      default: () => []
    },
    searchData: {
      type: Object,
      // eslint-disable-next-line vue/require-valid-default-prop
      default: {}
    }
  },
  data() {
    return {
    }
  },
  methods: {
    // 置空参数
    clearParams(val) {
      // eslint-disable-next-line no-return-assign
      // Object.keys(this.searchData).forEach(key => this.searchData[key] = '')
    }
  }
}

script>
<style scoped>
  .input{
    float: left;
  }
  .button{
    float: left;
  }
  .el-form-item__content {
    line-height: 28px;
  }
style>

父组件中引入

html部分:

 <search-form class="searchMain" size="mini" label-width="80px" :search-data="searchData" :search-form="searchForm" :search-handle="searchHandle" />
          

js部分:

import SearchForm from '@/components/SearchForm'
export default {
  components: {  SearchForm },
  data() {
    return {
	    searchData: { // 查询参数
	        pageNum: 1, pageSize: 5, userId: undefined, buildingId: undefined, communityId: undefined,
	        merchantId: undefined, roomNo: undefined, unitId: undefined,
	        data: { mobilePhone: undefined, residentName: undefined }
	    },
	    searchForm: [
	        { type: 'Input', label: '室', prop: 'roomNo', width: '100px', placeholder: '请输入室...' },
	        { type: 'Input', label: '姓名', prop: 'residentName', width: '100px', placeholder: '请输入姓名...' },
	        { type: 'Input', label: '手机号', prop: 'mobilePhone', width: '100px', placeholder: '请输入手机号码...' }
	    ],
	    searchHandle: [
	        { label: '查询', type: 'primary', handle: this.getList },
	        { label: '重置', type: 'primary', handle: this.resetForm }
	    ],
    }
  },
  methods:{
  // 获取小区列表、表格信息
    getList() {
      this.loadingTable = true
      this.searchData.userId = this.$store.getters.id
      this.searchData.data.mobilePhone = this.searchData.mobilePhone
      this.searchData.data.residentName = this.searchData.residentName

      listResident(this.searchData).then((response) => {
        const row = response.data.rows || []
        this.list = row.map(function(val) {
          val.identity = val.residentIdentity
          if (val.residentIdentity !== undefined) {
            val.residentIdentity = val.residentIdentity + 0 === 1 ? '住户' : '租户'
          }
          return val
        })
        this.total = response.data.total
        this.loadingTable = false
        this.loading = false
      })
    },
    // 表格重置
    resetForm() {
      Object.assign(this.$data.searchData, this.$options.data().searchData)
    }
  }
}

css部分

.searchMain {
    height: 100px;
 }

(提示:searchData里面不需要的自行删掉,方法和api也是自己写合适的)

详解:

1、子组件的
表单组件二次封装(vue-elementui、搜索条件)_第1张图片
与父组件的在这里插入图片描述
对应
2、子组件里的表单组件二次封装(vue-elementui、搜索条件)_第2张图片
与父组件里的
表单组件二次封装(vue-elementui、搜索条件)_第3张图片
对应

你可能感兴趣的:(vue-elementui,vue,elementui)