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

CommonPicker.vue组件

路径在 components\CommonPicker.vue

javascript 复制代码
<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中全局注册

javascript 复制代码
import CommonPicker from '@/components/CommonPicker.vue';
Vue.component('CommonPicker', CommonPicker)

使用

javascript 复制代码
<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>

相关推荐
zczlsy1132 分钟前
webpack介绍
前端·webpack·node.js
六个点34 分钟前
关于vue的面试考点总结🤯
前端·vue.js·面试
浪遏1 小时前
今晚揭开单例模式的面纱~
前端·设计模式·面试
驯龙高手_追风2 小时前
谷歌Chrome或微软Edge浏览器修改网页任意内容
前端·chrome·edge
luckyext2 小时前
Postman发送GET请求示例及注意事项
前端·后端·物联网·测试工具·小程序·c#·postman
dorabighead2 小时前
重构版:JavaScript 的 new 操作符——从“黑箱仪式”到“亲手造物”的认知跃迁
开发语言·javascript·重构
小满zs2 小时前
React第三十章(css原子化)
前端·react.js
一直在学习的小白~3 小时前
前端项目中创建自动化部署脚本,用于 Jenkins 触发 npm run publish 来完成远程部署
前端·自动化·jenkins
Perfect—完美3 小时前
Vue 3 事件总线详解:构建组件间高效通信的桥梁
前端·javascript·vue.js
wtrees_松阳3 小时前
【编程向导】-JavaScript-基础语法-类型检测
开发语言·javascript·原型模式