当前位置: 首页 > news >正文

uniapp封装picker选择器组件,支持关键字查询

CommonPicker.vue组件

路径在 components\CommonPicker.vue

<template><view><uni-easyinput v-model="searchQuery" :placeholder="placeholder" /><picker :range="filteredOptions" :range-key="'text'" v-model="selectedIndex" @change="onPickerChange"><view class="picker">{{ `当前选择: ${selectedText}` }}</view></picker></view>
</template><script>
export default {props: {value: { // v-model 的值 type: [String, Number],default: ''},options: { // 数据来源 格式为 [{value: '1', text: '选项1'}, {value: '2', text: '选项2'}]type: Array,required: true},placeholder: {type: String,default: '筛选'}},data() {return {selectedIndex: 0,selectedText: '',searchQuery: ''};},computed: {filteredOptions() {if (!this.searchQuery) {return this.options;}return this.options.filter(option => option.text.includes(this.searchQuery));}},watch: {value(val) {const index = this.filteredOptions.findIndex(option => option.value === val);if (index !== -1) {this.selectedIndex = index;this.selectedText = this.filteredOptions[index].text;}},options: {immediate: true,handler() {const index = this.filteredOptions.findIndex(option => option.value === this.value);if (index !== -1) {this.selectedIndex = index;this.selectedText = this.filteredOptions[index].text;}}},searchQuery() {this.updateSelectedText();}},methods: {onPickerChange(e) {const index = e.detail.value;const selectedOption = this.filteredOptions[index];this.selectedIndex = index;this.selectedText = selectedOption.text;this.$emit('input', selectedOption.value); // 触发 v-model 绑定的更新},updateSelectedText() {const index = this.filteredOptions.findIndex(option => option.value === this.value);if (index !== -1) {this.selectedIndex = index;this.selectedText = this.filteredOptions[index].text;} else {this.selectedText = '';this.selectedIndex = 0;}}},mounted() {this.updateSelectedText();}
};
</script><style lang="scss" scoped>
.picker {padding: 10px;background-color: #f0f0f0;border-radius: 5px;text-align: left;margin-top: 10px;
}
</style>

在main.js中全局注册

import CommonPicker from '@/components/CommonPicker.vue';
Vue.component('CommonPicker', CommonPicker)

使用

<template><uni-card><CommonPicker v-model="id" :options="options" :placeholder="`筛选`" /></uni-card>
</template><script>
export default {data() {return {options: [{text: '小明',value: "1"}, {text: '小红',value: "2"}, {text: '小王',value: "3"}],id: undefined,}}
}
</script>

http://www.lryc.cn/news/365425.html

相关文章:

  • 智慧城市的规划与实施:科技引领城市运行效率新飞跃
  • Linux——内存管理代码分析
  • 手机自动化测试:4.通过appium inspector 获取相关app的信息,以某团为例,点击,搜索,获取数据等。
  • 个人项目———密码锁的实现
  • 关于Input【type=number】可以输入e问题及解决方案
  • zabbix“专家坐诊”第241期问答
  • 了解Kubernetes-RKE2的PKI以及证书存放位置
  • 利用大语言模型进行事实匹配
  • 【Stable Diffusion】(基础篇一)—— Stable Diffusion的安装
  • 维纳运动的概念
  • 毫秒级查询性能优化实践!Apache Doris 在极越汽车数字化运营和营销方向的解决方案
  • vllm 大模型量化微调推理使用: lora、gptq、awq
  • WPS/Office(Word、Excel、PPT) 自动测评方法
  • ArrayList——简单洗牌算法
  • springboot vue 开源 会员收银系统 (6) 收银台的搭建
  • 重排和重绘的区别,什么情况下会触发这两种情况
  • 亮点回顾|智能汽车芯片创新技术应用与质量研讨会
  • 特征工程,减小过拟合
  • STM32-16-ADC
  • 单例模式(C语言)
  • js前端格式化日期函数
  • 五个超实用的 ChatGPT-4o 提示词
  • 基于51单片机多功能防盗报警proteus仿真( proteus仿真+程序+设计报告+原理图+讲解视频)
  • gitee和github的协同
  • 压力测试-性能指标-Jmeter使用-压力测试报告
  • 通过Slf4j中的MDC实现在日志中添加用户IP功能
  • 代码随想录算法训练营第四十九天| 139.单词拆分、背包问题总结
  • STM32F103VE和STM32F407VE的引脚布局
  • 搜维尔科技:使用 Xsens 动作捕捉技术创建栩栩如生的动画
  • 鸿蒙开发 一 (三)、ArkTS开发实战上