uniapp多功能选择器组件,类似于elementui,可搜索可下拉可清空(基于uView二次封装),支持各端兼容,非常好用!!!

该组件已更新,查看具体请移步个人主页(2023/11/23)

1.最近在uniapp上做了一款类似elementui的多功能选择器的组件,可下拉可搜索可情况,支持异步请求渲染下拉列表

废话不多说,先看视频!!!!!

uniapp多功能下拉框组件

2.需要的环境

(1)因为组件基于uView二次封装,所以请安装uView组件,安装步骤请看uView官方文档

3.以下代码直接复制

        说明:@select方法可以在父组件拿到当前选择项的对象数据

<template>
	<view class="uni-combox" :class="border ? '' : 'uni-combox__no-border'" :style="{position:selectPosition}">
		<view class="uni-combox__input-box">
			<view v-if='!isShowInput'  @click="isShowInput = true" class="wraper-inpu-text" >
				<text v-if="inputValCopy" class="wrper-text-vlaue">{{inputValCopy}}</text>
				<text v-else class="wrper-text-placeholder" :style='placeholderStyle'>{{inputValPlaceholder}}</text>
			</view>
			
			<u-input v-else :placeholderStyle='placeholderStyle' class="uni-combox__input" focus type="text"
				:placeholder=" inputValPlaceholder" ref="input" placeholder-class="uni-combox__input-plac"
				v-model="inputVal" @input="onChange" @focus="onFocus" @blur="onBlur" />
		</view>
		<view class="uni-combox__selector" v-show="showSelector && dataList.length !== 0">
			<view class="uni-popper__arrow"></view>
			<u-list @scrolltolower="scrolltolower">
				<u-list-item v-for="(item, index) in dataList" :key="index">
					<u-cell :title="item[valueKey]" @click="onSelectorClick(index)"></u-cell>
				</u-list-item>
			</u-list>
			
		<!-- 	<view class="uni-combox__selector-empty" v-if="filterCandidatesLength === 0 && isLoading == false">
				<text>{{emptyTips}}</text>
			</view> -->
		</view>
	</view>
</template>

<script>
	export default {
		name: 'uniCombox',
		emits: ['input'],
		props: {
			placeholderStyle: {        //输入框placeholder状态的样式
				type: String,
				default: 'font-size:15px;color:#C0C4CC'
			},
			selectPosition: {          //是否在组件父元素添加定位  默认relative
				type: String,
				default: 'relative'

			},
			valueKey: {                 //父组件data数据需要在列表中渲染的key值
				type: String,
				default: 'companyName'
			},
			border: {
				type: Boolean,         //默认显示下边框
				default: true
			},
			label: {
				type: String,         //选择器label  不建议使用
				default: ''
			},
			labelWidth: {				//选择器label宽度  不建议使用
				type: String,
				default: 'auto'
			},
			placeholder: {				//选择器placeholder文本 
				type: String,
				default: ''
			},
			candidates: {				//选择器下拉框数据  格式为[{}] 
				type: Array,
				default () {
					return []
				}
			},
			emptyTips: {				//选择器下拉框数据为空文本  ---已注释掉  需要的可以手动打开
				type: String,	
				default: '无匹配项'
			},
			value: {
				type: [String, Number],
				default: ''
			},
			myMessage: {				//父组件的值 
				type: [String, Number],
				default: ''
			},
		},
		data() {
			return {
				dataList:[],
				showSelector: false,
				placeholderCopy: '',
				inputVal: '',
				inputValCopy:'',
				isShowInput:false,
				inputValPlaceholder:"",
				input:""
			}
		},
		computed: {
			labelStyle() {
				if (this.labelWidth === 'auto') {
					return ""
				}
				return `width: ${this.labelWidth}`
			},
			filterCandidates() {
				return this.candidates
				
			},
			filterCandidatesLength() {
				return this.filterCandidates.length
			}
		},
		watch: {
			myMessage: {
				handler(newVal) {
					
					
					if (newVal) {
						this.inputValCopy = newVal
						this.inputValPlaceholder = newVal
					}else{
						this.inputValCopy = newVal
						this.inputValPlaceholder = this.placeholder
					}
					
					
				},
				immediate: true,
				deep: true
			},		
			candidates:{
				handler($value){
					this.dataList = $value
				},
				deep:true,
				immediate:true
			}
		},
		methods: {
			toggleSelector() {
				this.showSelector = !this.showSelector
			},

			onFocus() {
				if(this.inputValCopy){
					
					this.placeholderCopy = this.inputValCopy
				}else{

					this.placeholderCopy = this.placeholder
				}
				this.showSelector = true
			},
			onBlur() {

				setTimeout(() => {
					this.showSelector = false
					this.isShowInput = false
					this.inputVal = ''
				}, 153)
			},
			onSelectorClick(index) {
				this.showSelector = false
				this.isShowInput = false
				this.$emit('update:myMessage', this.dataList[index][this.valueKey])
				this.$emit('select', this.dataList[index])

			},
			as() {
				this.$emit('input', this.inputVal)
			},
			scrolltolower() {
				this.$emit('srolltolower')
			},
			onChange(e) {
				// this.input = e
				this.dataList = []
				uni.$u.debounce(this.as, 200)
				
				
			}
		}
	}
