uni-app 封装下拉选择组件 标红指定项

javascript 复制代码
<template>
	<view class="uni-dropdown">
		<view class="trigger" @tap="toggleDropdown">{{ showlabel }}</view>
		<view class="dropdown-content" v-if="isOpen">
			<view class="" style="position: relative;">
				<text class="blk blk-a-xialakuang-xiala"></text>
			</view>
			<view style="max-height: 150px;overflow-y: auto;">
				<view v-for="(item, index) in options" :key="index" :class="['dropdown-item', item.isred ? 'red' : '']"
					@tap="selectOption(item)">
					{{ item[labelkey] }}
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		watch,
	} from 'vue';

	const props = defineProps({
		options: {
			type: Array,
			default: () => []
		},
		modelValue: {
			type: [String, Number],
			default: ''
		},
		valuekey: {
			type: String,
			default: 'id'
		},
		labelkey: {
			type: String,
			default: 'name'
		},
		isredkey: {
			type: String,
			default: 'isred'
		}
	});
	let emit = defineEmits(['update:modelValue', 'change'])

	const isOpen = ref(false);
	const showlabel = ref('请选择'); // 初始化显示文本  

	const toggleDropdown = () => {
		isOpen.value = !isOpen.value;
	};

	const selectOption = (item) => {
		emit('update:modelValue', item[props.valuekey]); // 发出更新事件  
		emit('change', item[props.valuekey])
		isOpen.value = false; // 关闭下拉  
		// 同时更新显示的标签(如果需要)  
		showlabel.value = item[props.labelkey];
	};

	watch(() => props.modelValue, (newVal) => {
		const selectedOption = props.options.find(opt => opt[props.valuekey] === newVal);
		if (selectedOption) {
			// 注意这里应该是selectedOption而不是selectOption  
			showlabel.value = selectedOption[props.labelkey];
		} else {
			// 如果找不到匹配的选项,可以重置为默认文本或其他逻辑  
			showlabel.value = '请选择';
		}
	},{
		immediate:true
	});
</script>

<style scoped>
	.uni-dropdown {
		position: relative;
	}

	.trigger {
		padding: 5rpx;
		border: 1px solid #ccc;
		cursor: pointer;
		overflow-x: scroll;
		width: 100%;
		border-radius: 4px;
	}

	.dropdown-content {
		box-sizing: border-box;
		position: absolute;
		top: calc(100% + 5px);
		left: 0;
		width: 100%;
		background-color: #fff;
		border: 1px solid #ebeef5;
		border-radius: 6px;
		box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
		z-index: 3;
		padding: 4px 0;
		font-size: 20rpx;
	}

	.blk-a-xialakuang-xiala {
		position: absolute;
		left: 20%;
		top: -14px;
		color: #fff;
		border-top-width: 0;
		border-bottom-color: #ebeef5;
	}

	.dropdown-item {
		padding: 5rpx;
		cursor: pointer;
	}

	.dropdown-item.red {
		color: red;
	}

	.dropdown-item:hover {
		background-color: #f0f0f0;
	}
</style>

使用方式

html 复制代码
<selectBom v-if="!isOnlyRead&&supplies_detail_item.type_biaoshi==3&&item?.bom_list?.length"
								v-model="item.bom_versions" :options="item.bom_list" labelkey='bom_versions'
								valuekey='bom_versions'
								@change="handle_change_boom($event,item,index,idx,item.bom_list)"></selectBom>
相关推荐
余生H2 分钟前
前端大模型入门:实战篇之Vue3+Antdv+transformers+本地模型实现增强搜索
前端·javascript·vue.js·深度学习·transformer·深度搜索·webml
wei_shuo23 分钟前
上下位关系自动检测方法(论文复现)
前端·javascript·easyui
等你许久_孟然1 小时前
【CSS/HTML】左侧固定,右侧自适应的布局方式理解margin负值理论
前端·css·html·页面布局·margin负值理论
逆旅行天涯1 小时前
基于Vue3内置的lodash函数库实现防抖节流
前端·javascript·vue.js
williamdsy1 小时前
【chrome 插件】初窥
前端·javascript·chrome·插件
你会发光哎u2 小时前
学习Webpack中图片-JS-Vue-plugin
javascript·学习·webpack
谢尔登2 小时前
使用 npkill 快速清理本地 node_modules 文件
javascript·node.js
ededabo2 小时前
正则表达式的使用规则
开发语言·前端·爬虫·python·正则表达式
我是一只鱼啊2 小时前
前端 OnePiece CSS (娜美)篇
前端·css
南風知意2 小时前
🏅el-tooltip 组件在全屏状态下不显示提示框问题
前端·css