index.vue 8.48 KB
<!--  -->
<template>
  <div class="searchTop" :class="[searchData.length > 4 ?'':'padding-left',searchTopClass]">
    <div v-show="searchData.length > 4 || showType =='oneRow' ? true : false" :class="searchTopBtnStyle" class="searchTopBtn">
        <template
          v-for="(item, index) in searchBtn"
          >
          <el-badge 
            v-if="!item.nextToForm&&item.Badge"
            :key="index" 
            :value="$store.getters[item.BadgeStoreString]" >
            <el-button
              size="small"
              :class="item.class"
              :style="item.style"
              :type="item.type"
              :disabled="item.isDisabled"
              :loading="item.isDisabled"
              @click="btnClick(item.pType, item)">{{item.name}}
            </el-button>  
          </el-badge>
          <el-button
            v-else-if="!item.nextToForm"
            :key="index" 
            size="small"
            :class="item.class"
            :style="item.style"
            :type="item.type"
            :disabled="item.isDisabled"
            :loading="item.isDisabled"
            @click="btnClick(item.pType, item)">{{item.name}}
          </el-button>  
        </template>
    </div>
    <el-form
      ref="form"
      :model="searchForm"
      :class="searchData.length > 4 || showType =='oneRow' ? 'threeCol clearfix' : 'clearfix'">
      <el-form-item v-for="(item,index) in searchData" 
        style="height:36px" 
        :label-width="item.width?item.width:'85px'" 
        :key="index" 
        :prop='item.modelValue'
        :rules="item.rules"
         size="small"
        :label="item.label">
        <el-input 
          v-if="item.type === 'selectInput'" 
          placeholder="请输入对应条件值" 
          v-model="searchForm[item.modelValue]" 
          size="small" 
          class="input-with-select" 
          clearable>
          <el-select v-model="searchForm[item.optionKey]" slot="prepend" :style="item.style" placeholder="请选择" size="small">
            <el-option
              v-for="(i,index) in item.optionValue"
              :key="index"
              :label="i.label"
              :value="i.value">
            </el-option>  
          </el-select>
        </el-input>
        <el-input 
          v-if="item.type === 'input'" 
          v-model.trim="searchForm[item.modelValue]" 
          :placeholder="item.placeholder"
          :class="item.clickFunc?'input cursor':'input'"
          @click.native="item.clickFunc ? handleInputClick(item.modelValue, item) :''"
          :readonly="item.readonly"
          :disabled="item.disabled"
          clearable>
        </el-input>
        <el-date-picker 
          v-if="item.type === 'time'" 
          v-model="searchForm[item.modelValue]" 
          type="daterange"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="item.pickerOptions ? item.pickerOptions :pickerOption">
        </el-date-picker>
        <el-select 
          v-if="item.type === 'select'" 
          v-model="searchForm[item.modelValue]"
          :placeholder="item.placeholder">
          <el-option
            v-for="(i,index) in item.optionValue"
            :key="index"
            :label="i.label"
            :value="i.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-for="(item, index) in searchBtn" :key="item.name" class="next_form_item">
          <el-badge 
            v-if="item.nextToForm&&item.Badge"
            :key="index" 
            :value="$store.getters[item.BadgeStoreString]" >
            <el-button
              size="small"
              :class="item.class"
              :style="item.style"
              :type="item.type"
              :disabled="item.isDisabled"
              :loading="item.isDisabled"
              @click="btnClick(item.pType, item)">{{item.name}}
            </el-button>  
          </el-badge>
          <el-button
            v-else-if="item.nextToForm"
            :key="index" 
            size="small"
            :class="item.class"
            :style="item.style"
            :type="item.type"
            :disabled="item.isDisabled"
            :loading="item.isDisabled"
            @click="btnClick(item.pType, item)">{{item.name}}
          </el-button>  
      </el-form-item>
    </el-form>
    <div v-show="searchData.length > 4 || showType =='oneRow' ? false : true" class="searchBtn">
      <el-button  size="small" type="primary" @click="handleSearch">查询</el-button>
      <el-button  size="small" @click="handleReset">重置</el-button>
    </div>
  </div>
</template>

