uniapp uni-combox 数据源使用对象,选择后获取对应项的ID,可指定自定义的balbel,value

背景:uniApp中uni-combox数据源只支持接受一维数组,无法对每个选项指定具体的id,更无法实现选中后返回id,对原有的源码进行改造后如下:

<template><view class="uni-combox" :class="border ? '' : 'uni-combox__no-border'"><view v-if="label" class="uni-combox__label" :style="labelStyle"><text>{{label}}</text></view><view class="uni-combox__input-box"><input class="uni-combox__input" type="text" :placeholder="placeholder" placeholder-class="uni-combox__input-plac"v-model="inputVal" @input="onInput" @focus="onFocus" @blur="onBlur" /><uni-icons :type="showSelector? 'top' : 'bottom'" size="14" color="#999" @click="toggleSelector"></uni-icons></view><view class="uni-combox__selector" v-if="showSelector"><view class="uni-popper__arrow"></view><scroll-view scroll-y="true" class="uni-combox__selector-scroll" @scroll="onScroll"><view class="uni-combox__selector-empty" v-if="filterCandidatesLength === 0"><text>{{emptyTips}}</text></view><view class="uni-combox__selector-item" v-for="(item,index) in filterCandidates" :key="index"@click="onSelectorClick(index)"><text>{{item[`${labelKey}`]}}</text></view></scroll-view></view><uni-icons style="padding-left:20rpx;" class="content-clear-icon" type="clear" color="#c0c4cc" v-if="inputVal"@click="onClear"></uni-icons><!-- :size="clearSize":color="msg ? '#dd524d' : focusShow ? primaryColor : '#c0c4cc'" --><!-- 新增蒙层,点击蒙层时关闭选项显示 --><view class="uni-combox__mask" v-show="showSelector" @click="showSelector = false"></view></view></template><script>import {nextTick} from 'vue'/*** Combox 组合输入框* @description 组合输入框一般用于既可以输入也可以选择的场景* @tutorial https://ext.dcloud.net.cn/plugin?id=1261* @property {String} label 左侧文字* @property {String} labelWidth 左侧内容宽度* @property {String} placeholder 输入框占位符* @property {Array} candidates 候选项列表* @property {String} emptyTips 筛选结果为空时显示的文字* @property {String} value 组合框的值*/export default {name: 'uniCombox',emits: ['input', 'update:modelValue', 'change'],props: {border: {type: Boolean,default: true},label: {type: String,default: ''},labelWidth: {type: String,default: 'auto'},placeholder: {type: String,default: ''},candidates: {type: Array,default () {return []}},emptyTips: {type: String,default: '无匹配项'},labelKey: {type: String,default: 'dictName'},valueKey: {type: String,default: 'dictId'},// #ifndef VUE3value: {type: [String, Number],default: ''},// #endif// #ifdef VUE3modelValue: {type: [String, Number],default: ''},// #endif},data() {return {showSelector: false,inputVal: '',blurTimer: null,dictVal: "",filterCandidates: []}},computed: {labelStyle() {if (this.labelWidth === 'auto') {return ""}return `width: ${this.labelWidth}`},filterCandidatesLength() {console.log(this.filterCandidates)return this.filterCandidates.length}},watch: {// #ifndef VUE3value: {handler(newVal) {this.dictVal = newVal},immediate: true},// #endif// 因为获取列表是个异步的过程,需要对列表进行监听candidates: {handler(arr) {if (arr.length > 0 && this.dictVal) {let obj = arr.find((item, index) => {return this.dictVal == item[`${this.valueKey}`]})this.inputVal = obj[`${this.labelKey}`]this.$forceUpdate(); // 强制更新 DOM}this.filterCandidates = arr.filter((item) => {return item[`${this.labelKey}`].toString().indexOf(this.inputVal) > -1})},immediate: true,deep: true},// #ifdef VUE3modelValue: {handler(newVal) {// this.inputVal = newValthis.dictVal = newValif (this.candidates.length > 0 && newVal) {let obj = this.candidates.find((item, index) => {return newVal == item[`${this.valueKey}`]})// 兼容当传入错误的id在待选列表找不到时候的错误if (obj) {this.inputVal = obj[`${this.labelKey}`]} else {this.inputVal = ''}} else if (!newVal) { //当传入的是空值时直接将上一次回填数据清空this.inputVal = ''}},immediate: true,deep: true,},// #endif},methods: {toggleSelector() {this.showSelector = !this.showSelector},onFocus() {this.filterCandidates = this.candidatesthis.showSelector = true},onBlur() {this.blurTimer = setTimeout(() => {this.showSelector = false}, 153)},onScroll() { // 滚动时将blur的定时器关掉if (this.blurTimer) {clearTimeout(this.blurTimer)this.blurTimer = null}},onSelectorClick(index) {// this.inputVal = this.filterCandidates[index]this.dictVal = this.filterCandidates[index][`${this.valueKey}`]//this.dictVal 的赋值一定要在this.inputVal前执行,//因为this.filterCandidates会监听this.inputVal的变化被重新赋值//这样在选择列表中非第一个选项会报错this.inputVal = this.filterCandidates[index][`${this.labelKey}`]this.showSelector = falsethis.$emit('input', this.dictVal)this.$emit('change', this.dictVal)this.$emit('update:modelValue', this.dictVal)},onInput() {this.filterCandidates = this.candidates.filter((item) => {console.log(item, this.labelKey)return item[`${this.labelKey}`].toString().indexOf(this.inputVal) > -1})setTimeout(() => {this.$emit('input', this.dictVal)this.$emit('update:modelValue', this.dictVal)})},/*** 清理内容* @param {Object} event*/onClear(event) {this.inputVal = '';},}}
</script><style lang="scss">.uni-combox {font-size: 14px;border: 1px solid #DCDFE6;border-radius: 4px;// padding: 6px 10px;padding: 10px 6px 10px 0;position: relative;/* #ifndef APP-NVUE */display: flex;/* #endif */// height: 40px;flex-direction: row;align-items: center;// border-bottom: solid 1px #DDDDDD;}.uni-combox__label {font-size: 16px;line-height: 22px;padding-right: 10px;color: #999999;}.uni-combox__input-box {padding-left: 10px;position: relative;/* #ifndef APP-NVUE */display: flex;/* #endif */flex: 1;flex-direction: row;align-items: center;}.uni-combox__input {flex: 1;font-size: 14px;height: 22px;line-height: 22px;}.uni-combox__input-plac {font-size: 14px;color: #999;}.uni-combox__selector {/* #ifndef APP-NVUE */box-sizing: border-box;/* #endif */position: absolute;top: calc(100% + 12px);left: 0;width: 100%;background-color: #FFFFFF;border: 1px solid #EBEEF5;border-radius: 6px;box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);z-index: 3;padding: 4px 0;}.uni-combox__selector-scroll {/* #ifndef APP-NVUE */max-height: 200px;box-sizing: border-box;/* #endif */}.uni-combox__selector-empty,.uni-combox__selector-item {/* #ifndef APP-NVUE */display: flex;cursor: pointer;/* #endif */line-height: 36px;font-size: 14px;text-align: center;// border-bottom: solid 1px #DDDDDD;padding: 0px 10px;white-space: nowrap;overflow: auto;}.uni-combox__selector-item::-webkit-scrollbar {width: 0;height: 0;}.uni-combox__selector-item:hover {background-color: #f9f9f9;}.uni-combox__selector-empty:last-child,.uni-combox__selector-item:last-child {/* #ifndef APP-NVUE */border-bottom: none;/* #endif */}// picker 弹出层通用的指示小三角.uni-popper__arrow,.uni-popper__arrow::after {position: absolute;display: block;width: 0;height: 0;border-color: transparent;border-style: solid;border-width: 6px;}.uni-popper__arrow {filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));top: -6px;left: 10%;margin-right: 3px;border-top-width: 0;border-bottom-color: #EBEEF5;}.uni-popper__arrow::after {content: " ";top: 1px;margin-left: -6px;border-top-width: 0;border-bottom-color: #fff;}.uni-combox__no-border {border: none;}.uni-combox__mask {width: 100%;height: 100%;position: fixed;top: 0;left: 0;z-index: 1;}
</style>

使用:

 <uni-combox :candidates="testList" labelKey="text" valueKey="idx" emptyTips='暂无数据' placeholder="请选择"v-model="groupId" @change="selectGroupab"></uni-combox>// 选择区/苑const selectGroupab = (val) => {console.log(' 选择改变回调', val)}
const groupId =ref('')
const testList =ref([{idx:1,text:'A区'},{idx:2,text:'B区'}])

 

有借鉴他人的修改的同时进行了容错,异步回填,传入空值容错等兼容。

可以用以上代码可以在项目的src/uni_modules/uni-combox/components/uni-combox/uni-combox.vue直接替换该文件,

也可以不进行安装,直接将以上文件当做一个组件使用

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/870978.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

文件读写操作之c语言、c++、windows、MFC、Qt

目录 一、前言 二、c语言文件读写 1.写文件 2.读文件 三、c文件读写 1.写文件 2.读文件 四、windows api文件读写 1.写文件 2.读文件 五、MFC文件读写 1.写文件 2.读文件 六、Qt文件读写 1.写文件 2.读文件 七、总结 一、前言 我们在学习过程中&#xff0c…

面向对象机考指南

目录 Eclipse使用 调字体大小 Ptg to JavaBean 解决控制台消失问题 第三题大题 控制台 Eclipse使用 调字体大小 Window —> Preferences 搜索font 点击Color and Fonts 找到Java 展示字体 这个这个 即可调节字体大小 Ptg to JavaBean 生成 空参构造 带参构造 gett…

C++基础(一)

目录 1.不同版本的hello word&#xff01; 2.namespace和&#xff1a;&#xff1a;域作用限定符以及using 2.1 namespace 2.2&#xff1a;&#xff1a; 2.3using用于展开域 3.C输入和输出 4.缺省参数 5.重载 6.引用 6.1引用介绍 6.2 引用的特性 注意&#xff1a; 6.4 c…

win11用户由中文名改为英文名

目录 前情提要 一喜一悲&#xff1a;找回“消失”的文件&#xff0c;却失去新建文件的权限。 找回“消失”的文件 ​编辑 失去新建文件的权限 核心问题&#xff1a;怎么解决右键只建立文件夹&#xff1f; 弯路1&#xff1a;获取管理员权限 弯路2&#xff1a;获取管理员权…

Linux基础(权限)+mysql(函数)+初始shell

[rootcentos ~]# whereis test.c test: /usr/bin/test /usr/share/man/man1/test.1.gz /usr/share/man/man1p/test.1p.gz [rootcentos home]# zip -r my.zip lesson1 将目录设为zip unzip my.zip -d xxxxxx 可指定要减压到的位置。 tar [-cxtzjvf] 文件与目录 .... 参数&…

python自动化之用flask库写一个登陆接口(代码示例)

用到的库&#xff1a; 1、flask&#xff08;写接口&#xff09; 2、cerberus&#xff08;校验数据&#xff09; 实现效果&#xff1a;输入账号和密码&#xff0c;校验数据类型是否是字符串&#xff0c;如果是&#xff0c;返回登陆成功&#xff1b;如果不是&#xff0c;返回数…

UnityHub 无法添加模块问题

文章目录 1.问题描述2.问题解决 1.问题描述 在Hub中无法添加模块 2.问题解决 1、点击设置 2、设置版本安装位置 可以发现installs的安装位置路径设置不是unity安装位置&#xff0c;这里我们更改成自己电脑unity安装位置的上一级路径 添加模块正常&#xff1a;

【C++】哈希表的模拟实现及 unordered_set 和 unorderded_map 的封装

目录 前言一、哈希表的模拟实现1.1 哈希表的改造1.1.1 模板参数列表的改造1.1.2 增加迭代器操作 1.2 哈希表的模拟实现1.2.1 哈希表中仿函数的实现1.2.2 哈希表中节点类的实现1.2.3 哈希表中迭代器类的实现1.2.4 哈希表中构造函数、析构函数和 Clear() 函数的实现1.2.5 哈希表中…

阐述 C 语言中的浮点数精度问题?

&#x1f345;关注博主&#x1f397;️ 带你畅游技术世界&#xff0c;不错过每一次成长机会&#xff01; &#x1f4d9;C 语言百万年薪修炼课程 【https://dwz.mosong.cc/cyyjc】通俗易懂&#xff0c;深入浅出&#xff0c;匠心打磨&#xff0c;死磕细节&#xff0c;6年迭代&…

matlab支持向量机使用错误

&#x1f3c6;本文收录于《CSDN问答解答》专栏&#xff0c;主要记录项目实战过程中的Bug之前因后果及提供真实有效的解决方案&#xff0c;希望能够助你一臂之力&#xff0c;帮你早日登顶实现财富自由&#x1f680;&#xff1b;同时&#xff0c;欢迎大家关注&&收藏&…

ROS的TF系统

一、SLAM 1、SLAM全称是Simultaneous Localization And Mapping&#xff0c;即同时定位与地图构建 2、SLAM软件包Hector_Mapping&#xff0c;←建图可参考链接所示文章 二、机器人定位 1、假设机器人开始建图的位置是地图坐标系的原点 2、则机器人在建图过程中的位置可以描…

DenseNet算法实现乳腺癌识别

&#x1f368; 本文为&#x1f517;365天深度学习训练营 中的学习记录博客&#x1f356; 原作者&#xff1a;K同学啊 一、前期准备 1.设置GPU import torch import torch.nn as nn import torchvision.transforms as transforms import torchvision from torchvision import …

CUDA原子操作

代码 #include <cuda_runtime.h> #include <stdio.h>__global__ void atomicAddAndGet(int *result, int *valueToAdd) {// 原子加法int addedValue atomicAdd(result, *valueToAdd);// 通过原子操作后读取值&#xff0c;确保是加法后的值addedValue *valueToAd…

黑马程序员2024最新SpringCloud微服务开发与实战 个人学习心得、踩坑、与bug记录 Day4

你好,我是Qiuner. 为帮助别人少走弯路和记录自己编程学习过程而写博客 这是我的 github https://github.com/Qiuner ⭐️ gitee https://gitee.com/Qiuner &#x1f339; 如果本篇文章帮到了你 不妨点个赞吧~ 我会很高兴的 &#x1f604; (^ ~ ^) 想看更多 那就点个关注吧 我会…

HDFS Decommission节点的长尾分析和问题解决

文章目录 前言Decommission过慢的分析过程NameNode页面并不显示Decommission的进度和剩余块数量增加每次调度的块数量增加Stream Limit以避免节点被Skip节点被Skip时应该在DEBUG时打印原因在大量节点被Skip的时候加快有效调度其他可能改进 基本流程解析用户通过节点刷新触发Dec…

java Web学习笔记(一)

文章目录 1. 前置学习知识2. Tomcat介绍Tomcat目录文件介绍URL的组成部分和项目资源的对应关系idea配置部署tomcat并成功运行一个app-web项目 3. HTTP协议介绍&#xff08;很重要&#xff09;HTTP协议和HTTPS的区别HTTP协议的发展理解HTTP协议 报文格式报文响应的状态码 4. Ser…

以设备为核心的状态自动采集、人工运维和预测性维护为一体的智能管理系统

中服云设备全生命周期管理系统充分利用物联网、人工智能、机器学习、大数据等新一代技术&#xff0c;实现对企业生产设备从采购、安装、调试、使用、维护、维修、改造、更新直到报废全生命周期的智能化、数字化、可视化的实时管控&#xff0c;支持设备运行状态的自动采集和人工…

js 中 new Worker 报错 Failed to construct ‘Worker‘

new Worker("worker.js");运行多线程 Web Worker 的时候报错 Uncaught DOMException: Failed to construct ‘Worker’ 原因是浏览器不允许通过本地文件访问使用Web Worker。 解决方法&#xff1a; 将项目部署到服务器上或者用Node起本地服务访问项目

活动回顾|矩阵起源2024WAIC圆满落幕

2024年7月4日至7日&#xff0c;全球瞩目的2024世界人工智能大会暨人工智能全球治理高级别会议&#xff08;WAIC 2024&#xff09;在上海盛大举行&#xff0c;其中Future Tech 100未来之星创新孵化展再次成为大会焦点&#xff0c;它不仅是一场科技成果的展示&#xff0c;更是全球…

网络通信基本知识

网络通信 什么是网络通信&#xff1f; 通信网络是指将各个孤立的设备进行物理连接&#xff0c;实现人与人&#xff0c;人与计算机&#xff0c;计算机与计算机之间进行信息交换的链路&#xff0c;从而达到资源共享和通信的目的。 什么是网络协议&#xff1f; 网络协议是计算机…