当前位置: 首页 > news >正文

vue2 ant-design select组件自定义下拉框, dropdownRender 使用,以及遇到的坑

业务需求:下拉框需要满足用户可输入筛选 和 点击右侧 字符按钮 #A-Z进行用户选择

1、基础页面代码

<div><a-selectstyle="width: 100%"placeholder="请选择客户"allow-clearshow-search:filter-option="false":not-found-content="fetching ? undefined : null":defaultActiveFirstOption="false":getPopupContainer="getPopupContainer"@search="searhcCust"@dropdownVisibleChange="dropdownVisibleChange">//dropdownRender 插件使用,自定义右边<divslot="dropdownRender"slot-scope="menu"><div class="index-bar"><divv-for="letterItem in letter":key="letterItem"@click.prevent="scrollOn(letterItem)"class="index-letter"@mousedown="e => e.preventDefault()" // 阻止调用默认事件>{{ letterItem }}</div></div><v-nodes :vnodes="menu" /> // 注意要在components中定义</div><a-select-opt-groupv-for="(group, index) in peoArray":key="index"><spanslot="label"class="option-letter":id="'peo_' + componentId + group.key">{{group.key}}</span><a-select-optionv-for="option in group.list":key="option.custRowId":value="option.custRowId":title="option.custName"><span>{{ option.custName }}</span></a-select-option></a-select-opt-group></a-select></div>

2、script中的代码

