uni-app下拉框 可实现输入下拉框 搜索+选择组合框功能

这篇具有很好参考价值的文章主要介绍了uni-app下拉框 可实现输入下拉框 搜索+选择组合框功能。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

uni-app下拉框 可输入下拉框 搜索+选择组合框功能

效果图

uni-app下拉框 可实现输入下拉框 搜索+选择组合框功能
uni-app下拉框 可实现输入下拉框 搜索+选择组合框功能
插件示例地址文章来源地址https://www.toymoban.com/news/detail-513380.html

https://ext.dcloud.net.cn/plugin?id=3756
<!-- 页面添加该标签 -->
<template>
    <view>
		<combox-search label="选择职位:" labelWidth="130px" emptyTips="无匹配选项" :isJSON="true" :keyName="keyName"
			placeholder="请选择职位" :candidates="zhiweiList" @getValue="getValue"></combox-search>
	</view>		
</template>

<script>
<!-- 引入该vue文件 -->
	import combox_Searchfrom "@/pages/components/cuihai-combox/cuihai-combox.vue"
	export default {
		components: {
			combox_Search 
		},
		data() {
			return {
				keyName: 'keyName',
				form: {
					
					zhiweiId: '', //职位id
					
				},
				zhiweiList: [], //职位
				
			}
		},
		onLoad() {
			this.getzhiweiList();
		
		},
		methods: {
			getValue(e) {
				console.log(e)
				this.form.zhiweiId= e.zhiweiId
			},
			onchange(e) {
				const value = e.detail.value
			},
			goBack() {
				uni.navigateTo({
					url: '/pages/menu/menu'
				})
			},
			changeLog(e) {},
			//查询职位
			getzhiweiList() {
				//查询方法接口
			},
		}

	}
