首页 > 其他分享 >vue2的el-select虚拟下拉

vue2的el-select虚拟下拉

时间:2024-08-27 11:51:20浏览次数:8  
标签:el return val default value item vue2 type select

说明

vue3的的el-select,Element Plus封装了虚拟下拉的写法,但是有些vue2版本的el-select却没办法用虚拟下拉。如果下拉列表数据量大的话,很导致极度卡顿。那我们就自己封装一个吧

代码

components下新建elselectV2文件夹,新建两个文件:elOptionNode.vueVirtualSelect.vue
elOptionNode.vue代码如下:

<template>
  <el-option
    :key="label+value"
    :label="concatString(source[label], source[value])"
    :value="source[value]"
    :disabled="source.disabled"
    :title="concatString(source[label], source[value])"
  >
    <span>{{ concatString(source[label], source[value]) }}</span>
    <span
      v-if="isRight"
      style="float:right;color:#939393"
    >{{ source[value] }}</span>
  </el-option>
</template>
<script>
export default {
  name: 'ElOptionNode',
  props: {
    // 每一行的索引
    index: {
      type: Number,
      default: 0
    },
    // 每一行的内容
    source: {
      type: Object,
      default() {
        return {}
      }
    },
    // 需要显示的名称
    label: {
      type: String,
      default: ''
    },
    // 绑定的值
    value: {
      type: String,
      default: ''
    },
    // 是否拼接label | value
    isConcat: {
      type: Boolean,
      default: false
    },
    // 拼接label、value符号
    concatSymbol: {
      type: String,
      default: ' | '
    },
    // 右侧是否显示绑定的值
    isRight: {
      type: Boolean,
      default() {
        return false
      }
    }
  },
  methods: {
    concatString(a, b) {
      a = a || ''
      b = b || ''
      if (this.isConcat) {
        return a + ((a && b) ? this.concatSymbol : '') + b
      }
      return a
    }
  }
}
</script>

VirtualSelect.vue代码如下

<template>
  <el-select
    popper-class="virtualselect"
    class="virtual-select-custom-style"
    :value="defaultValue"
    filterable
    :filter-method="filterMethod"
    default-first-option
    clearable
    :placeholder="placeholder"
    :disabled="disabled"
    :multiple="isMultiple"
    :allow-create="allowCreate"
    @visible-change="visibleChange"
    v-on="$listeners"
    @clear="clearChange"
  >
    <virtual-list
      ref="virtualList"
      class="virtualselect-list"
      :data-key="value"
      :data-sources="selectArr"
      :data-component="itemComponent"
      :keeps="keepsParams"
      :extra-props="{
          label: label,
          value: value,
          isRight: isRight,
          isConcat: isConcat,
          concatSymbol: concatSymbol
        }"
    ></virtual-list>
  </el-select>
