element 虚拟select

由于select组件如果里面的选择项过多,会造成卡顿,所以分装一个组件,支持大数据滚动,不卡顿,废话不多说 直接上代码,直接一个vue组件支持v-model绑定,用法

 <VisualSelect   v-model="值"  :list="选择项"
  > </VisualSelect>

添加组件 VisualSelect.vue如下
   
<template>
  <div :class="classId">
    <el-select
      popper-class="VisualSelects"
      @visible-change="popChange"
      v-model="selectValue"
      placeholder="请选择"
      :filter-method="filterMethod"
      filterable
      clearable
      :disabled="disabled"
    >
      <el-option
        v-for="item in options"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      ></el-option>
    </el-select>
  </div>
</template>
<script>

export default {
  model: {
    prop: 'value', //绑定的值,通过父组件传递
    event: 'update', //自定义名
  },
  props: {
    defaultFirst: {
      type: Boolean,
    },
    list: {
      type: Array,
      default: () => {
        return [];
      },
    },
    value: {
      type: [String, Number],
      default: '',
    },
    disabled: Boolean,
  },
  data() {
    return {
      newList: [],
      classId: 'unlimited',
      selectValue: '',
      options: [],
      domList: null,
      startIndex: 0,
      endIndex: 0,
      maxLength: 8, //弹出框最大支持8个条目
      itemHeight: 34, // select组件选项高度
      maxHeightDom: 0, //滚动条高度
    };
  },
  watch: {
    selectValue(val) {
      this.$emit('update', val);
      if (!val) {
        this.resetList();
        this.maxHeightDom.style.height = this.newList.length * 34 + 'px';
        this.domList.style.paddingTop = 0 + 'px';
      }
    },
    list() {
      this.resetList();
      this.init();
    },
  },
  mounted() {
    this.resetList();
    this.init();
  },
  methods: {
    addScrollDiv(selectDom) {
      this.maxHeightDom = document.createElement('div');
      this.maxHeightDom.style.width = 0;
      selectDom.insertBefore(this.maxHeightDom, this.domList);
    },
    reCacularHeight() {
      this.maxHeightDom.style.height = (this.newList.length + 1) * 34 + 'px';
    },
    resetList(arrys) {
      if (Array.isArray(arrys)) {
        this.newList = arrys.slice();
      } else {
        this.newList = this.list.slice(); //筛选的数据
      }
      this.options = this.newList.slice(0, 8); //显示的数据
    },
    init() {
      if (this.defaultFirst && this.list.length > 0) {
        //默认第一个
        this.selectValue = this.list[0].value;
      }

      const selectDom = document.querySelector(
        `.${this.classId} .el-select-dropdown .el-select-dropdown__wrap`
      );
      const slectBoxDom = document.querySelector(
        `.${this.classId} .el-select-dropdown__wrap`
      );
      slectBoxDom.style.display = 'flex';
      slectBoxDom.style.flexDirection = 'row';
      this.domList = selectDom.querySelector(
        `.${this.classId} .el-select-dropdown__wrap .el-select-dropdown__list`
      );
      this.addScrollDiv(slectBoxDom); //添加一个滚动的div
      slectBoxDom.addEventListener('scroll', () => {
        this.reCacularHeight();
        const scrollTop = selectDom.scrollTop;
        const startIndex = parseInt(scrollTop / 34);
        const endIndex = startIndex + 7;
        this.domList.style.paddingTop = scrollTop + 'px';
        this.options = this.newList.slice(startIndex, endIndex);
      });
    },
    filterMethod(val) {
      if (val) {
        const arrys = this.list.filter((elem) => {
          return new RegExp(val).test(elem.label);
        }); //多这部是性能上优化 请勿合并
        this.resetList(arrys);
      } else {
        this.resetList();
      }
      this.reCacularHeight();
    },
    popChange() {
      this.domList.style.paddingTop = 0 + 'px';
      if (!this.selectValue) {
        this.resetList();
      }
      this.reCacularHeight();
    },
  },
};
</script>
<style lang="scss">
.VisualSelects {
  .el-select-dropdown__list {
    width: 100%;
  }
  .el-select-dropdown__wrap {
    height: 255px;
  }
}
</style>



### Element Plus Select 组件使用教程 #### 1. 安装与引入 为了使用 `Select` 组件,需确认项目已安装 Element Plus。通过 npm 或 yarn 进行安装: ```bash npm install element-plus --save ``` 或 ```bash yarn add element-plus ``` 接着,在 Vue 组件中按需引入 `ElSelect` 和 `ElOption` 组件[^1]。 ```javascript import { ElSelect, ElOption } from 'element-plus' export default { components: { ElSelect, ElOption } } ``` #### 2. 基本用法 创建一个简单的下拉菜单,允许用户从中选择选项。下面是一个基本的例子展示如何设置默认值以及监听变化事件[^2]。 ```html <template> <div> <el-select v-model="selectedValue" placeholder="请选择"> <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> </el-option> </el-select> </div> </template> <script> export default { data() { return { selectedValue: '', options: [{ value: 'option1', label: '黄金糕' }, { value: 'option2', label: '双皮奶' }] }; }, }; </script> ``` #### 3. 动态加载远程数据 当面对大量数据时,可以采用分页方式来优化性能。这里介绍一种基于虚拟滚动技术的方法,适用于处理海量数据场景下的高效渲染[^3]。 ```html <!-- TSelect.vue --> <template> <!-- ...其他代码省略... --> <el-select-v2 virtual-scrolling :options="remoteOptions" @search="handleSearch"/> </template> <script setup> // ...导入依赖... const remoteOptions = ref([]); async function handleSearch(query) { const result = await fetchRemoteData(query); remoteOptions.value = result; } function fetchRemoteData(/* 参数 */) {/* 实现异步请求 */} </script> ``` #### 4. 表单集成 在实际应用中,通常会将 `Select` 控件与其他表单项组合起来构建复杂的业务逻辑。比如在一个动态表格内嵌入可编辑的选择器,使得每一行都能独立操作而不影响整体布局稳定性[^4]。 ```html <template> <el-table :data="tableData"> <el-table-column prop="name"></el-table-column> <el-table-column> <template #default="scope"> <el-select v-model="scope.row.selectVal"> <el-option v-for="(opt, idx) in scope.row.options" :key="idx" :label="opt.text" :value="opt.id"/> </el-select> </template> </el-table-column> </el-table> </template> ```
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值