</script>
//下载后并在项目中添加该文件
<template>
	<view class="uni-combox">
		<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" style="margin-left: -14px;" type="text" :placeholder="placeholder" :disabled="isDisabled" v-model="inputVal"
			 @input="onInput" @focus="onFocus" @blur="onBlur" />
			 <!--自测真机测试可以使用清除按钮 -->
			<icon type="clear" size="10" @tap="clearInputValue" v-if="!isDisabled" />
			<!-- <uni-icons class="uni-combox__input-arrow" type="arrowdown" size="14" @click="toggleSelector"></uni-icons> -->
			<view class="uni-combox__selector" v-if="showSelector">
				<scroll-view scroll-y="true" class="uni-combox__selector-scroll">
					<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(item)"
					 :style="isCheck(index)?'color:'+selectColor+';background-color:'+selectBgColor+';':'color:'+color+';'">
						<text>{{isJSON?item[keyName]:item}}</text>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	/**
	 * Combox 组合输入框
	 * @description 组合输入框一般用于既可以输入也可以选择的场景
	 * @property {String} label 左侧文字
	 * @property {String} labelWidth 左侧内容宽度
	 * @property {String} placeholder 输入框占位符
	 * @property {Array} candidates 候选项列表
	 * @property {String} emptyTips 筛选结果为空时显示的文字
	 * @property {String} value 单选时组合框的初始值
	 * @property {Array} initValue 多选时组合框的初始值(下标集合)
	 * @property {String} keyName json数组显示的字段值
	 * @property {Boolean} isJSON 是否是json数组,默认不是
	 * @property {Boolean} isDisabled 是否是禁用输入,默认不禁用
	 * @property {Boolean} isCheckBox 是否是多选,默认不是,多选时不能输入查询
	 * @property {String} color 默认字体颜色,默认#000000
	 * @property {String} selectColor 选中字体颜色,默认#0081ff
	 * @property {String} selectBgColor 选中背景颜色,默认#e8e8e8
	 */
	export default {
		name: 'comboxSearch',
		props: {
			label: {
				type: String,
				default: ''
			},
			labelWidth: {
				type: String,
				default: 'auto'
			},
			placeholder: {
				type: String,
				default: ''
			},
			candidates: {
				type: Array,
				default () {
					return []
				}
			},
			emptyTips: {
				type: String,
				default: '无匹配项'
			},
			value: {
				type: String,
				default: ''
			},
			initValue: {
				type: Array,
				default: null
			},
			keyName: {
				type: String,
				default: ''
			},
			isJSON: {
				type: Boolean,
				default: false
			},
			isDisabled: {
				type: Boolean,
				default: false
			},
			isCheckBox: {
				type: Boolean,
				default: false
			},
			color: {
				default: "#000000",
				type: String
			},
			selectColor: {
				default: "#0081ff",
				type: String
			},
			selectBgColor: {
				default: "#e8e8e8",
				type: String
			}
		},
		data() {
			return {
				showSelector: false,
				inputVal: '',
				arrays: [],
				gid: `sm-org-dropDown-${(new Date()).getTime()}${Math.random()}`
			}
		},
		computed: {
			labelStyle() {
				if (this.labelWidth === 'auto') {
					return {}
				}
				return {
					width: this.labelWidth
					//width: 130
				}
			},
			filterCandidates() {
				if (!this.isDisabled) {
					if (this.isJSON) {
						let index = 0;
						return this.candidates.filter((item) => {
							item.index = index;
							index++;
							return item[this.keyName].indexOf(this.inputVal) > -1
						})
					} else {
						return this.candidates.filter((item) => {
							return item.indexOf(this.inputVal) > -1
						})
					}
				} else {
					if (this.isJSON) {
						let index = 0;
						return this.candidates.filter((item) => {
							item.index = index;
							index++;
							return item[this.keyName] != undefined;
						})
					} else {
						return this.candidates
					}
				}
			},
			filterCandidatesLength() {
				return this.filterCandidates.length
			}
		},
		created() {
			if (this.initValue != null) {
				this.arrays = this.initValue;
				this.inputVal = this.getInpuevals();
			}
			//在created的时候,给子组件放置一个监听,这个时候只是监听器被建立,此时这段代码不会生效
			//uni.$on接收一个sm-org-dropDown-show的广播
			uni.$on('sm-org-dropDown-show', (targetId) => {
				//接收广播,当该组件处于下拉框被打开的状态,并且跟下一个被点击的下拉框的gid不同时,将该组件的下拉框关闭,产生一个互斥效果。
				if (this.showSelector && this.gid != targetId) {
					this.showSelector = false
				}
			})
		},
		//当子组件销毁的时候,将建立的监听销毁,这里不会影响代码效果,但是在多次反复引用组件时,会在根节点定义越来越多的监听,长此以往影响性能,所以在不用的时候及时销毁,养成好习惯
		beforeDestroy() {
			uni.$off('sm-org-dropDown-show')
		},
		watch: {
			value: {
				handler(newVal) {
					this.inputVal = newVal
				},
				immediate: true
			}
		},
		methods: {
			toggleSelector() {
				this.showSelector = !this.showSelector
				if (this.showSelector) {
					uni.$emit('sm-org-dropDown-show', this.gid)
				}
			},
			onFocus() {
				this.showSelector = true;
				uni.$emit('sm-org-dropDown-show', this.gid)
			},
			onBlur() {
				/* setTimeout(() => {
					this.showSelector = false;
				}, 50) */
			},
			onSelectorClick(index) {
				var itemPlus = index
				var items = index.id
				index = index.index
				if (this.isCheckBox) {
					let flag = this.arrays.indexOf(index);
					if (flag != -1) {
						let x = (this.arrays || []).findIndex((item) => item === index)
						this.arrays.splice(x, 1);
					} else {
						this.arrays.push(index);
					}
					this.inputVal = this.getInpuevals();
					if (this.isJSON) {
						this.$emit('getValue', this.arrays);
					} else {
						this.$emit('getValue', this.trimSpace(this.inputVal.split(" ")));
					}
				} else {
					this.showSelector = false
					if (this.isJSON) {
						this.$emit('getValue', itemPlus);
						//循环根据页面数据传入的数据下标获取搜索时的名称跟id  并展示
						for (var i = 0; i < this.filterCandidates.length; i++){
							if(this.filterCandidates[i].index == index) {
								this.inputVal = this.filterCandidates[i].name;
							}
							
						}
					} else {
						this.$emit('getValue', this.filterCandidates[index]);
						this.inputVal = this.filterCandidates[index]
					}
				}
			},
			trimSpace(array) {
				for (var i = 0; i < array.length; i++) {
					if (array[i] == "") {
						array.splice(i, 1);
						i = i - 1;
					}
				}
				return array;
			},
			onInput() {
				setTimeout(() => {
					this.$emit('input', this.inputVal)
				})
			},
			clearInputValue() {
				this.inputVal = "";
				this.showSelector = false;
			},
			getInpuevals() {
				if (this.arrays.length == 0) {
					this.inputVal = "";
				} else {
					this.arrays.sort(function(a, b) {
						return a - b
					})
					this.inputVal = "";
					if (this.isJSON) {
						this.arrays.forEach(v => {
							this.inputVal += this.candidates[v][this.keyName] + " ";
						});
					} else {
						this.arrays.forEach(v => {
							this.inputVal += this.candidates[v] + " ";
						});
					}
				}
				return this.inputVal;
			},
			isCheck(index) {
				
				return this.arrays.indexOf(index) != -1;
			}
		}
	}