</script>

<style lang="scss" scoped>
	.wraper-inpu-text{
		display: flex;
		align-items: center;
		// justify-content: center;
		height: 44rpx;
		width: 100%;
		flex: 1;
		& text{
			line-height: 26px;
		}
	}
	.wrper-text-vlaue{
		font-size: 15px;
	}
	.uni-combox {

		font-size: 14px;
		// border: 1px solid #DCDFE6;
		border-radius: 4px;
		// padding: 6px 10px;
		position: relative;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		// height: 40px;
		flex-direction: row;
		align-items: center;
		width: 100% !important;
		// 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: 14px;
		height: 22px;
		line-height: 22px;
	}

	.uni-combox__input-plac {
		font-size: 28rpx;
		text-align: left;
		color: rgb(180, 180, 180);
	}

	.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: 2;
		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;
	}

	.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;
	}

	::v-deep .u-list {
		min-height: 0 !important;
		max-height: 350rpx !important;
		height: auto !important;
	}
</style>

4.使用

(1)引入该组件

(2)父组件写法

5.创作不易,点个赞再走吧,不懂的老铁可以私我

  • 11
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
好的,下面是一个使用 uview-ui 多列选择器组件进行二次封装的示例: ``` <template> <view> <u-popup v-model="showPicker" position="bottom" :overlay="true"> <view> <u-picker-view :value="selectedValues" @change="onPickerChange"> <u-picker-view-column v-for="(column, index) in columns" :key="index"> <view v-for="(item, i) in column" :key="i"> <u-checkbox v-model="item.checked"> {{ item.label }} </u-checkbox> </view> </u-picker-view-column> </u-picker-view> <u-button type="primary" @click="confirm">确定</u-button> </view> </u-popup> <u-input :value="selectedLabels.join(',')" @click="showPicker = true" disabled></u-input> </view> </template> <script> export default { props: { columns: { type: Array, default: () => [] } }, data() { return { showPicker: false, selectedValues: [], selectedLabels: [] } }, mounted() { this.initSelectedValues() }, methods: { initSelectedValues() { this.selectedValues = this.columns.map(column => { return column.findIndex(item => item.checked) }) this.updateSelectedLabels() }, onPickerChange(e) { this.selectedValues = e.detail.value this.updateSelectedLabels() }, updateSelectedLabels() { this.selectedLabels = this.selectedValues.map((value, index) => { return this.columns[index][value].label }) }, confirm() { this.showPicker = false this.$emit('change', this.selectedLabels) } } } </script> ``` 在这个示例中,我们使用了 u-popup、u-picker-view、u-picker-view-column 和 u-checkbox 组件来构建一个多列联动多选选择器。它接收一个 columns 数组作为参数,每个元素代表一列数据,每个元素包含 label 和 checked 两个属性,分别表示选项的文本和是否被选中。通过 u-checkbox 组件来实现多选功能。 组件内部使用了 selectedValues 数组来存储每列选中的值的索引,使用 selectedLabels 数组来存储每列选中的值的文本。在初始化和选择变化时,通过 updateSelectedLabels 方法来更新 selectedLabels 数组。在点击确定按钮时,通过 emit 方法将选中的值传递给父组件。 这是一个简单的示例,你可以根据自己的业务需求进行修改和扩展。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值