index.vue 7.39 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">
        <el-button plain @click="resetTable" size="small">重置</el-button>
        <el-button type="primary" @click="searchTable" size="small">查询</el-button>
        <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 || '请输入'" :readonly="item.readonly || false" @keyup.enter.native="onInput"></el-input>
            </template>
            <template v-else-if="item.type === 'select'">
              <el-select v-model="formModel[item.prop]" :placeholder="item.placeholder || '请选择'">
                <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'">
              <el-date-picker 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>
          <template>
            <slot name="form-slot"></slot>
          </template>
        </el-form-item>
        <!-- <el-form-item>
          <template>
            <slot name="formsearch-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: 'left'
    },
    labelWidth: {
      type: String,
      default: 'auto'
    },
    ploading: {
      type: Boolean,
      default: false
    },
    isHasSyncBtn: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      expande: true,
      needShowExpande: false,
      FormHeight: 0,
      isShowBtns: true
    }
  },
  mounted() {
    var sf = document.getElementById('searchForm')
    this.$nextTick(() => {
      if (sf.clientWidth < 1280 && this.isHasSyncBtn) {
        console.log(sf.clientWidth, 'sf')
        document.querySelector('.itemWidth').style.maxWidth = 700 + 'px'
      }
      if (sf.clientWidth < 952 && this.isHasSyncBtn) {
        this.isShowBtns = false
      }
      let lineHeight = 75
      if (document.getElementById('searchForm').offsetHeight > lineHeight * 2) {
        // console.log('第一次searchForm高度', document.getElementById('searchForm').offsetHeight)
        this.expande = false
        this.needShowExpande = true
      } else {
        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() {
    //     console.log('窗口发生改变了哟!')
    //   }, 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')
    },
    // openClose(){
    //  console.log( document.getElementById('searchForm').offsetHeight);
    //  document.getElementById('searchForm').offsetHeight=172;
    // }
    expandeClick() {
      this.expande = !this.expande
      setTimeout(() => {
        this.$emit('setTableHeightEvent')
      }, 200)
    }
  }
}
</script>
<style lang="scss">
.search-form {
  padding: 18px 25px 0 0;
  position: relative;
  overflow: hidden;
  // height: 50px;
  .firstRowBtn {
    position: absolute;
    right: 60px;
    .el-button + .el-button {
      margin-left: 20px;
    }
  }
  .itemWidth {
    max-width: 1050px;
    min-width: 660px;
  }
  .expande-button-wrap {
    position: absolute;
    right: 0;
    z-index: 99;
    .expande-button {
      color: #5850ff;
      cursor: pointer;
      i {
        height: 8px;
      }
      span {
        height: 20px;
        font-size: 14px;
        font-weight: 400;
        color: #5850ff;
        line-height: 20px;
      }
    }
  }
  .el-form-item {
    display: inline-block;
    // float: left;
    // width: calc(50% - 20px);
    // max-width: 300px;//420
    margin-right: 15px;
  }
  .el-range-editor--small.el-input__inner {
    width: 215px;
  }
  .el-input--small .el-input__inner {
    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>