index.vue 10.2 KB
<template>
  <div>
    <el-form class="clearfix search-form" id="searchForm" :class="[expande ? 'expande' : 'close']" :model="formModel" :size="size" @submit.native.prevent :label-position="labelPosition" :label-width="labelWidth">
      <!-- <div class="expande-button-wrap" @click="openClose"> 
      <i class="el-icon-arrow-down"></i>
      <span v-if="expande">展开</span>
      <span v-else>收起</span>
    </div> -->

      <div class="expande-button-wrap" v-if="needShowExpande">
        <div class="expande-button" @click="expandeClick" v-if="!expande">
          <i class="el-icon-arrow-down"></i>
          <span>展开</span>
        </div>
        <div class="expande-button" @click="expandeClick" v-else>
          <i class="el-icon-arrow-up"></i>
          <span>收起</span>
        </div>
      </div>
      <div class="firstRowBtn" v-if="!expande && isShowBtns && isNeedThreeBtns">
        <el-button plain @click="resetTable" size="small">重置</el-button>
        <el-button type="primary" @click="searchTable" size="small">查询</el-button>
        <el-button type="primary" size="small" @click="handleExport">导出</el-button>
        <!-- v-if="(merchantModel == 1 && accountLevel == 1) || (merchantModel == 2 && accountLevel == 4)" -->
        <!-- <el-button type="primary" @click="syncClick" :loading="ploading" size="small" >同步数据</el-button> -->
      </div>
      <div class="itemWidth">
        <el-form-item v-for="(item, index) in formList" :key="index" :label="item.label || ''">
          <template>
            <template v-if="item.type === 'input'">
              <el-input v-model="formModel[item.prop]" :type="item.inputType || 'text'" :placeholder="item.placeholder || '请输入'" :clearable="item.clearable || false" :readonly="item.readonly || false" @keyup.enter.native="onInput"></el-input>
            </template>
            <template v-else-if="item.type === 'select'">
              <el-select size="small" v-model="formModel[item.prop]" :placeholder="item.placeholder || '请选择'" :filterable="!item.noFilter">
                <el-option v-for="i in item.options" :key="i[item.value] || i.value" :label="i[item.key] || i.label" :value="i[item.value] || i.value" :disabled="i.disabled"></el-option>
              </el-select>
            </template>
            <template v-else-if="item.type === 'daterange'">
              <!-- :default-value="['2021-10-03', '2021-11-03']" -->
              <el-date-picker v-if="showDefaultTime" v-model="formModel[item.prop]" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" :picker-options="item.pickerOptions || {}"></el-date-picker>
              <el-date-picker v-else v-model="formModel[item.prop]" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" :picker-options="item.pickerOptions || {}"></el-date-picker>
            </template>
          </template>
        </el-form-item>
        <el-form-item style="position:absolute;right:-12px;bottom:0;" v-if="!needShowExpande || expande">
          <template>
            <slot name="form-slot"></slot>
          </template>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>