</template>
<script>
const validatenull = (val) => {
  if (typeof val === 'boolean') {
    return false
  }
  if (typeof val === 'number') {
    return false
  }
  if (val instanceof Array) {
    if (val.length===0) return true
  } else if (val instanceof Object) {
    if (JSON.stringify(val) === '{}') return true
  } else {
    if (val==='null' || val===null || val==='undefined' || val===undefined || val==='') return true
    return false
  }
  return false
}
import virtualList from 'vue-virtual-scroll-list'
import ElOptionNode from './elOptionNode.vue'
export default {
  name: 'VirtualSelect',
  components: {
    'virtual-list': virtualList
  },
  model: {
    prop: 'bindValue',
    event: 'change'
  },
  props: {
    // 数组
    list: {
      type: Array,
      default() {
        return []
      }
    },
    // 显示名称
    label: {
      type: String,
      default: ''
    },
    // 标识
    value: {
      type: String,
      default: ''
    },
    // 是否拼接label | value
    isConcat: {
      type: Boolean,
      default: false
    },
    // 拼接label、value符号
    concatSymbol: {
      type: String,
      default: ' | '
    },
    // 显示右边
    isRight: {
      type: Boolean,
      default: false
    },
    // 加载条数
    keepsParams: {
      type: Number,
      default: 10
    },
    // 绑定的默认值
    bindValue: {
      type: [String, Number, Array],
      default() {
        if (typeof this.bindValue === 'string') return ''
        return []
      }
    },
    // 是否多选
    isMultiple: {
      type: Boolean,
      default: false
    },
    // 输入框占位文本
    placeholder: {
      type: String,
      default: '请选择'
    },
    // 是否禁用
    disabled: {
      type: Boolean,
      default: false
    },
    // 是否允许创建条目
    allowCreate: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      itemComponent: ElOptionNode,
      selectArr: [],
      defaultValue: null // 绑定的默认值
    }
  },
  watch: {
    'list'() {
      this.init()
    },
    bindValue: {
      handler(val, oldVal) {
        this.defaultValue = this.bindValue
        if (validatenull(val)) this.clearChange()
        this.init()
      },
      immediate: false,
      deep: true
    }
  },
  mounted() {
    this.defaultValue = this.bindValue
    this.init()
  },
  methods: {
    init() {
      if (!this.defaultValue || this.defaultValue?.length === 0) {
        this.selectArr = this.list
      } else {
        // 回显问题
        // 由于只渲染固定keepsParams(10)条数据,当默认数据处于10条之外,在回显的时候会显示异常
        // 解决方法:遍历所有数据,将对应回显的那一条数据放在第一条
        this.selectArr = JSON.parse(JSON.stringify(this.list))
        let obj = {}
        if (typeof this.defaultValue === 'string' && !this.isMultiple) {
          if (this.allowCreate) {
            const arr = this.selectArr.filter(val => {
              return val[this.value] === this.defaultValue
            })
            if (arr.length === 0) {
              const item = {}
              // item[this.value] = `Create-${this.defaultValue}`
              item[this.value] = this.defaultValue
              item[this.label] = this.defaultValue
              item.allowCreate = true
              this.selectArr.push(item)
              this.$emit('selChange', item)
            } else {
              this.$emit('selChange', arr[0])
            }
          }

          // 单选
          for (let i = 0; i < this.selectArr.length; i++) {
            const element = this.selectArr[i]
            if (element[this.value]?.toLowerCase() === this.defaultValue?.toLowerCase()) {
              obj = element
              this.selectArr?.splice(i, 1)
              break
            }
          }
          this.selectArr?.unshift(obj)
        } else if (this.isMultiple) {
          if (this.allowCreate) {
            this.defaultValue.map(v => {
              const arr = this.selectArr.filter(val => {
                return val[this.value] === v
              })
              if (arr?.length === 0) {
                const item = {}
                // item[this.value] = `Create-${v}`
                item[this.value] = v
                item[this.label] = v
                item.allowCreate = true
                this.selectArr.push(item)
                this.$emit('selChange', item)
              } else {
                this.$emit('selChange', arr[0])
              }
            })
          }

          // 多选
          for (let i = 0; i < this.selectArr.length; i++) {
            const element = this.selectArr[i]
            this.defaultValue?.map(val => {
              if (element[this.value]?.toLowerCase() === val?.toLowerCase()) {
                obj = element
                this.selectArr?.splice(i, 1)
                this.selectArr?.unshift(obj)
              }
            })
          }
        }
      }
    },
    // 搜索
    filterMethod(query) {
      if (!validatenull(query?.trim())) {
        this.$refs.virtualList.scrollToIndex(0) // 滚动到顶部
        setTimeout(() => {
          this.selectArr = this.list.filter(item => {
            return this.isRight || this.isConcat
              ? (item[this.label].trim()?.toLowerCase()?.indexOf(query?.trim()?.toLowerCase()) > -1 || item[this.value]?.toLowerCase()?.indexOf(query?.trim()?.toLowerCase()) > -1)
              : item[this.label]?.toLowerCase()?.indexOf(query?.trim()?.toLowerCase()) > -1
          })
        }, 100)
      } else {
        setTimeout(() => {
          this.init()
        }, 100)
      }
    },
    visibleChange(bool) {
      if (!bool) {
        this.$refs.virtualList.reset()
        this.init()
      }
      this.$emit('visible-change', bool)
    },
    clearChange() {
      if (typeof this.defaultValue === 'string') {
        this.defaultValue = ''
      } else if (this.isMultiple) {
        this.defaultValue = []
      }
      this.visibleChange(false)
    }
  }
}
</script>
<style scoped>
/* .virtual-select-custom-style ::v-deep .el-select-dropdown__item {
  // 设置最大宽度,超出省略号,鼠标悬浮显示
  // options 需写 :title="source[label]"
  width: 250px;
  display: inline-block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
} */
.virtualselect {
  max-height:245px;
  overflow-y:auto;
  /* // 设置最大高度
  &-list {
    max-height:245px;
    overflow-y:auto;
  } */
}
.virtualselect-list {
  max-height:245px;
  overflow-y:auto;
}
.el-select {
  width: 100%;
}
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
  background-color: transparent;
  cursor: pointer;
  margin-right: 5px;
}
::-webkit-scrollbar-thumb {
  background-color: rgba(144,147,153,.3) !important;
  border-radius: 3px !important;
}
::-webkit-scrollbar-thumb:hover{
  background-color: rgba(144,147,153,.5) !important;
}
::-webkit-scrollbar-track {
  background-color: transparent !important;
  border-radius: 3px !important;
  -webkit-box-shadow: none !important;
}
::v-deep  .el-select__tags {
  flex-wrap: unset;
  overflow: auto;
}
</style>
<style>
.virtualselect .el-select-dropdown__item{
  display: block;
  //max-width: 350px;
  overflow: visible;
}
</style>
用法