<script>
// import './date.js'
const today = new Date()
const limitday = new Date('2017-12-31')
// const startDate = new Date(today.getTime() - 3600 * 1000 * 24 * 7).format('yyyy-MM-dd')
// const endDate = new Date(today.getTime()).format('yyyy-MM-dd')
export default {
  name: 'SearchTop',
  props: {
    searchForm: Object,
    searchData: Array,
    searchBtn: Array,
    searchTopBtnStyle: String,
    searchTopClass: String,
    showType:{
      type:String,
      default:''
    },
    noClearAttr:{
      type:Boolean,
      default:false
    },
    
  },
  data () {
    return {
      pickerOption: {
        disabledDate(time) {
          return time > today || time < limitday
        }
      },
    }
  },
  watch:{
    searchData:{
      handler(newVal,oldVal){
        this.searchData = newVal;
      },
      deep:true,
    }
  },
  created() {
    for(var item in this.searchData) {
      let temp = this.searchData[item]
      switch(temp.type) {
        case 'input':
          this.searchData[item].placeholder = temp.placeholder ? temp.placeholder :'请输入' + temp.label.split(':')[0]
          break
        case 'select':
          this.searchData[item].placeholder = temp.placeholder ? temp.placeholder :'请选择' + temp.label.split(':')[0]
          break
      }
    }
  },
  methods: {
    deleteEmptyAttribute(val) {
      for (var i in val) {
        !val[i] && (delete val[i])
      }
      return val
    },
    handleSearch() {
      // for(var item in this.searchData) {
      //   if(this.searchData[item].type == 'time') {
      //     !this.searchForm.time ? this.searchForm.time = [startDate, endDate] : ''
      //   }
      // }
      this.$refs['form'].validate((valid) => {
        if (valid) {
          !this.noClearAttr&&this.deleteEmptyAttribute(this.searchForm)
          this.$emit('search', this.searchForm)
        }
      })
    },
    handleReset() {
      for(var item in this.searchForm) {
          this.searchForm[item] = "";
      }
      this.$emit('reset', this.searchForm)
    },
    btnClick(pType, item) {
      switch(pType) {
        case 'search':
          this.handleSearch()
          break
        case 'reset':
          this.handleReset()
          break
        default:
          item.callback()
          break
      }
    },
    handleInputClick(modelValue, item){
      this.$emit('inputClick', modelValue,item)
    },
  }
}

</script>
<style rel="stylesheet/scss" lang="scss" >
.clearfix:after {
  display:block; 
  clear:both; 
  content:""; 
  visibility:hidden; 
  height:0;
}
.clearfix {
  zoom:1;
}
.searchTop {
  padding: 20px 10px 0;
  /* border: 2px solid #f0f2f5;
  border-radius: 5px; */
  margin-bottom: 30px;  
  border-width: 1px;
  border-style: solid;
  border-color: rgb(235, 235, 235);
  border-image: initial;
  border-radius: 3px;
  transition: all 0.2s ease 0s;
  &:hover {
      box-shadow: rgba(232, 237, 250, 0.6) 0px 0px 8px 0px,
        rgba(232, 237, 250, 0.5) 0px 2px 4px 0px;
    }
  .el-form-item {
    float: left;
    width: 50%;
    max-width: 420px;
  }
  .input,.el-date-editor--daterange.el-input__inner,
  .el-select { 
    width: 85%;
  }
  .threeCol .el-form-item{
    float: left;
    width: 33%;
    // max-width: unset;
  }
  .threeCol .input,
  .threeCol .el-date-editor--daterange.el-input__inner,
  .threeCol .el-select { 
    width: 95%;
  }
  .searchTopBtn {
    display: flex;
    justify-content: flex-end;
    margin: 0 2.2% 15px 0;
  }
  .searchBtn{
    margin-left: 3px;
    margin-bottom: 20px;
  }
}

.padding-left{
  padding-left: 35px;
  margin-top: 5px;
}

.cursor .el-input--medium .el-input__inner{
  cursor: pointer;
}

.noBorder{
  border:none !important;
  &:hover {
    box-shadow:none !important;
  }
}
</style>
<style rel="stylesheet/scss" lang="scss" scoped>
  .searchTop .el-form-item.next_form_item{
    width: auto;
    margin-right: 10px;
    .el-button{
      vertical-align: top;
    }
  }
</style>