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>
相关推荐
子兮曰2 天前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
子兮曰2 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
前端小万2 天前
写公众号赚了 3000 块后,我做了一款叫 "一键成稿" 的软件
前端·微信小程序
爱勇宝2 天前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
三十而立洋2 天前
Cookie 详解:从产生到安全,一次讲透
前端·javascript
卡布鲁2 天前
把一个 Vite + Vue3 应用塞进 qiankun (React + Umi3) 主站:十个坑的复盘
前端·javascript·react.js
李少兄2 天前
JavaScript 隐式全局变量解析
javascript
汉堡大王95272 天前
Jev:不是聊天机器人, 而是一个智能 if 语句
前端·人工智能·后端
梦想很大很大2 天前
从运行事实到回归证据:Workrun 的 Telemetry 与 Evaluation 实践
前端·人工智能·后端
计算机魔术师2 天前
Meta Muse agent 接入 Shopify 的 Shop Pay 实现代理式购物
前端