</script>

到了这里,关于uni-app下拉框 可实现输入下拉框 搜索+选择组合框功能的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • uni-app-使用tkiTree组件实现树形结构选择

    前言 在实际开发中我们经常遇见树结构-比如楼层区域-组织架构-部门岗位-系统类型等情况 往往需要把这个树结构当成条件来查询数据,在PC端可以使用Tree,table,Treeselect等组件展示 在uni-app的内置组件中似乎没有提供这样组件来展示,但是是有第三方包tkiTree组件来解决这个

    2024年02月14日
    浏览(43)
  • uni-app顶部下拉舒心

    操作步骤 基于 scroll-view 组件实现下拉刷新,需要通过以下方式来实现下拉刷新的功能。 配置 refresher-enabled 属性,开启下拉刷新交互 监听 @refresherrefresh 事件,判断用户是否执行了下拉操作 配置 refresher-triggered 属性,关闭下拉状态

    2024年01月24日
    浏览(28)
  • uni-app 自定义下拉框

    如图:     html: view class=\\\"row-item\\\" view class=\\\"lable-tit\\\"性别:/view view class=\\\"selected-all\\\" view class=\\\"drop-down-box\\\" @click=\\\"btnShowHideClick\\\" text class=\\\"dropdown-content\\\"{{choiceContent}}/text image class=\\\"dropdown-icon\\\" src=\\\"/static/down.png\\\" mode=\\\"widthFix\\\"/image /view  view class=\\\"dialog-view\\\" v-if=\\\"isShowChoice\\\" text :class=\\\"choiceI

    2023年04月19日
    浏览(29)
  • uni-app中select下拉菜单

    select-lay - DCloud 插件市场

    2024年02月12日
    浏览(27)
  • uni-app在app端设置下拉刷新不生效

    下拉刷新这个问题,查了老半天。 我这只小菜鸡,完全不知道view和scroll-view是不一样的! 按照官网的方式, 1. 在 pages.json 里,找到的当前页面的pages节点,并在 style 选项中开启 enablePullDownRefresh: true 2. 在页面中使用uni.startPullDownRefresh();以及uni.stopPullDownRefresh(); 可是可是,我

    2024年02月11日
    浏览(30)
  • 前端vue uni-app仿美团下拉框下拉筛选组件

    在前端Web开发中,下拉筛选功能是一种非常常见的交互方式,它可以帮助用户快速选择所需的选项。本文将介绍如何利用Vue.js和uni-app框架来实现一个高效的下拉筛选功能。通过使用这两个强大的前端框架,我们可以轻松地创建具有响应式用户操作的下拉筛选组件。 首先,我

    2024年02月09日
    浏览(33)
  • 【Uni-App】uniapp使用uview实现弹出键盘输入密码/验证码功能

    组件使用的是uview组件,Keyboard 键盘和MessageInput 验证码输入两个组件配合使用。 通过mode参数定义键盘的类型,v-model绑定一个值为布尔值的变量,我绑定的是showKeyboard变量,控制键盘的弹出与收起; mode = number (默认值)为数字键盘,此时顶部工具条中间的提示文字为\\\"数字键盘

    2023年04月16日
    浏览(39)
  • uni-app封装省市区下拉组件(后台获取数据)

    一.后台数据格式  PROCINCE:[{itemName:\\\'\\\',itemValue:\\\'\\\'}] CITY:[{itemName:\\\'\\\',itemValue}] AREA:[{itemName:\\\'\\\',itemValue}] 前端将地址数据缓存在了pinia中 前端主要使用picker进行勾选 二.代码

    2024年02月12日
    浏览(39)
  • uni-app uni-ui 微信小程序 uni-datetime-picker 时间选择组件设置start和end属性,实现时间选择限制

     效果如图,先选择开始日期,完成日期需要在开始日期之后,先选择完成日期,开始日期需要在完成日期之前 需要用到uni-datetime-picker官方的三个属性  代码如下 这样一个能够限制选定范围的组件就ok了!    

    2024年02月11日
    浏览(45)
  • uni-app日期选择器

    写个简单的日期选择器,还没搞样式,所以有点丑 大概长这样吧 首先是这个picker选择器,mode选择日期,end是写一个范围前日期,:end就是这个日期是动态变化的,还有change函数 然后是脚本,就默认显示当前日期,设计最后范围日期为今日日期,再有一个事件函数

    2024年02月12日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包