引入组件

import ElSelectV2 from '@/components/elSelectV2/VirtualSelect.vue';

使用组件,和el-select基本一致,具体参数见上面的注释

<el-select-v2
	v-model.trim="form.value"
	:list="selectListData"
	label="label"
	value="value"
	placeholder="请选择"
	multiple
	collapse-tags
	filterable
	@change="(val) => changeSelect"
/>

标签:el,return,val,default,value,item,vue2,type,select
From: https://blog.csdn.net/yan1915766026/article/details/141599116

相关文章

  • xshell使用跳板机ssh连结远程后使用SFTP
    xshell连结跳板机后,再使用ssh连结到真实机器,SFTP只能显示原跳板机的目录,不能显示真实目录.可以使用隧道,代理来显示真实目标机器的目录.参考:https://blog.csdn.net/qq_43797186/article/details/1236690711.在原跳板机的ssh连结,增加隧道ssh连结属性->连结->SSH->隧道......
  • avue-crud 新增弹窗里根据select的内容动态控制显隐其它字段
    @change="handleChange"<avue-crud:option="option":table-loading="loading":data="data"ref="crud":cell-style="{padding:'0'......
  • 4.Python操控Excel之格式
    1.设置字体斜体、加粗、颜色2.计算公式3.设置表格高度和宽度4.指定单元格合并5.取消指定单元格合并 ......
  • 关于java中Excel的导入导出
    前言提示:注意看文字的提醒:例如:提示:就这几个表别搞乱了就行其实很简单ExcelClassField------Excel标题栏工具类–不用管ExcelExport------导出配置工具类—用于对象表的配置上ExcelImport----导入配置工具类—用于对象表的配置上ExcelUtils-----用于接口调用上 一、配置pom......
  • Apache SeaTunnel技术架构演进及其在AI领域的应用
    随着数据集成需求的增长,ApacheSeaTunnel作为新一代的数据同步引擎,不仅在技术架构上不断演进,也在AI领域展现出其独特的应用价值。在CommunityOverCodeAsia2024大会上,ApacheSeaTunnelPMCChair高俊深入探讨SeaTunnel的技术演进路径,分析其在AI领域的应用案例,并展望未来的发展......
  • [kernel] 带着问题看源码 —— 脚本是如何被 execve 调用的
    前言在《[apue]进程控制那些事儿》一文的"进程创建->exec->解释器文件"一节中,曾提到脚本文件的识别是由内核作为exec系统调用处理的一部分来完成的,并且有以下特性:指定解释器的以#! (shebang)开头的第一行长度不得超过128shebang最多只能指定一个参数shebang指......
  • 微信小程序报 For developer:Two-way binding does not support complex data paths c
    微信小程序报:Fordeveloper:Two-waybindingdoesnotsupportcomplexdatapathscurrently.Thistwo-waybindingisignored.翻译过来是:对于开发人员:双向绑定目前不支持复杂的数据路径。这种双向绑定被忽略。原因:model:value不是能双向绑定子对象的值,如定义了data:......
  • selenium4在使用 下载驱动的时候报错: THIRD_PARTY_NOTICES.chromedriver
    在使用seeleniun自动下载驱动时报错:THIRD_PARTY_NOTICES.chromedriver原来的代码运行一直没有错误的,现在运行后下载下来的驱动是上面的格式导致运行报错,在github和google上查了官方已经修复在4.0.2版本中已经修复通过重新安装或者升级安装pipuninstallwebdriver-manag......
  • 搭建ELK-Filebeat采集系统日志
    1、解压到/data/elk/filebeatmkdir-p/data/elk/filebeattar-zxffilebeat-7.17.7-linux-x86_64.tar.gz-C/data/elk/filebeat--strip-components=1#--strip-components选项表示从目录级别上去除指定的前缀,以实现更加控制解压的效果2、修改配置文件vi/data/elk/fileb......
  • BAdam A Memory Efficient Full Parameter Optimization Method for Large Language M
    目录概BAdam代码LuoQ.,YuH.andLiX.BAdam:Amemoryefficientfullparameteroptimizationmethodforlargelanguagemodels.arXivpreprint,2024.概本文介绍了一种Blockcorrdinatedescent(BCD)的训练方式.BAdam当模型本身很大的时候,训练它会成为一......