el-select虚拟滚动

首先下载虚拟滚动的依赖

npm install vue-virtual-scroll-list --save

主页面 index.vue

<template>
  <el-select v-model="value1"
             filterable
             :filter-method="remoteMethod"
             multiple
             clearable
             v-bind="$attrs"
             v-on="$listeners"
             @change="handleChange"
             placeholder="请选择">
    <!-- <el-option v-for="(item,index) in tableList"
                       :key="index"
                       :label="item.tableCnName"
                       :value="item.targetTableId">
            </el-option> -->

    <vue-virtual-list ref="virtualList"
                      style="overflow-y: auto;max-height:230px;"
                      :data-key="'targetTableId'"
                      :data-sources="sourcesList"
                      :data-component="itemComponent"
                      :keeps="20"
                      :extra-props="extraProps"></vue-virtual-list>
  </el-select>
</template>

<script>
import VueVirtualList from 'vue-virtual-scroll-list'
import itemComponent from './itemComponent'
export default {
  name: 'virtualSelect',
  props: {
    value: {
      type: [String, Number, Array],
      default: ''
    },
    extraProps: {
      type: Object,
      default: () => {
        return {
          label: 'tableCnName',
          value: 'targetTableId'
        }
      }
    },
    list: {
      type: Array
    }
  },
  components: {
    itemComponent,
    VueVirtualList
  },
  data () {
    return {
      itemComponent,
      value1: '',
      sourcesList: []

    }
  },
  watch: {
    value: {
      handler (newVal) {
        this.value1 = newVal
      }
    },
    list: {
      handler (newVal) {
        this.sourcesList = newVal
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    remoteMethod (query) {
      if (query !== '') {
        this.sourcesList = this.list.filter((item) => {
          return (
            item[this.extraProps.label]
              .toLowerCase()
              .indexOf(query.toLowerCase()) > -1
          )
        })
      } else {
        this.sourcesList = this.list
      }
      console.log(this.sourcesList)
    },
    handleChange () {
      this.$emit('input', this.value1)
    }
  }
}
</script>

<style>
</style>

item页面

<template>
    <div>
      <el-option
          :key="label + value"
          :label="source[label]"
          :value="source[value]"
          class="elOption"
      >
        <span>{{ source[label] }}</span>
        <span v-if="rightLabel" style="float:right;color:#939393">{{
            source[rightLabel]
          }}</span>
      </el-option>
    </div>
  </template>

<script>
export default {
  name: 'item-component',
  props: {
    // index of current item
    // 每一行的索引
    index: {
      type: Number
    },
    // 每一行的内容
    source: {
      type: Object,
      default () {
        return {}
      }
    },
    // 需要显示的名称
    label: {
      type: String
    },
    // 绑定的值
    value: {
      type: String
    },
    // 右侧显示绑定的值,为空则不显示
    rightLabel: {
      type: String,
      default: ''
    }
  },
  mounted () {}
}
</script>
<style scoped>
.elOption {
    width: 600px;
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值