index.vue 3.21 KB
<template>
  <el-select ref="selectRef" clearable filterable remote :remote-method="remoteMethod" v-el-select-loadmore="loadmore" v-model="inputVal" placeholder="请选择门店" @change="selectChange">
    <el-option v-for="(item, i) in dataItems" :key="i" :label="item[label]" :value="item[value]"></el-option>
  </el-select>
</template>

<script>
import { getStoreList } from './api'
export default {
  name: 'SearchDropDown',
  props: ['inputBoxSearchValue', 'label', 'value'],
  data() {
    return {
      searchContentStorage: '',
      inputVal: '',
      dataItems: [],
      arrBack:[],
      totalNum:'',
      pageData: {
        //懒加载相关参数,这里代表从第一条数据开始加载,一次加载40项
        pageNum: 1,
        pageSize: 10
      }
    }
  },
  directives: {
    /** 下拉框懒加载 */
    'el-select-loadmore': {
      bind(el, binding) {
        const SELECTWRAP_DOM = el.querySelector('.el-select-dropdown .el-select-dropdown__wrap')
        SELECTWRAP_DOM.addEventListener('scroll', function() {
          const condition = this.scrollHeight - this.scrollTop <= this.clientHeight
          if (condition) {
            binding.value()
          }
        })
      }
    }
  },
  mounted() {
    this.loadUpData(this.pageData)
  },
  watch: {
    // 'inputVal'(newVal, oldVal) {
    //   console.log(newVal,oldVal)
    //   // if (oldVal !== newVal) {
    //   //   this.dataItems = []
    //   //   this.loadUpData({
    //   //     pageNum: 1,
    //   //     pageSize: 10
    //   //   },newVal)
    //   // }
    // }
    'inputVal': {
      handler(val) {
        console.log(val)
        if (!val) {
          this.dataItems = []
          this.loadUpData({
            pageNum: 1,
            pageSize: 10
          })
        }
      }
    }
  },
  methods: {
    async loadUpData(pageData, searchVal) {
      let params = {
        ...pageData,
        merchantName: searchVal
      }
      await this.$apis.wechatGroup.getStoreList(params).then(res => {
        this.totalNum = res.count
        this.arrBack = res.data
      })
      this.dataItems = [...this.dataItems,...this.arrBack]
    },
    loadmore() {
      this.pageData.pageNum++
      let acquireCount = this.pageData.pageNum * this.pageData.pageSize
     if (acquireCount - this.totalNum >= this.pageData.pageSize){
       return
     }
      if (this.searchContentStorage) {
        this.loadUpData(this.pageData, this.searchContentStorage)
      } else {
        this.loadUpData(this.pageData, '') //类似于分页查询
      }
    },
    // 远程搜索
    async remoteMethod(searchValue) {
      console.log(this.inputVal)
      if (searchValue !== '') {
        this.searchContentStorage = searchValue
        this.dataItems = []
        this.timer = setTimeout(() => {
          this.loadUpData(this.pageData, searchValue)
        }, 300)
      } else {
        this.dataItems = []
        this.loadUpData({
          pageNum: 1,
          pageSize: 10
        },'')
      }
    },
    // 根据所选商户获取品牌和地址
    async selectChange(label) {
      if (label) {
        let obj = this.dataItems.filter(el => {
          return el['incomingDisplayLabel'] == label
        })
      }
    }
  }
}
</script>

<style scoped></style>