首页 > 其他分享 >uniapp 远程搜索功能的封装

uniapp 远程搜索功能的封装

时间:2023-09-01 14:44:10浏览次数:29  
标签:uniapp 封装 String item default index inputVal uni 远程

封装组件

<template>
	<view class="uni-combox">
		<!-- 输入查询选择器 -->
		<view class="uni-combox__input-box">
			<input class="uni-combox__input" type="text" :placeholder="placeholder" @click="toggleSelector" :disabled="isDisabled" v-model="inputVal"
			 @input="onInput" @focus="onFocus" @blur="onBlur" />
			<uni-icons class="uni-combox__input-arrow" type="arrowdown" size="14" ></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(index)"
					 :style="isCheck(index)?'color:'+selectColor+';background-color:'+selectBgColor+';':'color:'+color+';'">
						<text>{{isJSON?(isAloneShow?item[keyName]:item[keyCode]+' _ '+item[keyName]):item}}</text>
						<!-- <text>{{isJSON?isAloneShow?(item.enumCode?item.enumCode: item.usercode):(item.enumCode?(item.enumCode+' _ '+item.enumValue) : item.usercode+' _ '+item.username):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 {String} keyCode json数组传参的字段值
	 * @property {Boolean} isJSON 是否是json数组,默认不是isAloneShow
	 * @property {Boolean} isAloneShow 是否单独显示
	 * @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: ''
			},
			keyCode: {
				type: String,
				default: ''
			},
			isJSON: {
				type: Boolean,
				default: false
			},
			isAloneShow: {
				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
			},
			indexVal:{
				type:[Number,String],
				default:()=>{
					return 0
				}
			}
		},
		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
				}
			},
			//下拉列表数据
			filterCandidates() {
				let _this = this
				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 || item[_this.keyCode].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.keyCode] != undefined ||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();
				// this.inputVal = '北京';
			}
			//在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) {
				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', this.filterCandidates[index].index);
						this.inputVal = this.filterCandidates[index].username;
					} else {
						this.$emit('getValue', this.filterCandidates[index]);
						this.inputVal = this.filterCandidates[index]
					}
				}
				this.$emit('inputVal', this.inputVal,this.indexVal)
			},
			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)
					this.$emit('inputVal', 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].username+ ",";
						});
					} else {
						this.arrays.forEach(v => {
							this.inputVal += this.candidates[v] + " ";
						});
					}
				}
				if(this.isJSON){
					this.inputVal = this.inputVal.substr(0, this.inputVal.length - 1);  
				}
				return this.inputVal;
			},
			isCheck(index) {
				return this.arrays.indexOf(index) != -1;
			}
		}
	}
</script>

<style scoped>
	.uni-combox {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		height: 40px;
		flex-direction: row;
		align-items: center;
		width: 100% ;
		/* border-bottom: solid 1px #DDDDDD; */
	}

	.uni-combox__label {
		font-size: 16px;
		line-height: 22px;
		padding-right: 10px;
		color: #999999;
	}

	.uni-combox__input-box {
		position: relative;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex: 1;
		flex-direction: row;
		align-items: center;
	}

	.uni-combox__input {
		flex: 1;
		font-size: 16px;
		height: 22px;
		line-height: 22px;
	}

	.uni-combox__input-arrow {
		padding: 10px;
	}

	.uni-combox__selector {
		box-sizing: border-box;
		position: absolute;
		top: 42px;
		left: 0;
		width: 100%;
		background-color: #FFFFFF;
		border-radius: 6px;
		box-shadow: #DDDDDD 4px 4px 8px, #DDDDDD -4px -4px 8px;
		z-index: 2;
	}

	.uni-combox__selector-scroll {
		max-height: 200px;
		box-sizing: border-box;
	}

	.uni-combox__selector::before {
		content: '';
		position: absolute;
		width: 0;
		height: 0;
		border-bottom: solid 6px #FFFFFF;
		border-right: solid 6px transparent;
		border-left: solid 6px transparent;
		left: 50%;
		top: -6px;
		margin-left: -6px;
	}

	.uni-combox__selector-empty,
	.uni-combox__selector-item {
		/* #ifdef APP-NVUE */
		display: flex;
		/* #endif */
		line-height: 36px;
		font-size: 14px;
		text-align: center;
		border-bottom: solid 1px #DDDDDD;
		/* margin: 0px 10px; */
	}

	.uni-combox__selector-empty:last-child,
	.uni-combox__selector-item:last-child {
		border-bottom: none;
	}
</style>

readme.md

<template>
	<view class="allInputOrder">
		<uni-forms label-align="right">
			<uni-forms-item label="推送人" name="pusher">
				<combox-search 
					label="" 
					labelWidth="100px" 
					:value="defaulPusher" 
					emptyTips="无匹配选项" 
					:isJSON="true" 
					:keyName="keyName" 
					:keyCode="keyCode" 
					:candidates="candidates" 
					placeholder="请选择" 
					@inputVal="inputVal"  
					@getValue="getValue($event,'json')"
					style="width: 90%;display: inline-block;"
					></combox-search>
			</uni-forms-item>
		</uni-forms>
	</view>
</template>

