vue+element 封装下拉分页

该文章展示了一个Vue组件的实现,主要用于创建一个具有远程搜索、可清除、可过滤的下拉选择框。组件支持动态分页,当输入关键词时,会触发远程方法进行数据过滤,并在选项中显示。同时,组件提供了当前选中值的变化、清除选择、以及页面切换等事件处理功能。
摘要由CSDN通过智能技术生成
<template>
  <el-select
    v-model="value"
    filterable
    clearable
    remote
    reserve-keyword
    placeholder="请输入关键词"
    :remote-method="remoteMethod"
    :disabled="disable"
    @visible-change="clickSelect"
    @change="updateSelect"
    @clear="cleanSelect"
  >
    <el-option
      v-for="item in selectOptions"
      :key="item.key"
      :label="item.value"
      :value="item.key"
      placeholder="请输入"
    >
      <span style="float: left">{{ item.value }}</span>
      <span style="float: right; color: #8492a6; font-size: 13px">{{ item.key }}</span>
    </el-option>
    <div style="bottom: -10px">
      <el-pagination
        small
        :current-page="this.pageInfo.current"
        :page-size="this.pageInfo.size"
        layout="prev, pager,next,total"
        :total="this.pageInfo.total"
        @current-change="handleCurrentChange"
      />
    </div>
  </el-select>
</template>

<script>
export default {
  name: 'SelectPagin',
  model: {
    prop: 'inputValue',
    event: 'updateInputValue'
  },
  props: {
    inputValue: { type: String, default: '' },
    disable: { type: Boolean, default: false },
    changePrev: { type: Function, default: (data) => {} },
    inputChange: { type: Function, default: () => {} },
    selectItem: { type: Function, default: () => {} },
    cleanSelect: { type: Function, default: () => {} },
    dataOptions: {
      type: Array,
      default: () => []
    }, pageInfoProps: {
      type: Object,
      default: () => {
        return { current: 1,
          size: 7,
          total: 0
        }
      }
    }
  },
  data() {
    return { value: '', selectOptions: [], tempOptions: [], pageInfo: {}, query: '' }
  }, watch: {
    dataOptions() {
      if (JSON.stringify(this.dataOptions) !== JSON.stringify(this.tempOptions)) {
        this.selectOptions = this.dataOptions
        this.pageInfo.current = 1
        this.initPage(null)
      }
    }, inputValue() {
      if (this.inputValue !== this.value) { this.value = this.inputValue }
    }
  },
  mounted() {
    this.value = this.inputValue
    this.selectOptions = this.dataOptions
    this.tempOptions = JSON.parse(JSON.stringify(this.dataOptions))
    this.pageInfo = { ...this.pageInfoProps }
    this.initPage(null)
  },
  beforeDestroy() {
  },
  methods: {
    clickSelect(val) {
      if (val === true) {
        this.value = this.inputValue
        this.selectOptions = []
        this.changePrev()
      }
    },
    // 过滤本地数据
    remoteMethod(val) {
      this.pageInfo.current = 1
      this.query = val
      const datas = this.initPage(val)
      this.inputChange({ haveSelect: datas.length > 0, val: val })
    },
    updateSelect(val) {
      if (val !== '') {
        this.$emit('updateInputValue', val)
        this.selectItem(this.selectOptions.find(item => item.key === val))
        this.query = ''
        this.pageInfo = { ...this.pageInfoProps }
        this.initPage(null)
      }
    },
    initPage(val) {
      let temp = []
      if (val === null || val === '') {
        temp = this.dataOptions
      } else {
        const dataTemp = JSON.parse(JSON.stringify(this.dataOptions))
        const filterList = dataTemp.filter(item => item.value?.includes(val))
        temp = filterList
        debugger
      }
      this.selectOptions = []
      let startIndex = (this.pageInfo.current - 1) * this.pageInfo.size; const endIndex = startIndex + this.pageInfo.size > temp.length ? temp.length : startIndex + this.pageInfo.size
      this.pageInfo.total = temp.length
      while (startIndex < endIndex) {
        this.selectOptions.push(temp[startIndex])
        startIndex++
      }
      return temp
      // 没有内容     接口调用
    }, handleCurrentChange(val) {
      this.pageInfo.current = val
      this.initPage(this.query)
    }
  }
}
</script>

<style scoped>
.select-cover {
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: red;
  z-index: 9;
  opacity: 0;
  pointer-events: none;
}

.back-to-ceiling .Icon {
  fill: #9aaabf;
  background: none;
}
</style>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值