<script>
import { debounce } from 'lodash'
export default {props: {componentId: { // 设置不同的id,用于同页面有多个此组件时锚点不生效type: String,default: ''}},components: {VNodes: {functional: true,render: (h, ctx) => ctx.props.vnodes,}},data() {return {dropOpen: false,searchValue: '',navBarHeight: '50px', // 导航栏高度letter: [], // 字母检索列表peoArray: [], // 通讯录列表custList: null,custRowId: undefined,fetching: false,debounceGetCustInfoKeyList: null,}},created() {this.getCustInfoKeyList()this.debounceGetCustInfoKeyList = debounce(this.getCustInfoKeyList, 500)},methods: {dropdownVisibleChange(open) {this.dropOpen = open},getPopupContainer(triggerNode) {if (this.modalSelect) {return triggerNode.parentNode} else {return document.body}},changeCust(val) {this.$emit('change', val)},getList(peoArray) {let newList = []peoArray.forEach(element => {newList.push(...element.list)})return newList},searhcCust(val) {this.searchValue = valthis.debounceGetCustInfoKeyList()},getCustInfoKeyList() {const params = {custName: this.searchValue,}this.$http.XXX(params).then(res => {if (res.code === 200) {this.custList = res.dataif (this.custList) {this.setList()} else {this.peoArray = []}this.fetching = false} else {this.$message.warn(res.msg)this.fetching = false}})},setList() {let list = []this.letter = []for (const key in this.custList) {this.letter.push(key)list.push({key,list: this.custList[key]})}setTimeout(() => {this.peoArray = list})},// 字母检索scrollOn(item) {let target = document.getElementById('peo_' + this.componentId + item) // 获取每个字母通讯录对象if (target) {const scrollDom = document.getElementsByClassName('ant-select-dropdown-menu')[0]scrollDom.scrollTo({behavior: 'smooth',top: target.offsetTop - 10})} else {this.$message.warn(`当前${item}元素下暂无客户`)}}}
}
</script>

3、基础CSS代码


.index-bar {position: absolute;z-index: 99;right: 10px;top: 50%;transform: translateY(-50%);display: flex;flex-direction: column;align-items: center;
}.index-letter {margin: 0;cursor: pointer;color: #1677ff;font-weight: 500;font-size: 12px;line-height: 20px;
}
.option-letter {font-weight: 600;color: rgba(0, 0, 0, 0.45);
}

4、遇到的坑是什么呢?
就是在同一个页面,渲染同一个组件时,在点击前一个组件后,后面的组件右侧按钮滚动失效。
造成这个问题的原因就是 dropdownRender 渲染不会销毁,导致scrollOn获取到的DOM是同一组数据,解决方法有两种:

	// 1、在 dropdownRender 插件的地方<divv-if="dropOpen"slot="dropdownRender"slot-scope="menu">// 2、scrollOn 中修改查询Dom方法let target = document.getElementById('peo_' + this.componentId + item) // 获取每个字母通讯录对象if (target) {const parentDom = document.getElementById(offsetParent.id)const scrollDom = parentDom.children[0]scrollDom.scrollTo({behavior: 'smooth',top: target.offsetTop - 10})} else {this.$message.warning(`当前${item}元素下暂无客户`)}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • fastadmin 如何通过权限组来控制列的显示与隐藏
  • 老物件线上3D回忆展拓宽了艺术作品的展示空间和时间-深圳华锐视点
  • Sqlmap中文使用手册 - Request模块参数使用
  • Hutool-Guava
  • 如何安装dotenv,避坑指南,安装包的包名有误?
  • SpringBoot使用Redis(事务异步add + 更新)
  • 常见CSS属性
  • 学习小记-一些Redis小知识
  • 《警世贤文》摘抄:处人篇、受恩篇、宽人篇、听劝篇、劝善篇(多读书、多看报、少吃零食多睡觉)
  • 公司想无偿裁员,同事赖着不走
  • HTML+CSS+JS井字棋(来自动下棋)
  • 408数据结构-图的应用2-最短路径 自学知识点整理
  • RuntimeError: cuDNN error: CUDNN_STATUS_NOT_SUPPORTED.
  • 「Pytorch」roLabelImg 图像异常旋转 bug
  • 详解C#委托与事件
  • ----------
  • 2019.2.20 c++ 知识梳理
  • Angular Elements 及其运作原理
  • CentOS学习笔记 - 12. Nginx搭建Centos7.5远程repo
  • Java比较器对数组,集合排序
  • Java到底能干嘛?
  • JAVA之继承和多态
  • js中的正则表达式入门
  • vue从创建到完整的饿了么(11)组件的使用(svg图标及watch的简单使用)
  • vue和cordova项目整合打包,并实现vue调用android的相机的demo
  • vue--为什么data属性必须是一个函数
  • Wamp集成环境 添加PHP的新版本
  • 从 Android Sample ApiDemos 中学习 android.animation API 的用法
  • 快速体验 Sentinel 集群限流功能,只需简单几步
  • 利用DataURL技术在网页上显示图片
  • 配置 PM2 实现代码自动发布
  • 使用阿里云发布分布式网站,开发时候应该注意什么?
  • 中文输入法与React文本输入框的问题与解决方案
  • ​Z时代时尚SUV新宠:起亚赛图斯值不值得年轻人买?
  • ​十个常见的 Python 脚本 (详细介绍 + 代码举例)
  • #LLM入门|Prompt#3.3_存储_Memory
  • #免费 苹果M系芯片Macbook电脑MacOS使用Bash脚本写入(读写)NTFS硬盘教程
  • #数据结构 笔记三
  • $.ajax()方法详解
  • $分析了六十多年间100万字的政府工作报告,我看到了这样的变迁
  • (8)Linux使用C语言读取proc/stat等cpu使用数据
  • (C)一些题4
  • (二)延时任务篇——通过redis的key监听,实现延迟任务实战
  • (附源码)springboot学生选课系统 毕业设计 612555
  • (一)Spring Cloud 直击微服务作用、架构应用、hystrix降级
  • (转) 深度模型优化性能 调参
  • .[backups@airmail.cc].faust勒索病毒的最新威胁:如何恢复您的数据?
  • .dwp和.webpart的区别
  • .NET Core实战项目之CMS 第一章 入门篇-开篇及总体规划
  • .NET IoC 容器(三)Autofac
  • .NET/C# 判断某个类是否是泛型类型或泛型接口的子类型
  • .net程序集学习心得
  • .NET使用HttpClient以multipart/form-data形式post上传文件及其相关参数
  • /bin/rm: 参数列表过长"的解决办法
  • [ 环境搭建篇 ] 安装 java 环境并配置环境变量(附 JDK1.8 安装包)