<script>
	import comboxSearch from "@/components/cuihai-combox/common-combox.vue"
	import {getUserList,} from "@/api/InjectionMoldingApi.js"//烘料扫码,提交
	
	export default{
		data(){
			return{
				candidates: [{usercode:uni.getStorageSync('usercode'),username:uni.getStorageSync('username')}],   //  查询列表
				keyName:'username', // 显示字段
				keyCode:'usercode',  // 上传字段
				defaulPusher:uni.getStorageSync('username'), // 默认显示
				form:{
					pusher:uni.getStorageSync('usercode'),//推送人
				},
			}
		},
		components:{
			comboxSearch
		},
		methods:{
			// 点击选择的值
			getValue(e,type){
				switch (type){
					case 'json':
						this.form.pusher=this.candidates[e].usercode; // usercode为定义的字段,点击选择的值赋值给传参
						break;
					case 'common':
						this.city=e;
						break;
					default:
						break;
				}
				console.log(e);
			},
			// 输入框参数,
			inputVal(data){
				this.getUserList(data)
			},
			// 查询接口
			getUserList(val){
				let data = {
					keyword:val,
					pageNum:1,
					pageSize:5
				} 
				getUserList(data).then(res=>{
					if(res.code == 'Y'){
						this.candidates = [];
						res.data.list.map(item =>{
							this.candidates.push({
								usercode:item.usercode,
								username:item.username
							})
						})
					}
					}).catch(()=>{
						
					})
			},
		}
	}
</script>

<style lang="scss" scoped>
	::v-deep .uni-combox{
		width: 80%;
		border: solid 1px #DDDDDD;
		border-radius: 3px;
	}
</style>

标签:uniapp,封装,String,item,default,index,inputVal,uni,远程
From: https://www.cnblogs.com/axingya/p/17671852.html

相关文章

  • .Net 6/NetCore3.1 Vue Element Uniapp前后端分离低代码快速开发框架
    .Net6/NetCoreVueElementUniapp前后端分离低代码快速开发框架这是一个能提高开发效率的开发框架,全自动生成PC与移动端(uniapp)代码;支持移动ios/android/h5/微信小程序。一、框架能做什么1、前后端分离项目2、纯后端项目3、移动端开发uni-app(IOS、Android、H5、微信小程......
  • 智能远程监考方案下的线上等级考试变革
    用指尖,「敲」下一段天马行空的想象。 7月底,2023青少年人工智能编程水平测试(YCL)如期开展,10万+考生完成线上应考。 作为“青少年信息技术培养工程”的重点项目之一,青少年人工智能编程水平测试,由工业和信息化部教育与考试中心、中国电子教育学会、中国工信出版传媒集团——北......
  • 应用交付交付管理(报表)系统LOGIN 远程命令执⾏漏洞
    漏洞描述深信服应⽤交付管理系统login存在远程命令执⾏漏洞,攻击者通过漏洞可以获取服务器权限,执⾏任意命令漏洞影响深信服应⽤交付管理系统7.0.8-7.0.8R5⽹络测绘fid="iaytNA57019/kADk8Nev7g=="登录页面如下:第一个POC输入账号密码拦截登录请求包,然后更改数据包。......
  • 使Windows11支持同时多个用户远程桌面连接(RDP)
    参考:https://www.wyr.me/post/701一、配置远程桌面服务更改限制连接的数量将用户限制到单独的远程桌面服务会话(可选)二、为termsrv.dll增加修改权限C:\Windows\System32\termsrv.dll详情请参考:https://www.wyr.me/post/701三、停止RemoteDesktopServices服务打开......
  • 使用第三方RDP(远程桌面)客户端远程连接Windows10/11
    一、打开「编辑组策略」并定位  二、指定RDP为安全层三、禁用「要求使用网络级别的身份验证……」......
  • 分布式光伏储能系统远程监控运维解决方案
    行业背景随着经济发展对于能源需求的不断提升,光伏发电作为一种重要的可再生清洁能源,受到国家和企业的重点关注。光伏发电是将太阳能转换为电能的过程,其输出功率“靠天吃饭”,容易受到太阳辐射强度、温度等环境因素影响,具有波动性、间歇性、不稳定性等特性。多地开始要求分布式新能源......
  • uniapp 项目实践总结(二)从零开始搭建一个项目
    导语:本篇文章主要是项目方面的技术开发总结,新建一个项目可以选择使用可视化界面,也可以使用命令行搭建。目录可视化界面命令行搭建安卓开发环境苹果开发环境可视化界面安装软件使用官方推荐的HbuilderX软件,开发方式比较简单,内置相关环境以及终端,无需配置node。下......
  • 远程连接阿里云服务器的几种方式(包括Windows和linux系统)
    远程连接阿里云服务器的几种方式(包括Windows和linux系统)_远程登录阿里云_库博客的博客-CSDN博客远程连接Windows系统阿里云服务器:1、workbench远程连接方式点击“立即登陆”,然后输入Administrator用户的密码,即可登录云服务器。2、VNC连接方式输入远程连接密码后,按照提示......
  • uniapp APP热更新
    在app.vue的onLaunch生命周期(应用初始化完成触发(只触发一次))请求接口,返回最新版本号,判断当前本地版本号月线上版本号是否一致//请求版本更新号getNewest().then((res)=>{ if(res.code=='200'&&res.data){ letopenUrl=plus.os.name==="......
  • win10神州网信政府版开启远程桌面服务
    1、打开组策略编辑器(gpedit.msc)2、计算机配置》管理模板》Windows组件》远程桌面服务》远程桌面会话主机》连接》“允许用户通过使用远程桌面服务进行远程连接”项,状态改为未配置,默认是禁用。3、计算机配置》管理模板》Windows组件》远程桌面服务》远程桌面会话主机》设备和资源......