<script>
export default {
  name: 'search-form',
  props: {
    formModel: {
      type: Object,
      default: () => {}
    },
    size: {
      type: String,
      default: 'small'
    },
    formList: {
      type: Array,
      default: () => []
    },
    labelPosition: {
      type: String,
      default: 'right'
    },
    labelWidth: {
      type: String,
      default: 'auto'
    },
    ploading: {
      type: Boolean,
      default: false
    },
    isHasSyncBtn: {
      type: Boolean,
      default: false
    },
    isThreeRowShow: {
      type: Boolean,
      default: false
    },
    merchantModel: {
      type: Number,
      default: 1
    },
    accountLevel: {
      type: Number,
      default: 0
    },
    showDefaultTime: {
      type: Boolean,
      default: false
    },
    isNeedThreeBtns: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      expande: true,
      needShowExpande: false,
      FormHeight: 0,
      isShowBtns: true
    }
  },
  created() {},
  mounted() {
    if (this.showDefaultTime) {
      //调用默认时间(30天)
      this.getPassMonthFormatDate()
    }
    this.$nextTick(() => {
      var sf = document.getElementById('searchForm')

      if (sf.clientWidth < 1280 && this.isHasSyncBtn) {
        document.querySelector('.itemWidth').style.maxWidth = 700 + 'px'
      }
      if (sf.clientWidth < 952 && this.isHasSyncBtn) {
        this.isShowBtns = false
      }

      let lineHeight = 75 //75
      if (document.getElementById('searchForm').offsetHeight > lineHeight * 2) {
        this.expande = false
        this.needShowExpande = true
      } else {
        this.needShowExpande = false
        this.expande = true
      }

      if (sf.clientWidth < 840 && this.isThreeRowShow) {
        document.querySelector('.itemWidth').style.minHeight = document.querySelector('.itemWidth').offsetHeight + 50 + 'px'
        // 客户群里面四个表做特殊处理,当为苹果笔记本电脑尺寸时,不是现实展开收起按钮
        this.needShowExpande = false
        this.expande = true
      }
    })

    //有数据不算表格高度,没数据算高度
    // setTimeout(() => {
    //   this.$emit('setTableHeightEvent')
    // }, 200)
    // })
    //-------------------------
    // var that = this
    // var resizeTimer = null
    // 如何实现不管窗口如何改变,只在停止改变之后才执行代码呢?
    // $(window).bind('resize', function() {
    //   if (resizeTimer) clearTimeout(resizeTimer)
    //   resizeTimer = setTimeout(function() {
    //
    //   }, 100)
    // })
    // window.onresize = () => {
    //   return (() => {
    //     if (resizeTimer) clearTimeout(resizeTimer)
    //     resizeTimer = setTimeout(function() {
    //       window.location.reload() //浏览器重新从服务器请求资源,在http请求头中不会包含缓存标记
    //       // that.FormHeight = document.getElementById('searchForm').offsetHeight
    //       if (sf.clientWidth < 952 && that.isHasSyncBtn) {
    //         that.isShowBtns = false
    //       }
    //     }, 100)
    //   })()
    // }
    // window.addEventListener('resize', () => {
    //   if (resizeTimer) clearTimeout(resizeTimer)
    //   resizeTimer = setTimeout(function() {
    //     window.location.reload() //浏览器重新从服务器请求资源,在http请求头中不会包含缓存标记
    //     // that.FormHeight = document.getElementById('searchForm').offsetHeight
    //     if (sf.clientWidth < 952 && that.isHasSyncBtn) {
    //       that.isShowBtns = false
    //     }
    //   }, 100)
    // })()
  },

  watch: {
    FormHeight: {
      handler(val) {
        let lineHeight = 75 //22
        if (val > lineHeight * 2) {
          this.expande = false
          this.needShowExpande = true
        } else {
          this.needShowExpande = false
          this.expande = true
        }
      }
    }
  },

  methods: {
    onInput(e) {
      this.$emit('input', e)
    },
    resetTable() {
      this.$emit('resetTable')
    },
    searchTable() {
      this.$emit('searchTable')
    },
    syncClick() {
      this.$emit('syncClick')
    },
    handleExport() {
      this.$emit('handleExport')
    },

    expandeClick() {
      this.expande = !this.expande
      // setTimeout(() => {
      //   this.$emit('setTableHeightEvent')
      // }, 200)
    },
    //时间选择器显示默认一月的时间
    /* 获取过去时间,并传值给现在时间 */
    getPassMonthFormatDate() {
      var nowDate = new Date()
      var date = new Date(nowDate)
      date.setDate(date.getDate() - 30) //7 30 365
      var seperator1 = '-'
      var year = date.getFullYear()
      var month = date.getMonth() + 1
      var strDate = date.getDate()
      if (month >= 1 && month <= 9) {
        month = '0' + month
      }
      if (strDate >= 0 && strDate <= 9) {
        strDate = '0' + strDate
      }
      var formatDate = year + seperator1 + month + seperator1 + strDate
      this.getNowFormatDate(formatDate)
    },
    /* 获取现在时间,并接受过去时间的值 */
    getNowFormatDate(formatDate) {
      var date = new Date()
      var seperator1 = '-'
      var year = date.getFullYear()
      var month = date.getMonth() + 1
      var strDate = date.getDate()
      if (month >= 1 && month <= 9) {
        month = '0' + month
      }
      if (strDate >= 0 && strDate <= 9) {
        strDate = '0' + strDate
      }
      var nowData = year + seperator1 + month + seperator1 + strDate
      this.$set(this.formModel, 'daterange', [formatDate, nowData])
      // this.formModel['daterange'] = [formatDate, nowData] //  默认赋值一个月时间,不要直接赋值,不是响应式
    }
  }
}
</script>
<style lang="scss" scoped>
@import '@/styles/variables.scss';
.search-form {
  padding: 18px 0 0 0;
  position: relative;
  overflow: hidden;
  min-height: 50px;
  /deep/.el-form-item__label-wrap .el-form-item__label {
    // display: inline-block;
    // width: 110px;
  }

  .firstRowBtn {
    position: absolute;
    right: 52px;
    z-index: 1;
    .el-button + .el-button {
      margin-left: 10px;
    }
  }
  .itemWidth {
    max-width: 1027px;
    min-width: 660px;
  }
  .expande-button-wrap {
    position: absolute;
    top: 24px;
    right: 0;
    z-index: 99;
    .expande-button {
      color: $btnBlue;
      cursor: pointer;
      i {
        height: 8px;
      }
      span {
        height: 20px;
        font-size: 14px;
        font-weight: 400;
        color: $btnBlue;
        line-height: 20px;
      }
    }
  }
  .el-form-item {
    display: inline-block;
    // float: left;
    // width: calc(50% - 20px);
    // max-width: 350px; //420
    margin-right: 15px;
  }
  .el-range-editor--small.el-input__inner {
    width: 215px;
  }
  .el-input--small .el-input__inner {
    width: 215px;
  }
  .el-select {
    width: 215px;
  }
  .el-select > .el-input {
    width: 215px;
  }
  .el-form-item__content {
    width: 100%;
  }
}
.expande {
  // overflow: auto;
  height: auto;
  // padding-bottom: 22px;
}

.close {
  overflow: hidden;
  height: 50px;
  // padding-bottom: 0;
}
</style>