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>

相关推荐
Zheng113几秒前
【可视化大屏】将柱状图引入到html页面中
javascript·ajax·html
程序猿小D7 分钟前
第二百六十七节 JPA教程 - JPA查询AND条件示例
java·开发语言·前端·数据库·windows·python·jpa
john_hjy25 分钟前
【无标题】
javascript
奔跑吧邓邓子39 分钟前
npm包管理深度探索:从基础到进阶全面教程!
前端·npm·node.js
软件开发技术深度爱好者1 小时前
用HTML5+CSS+JavaScript庆祝国庆
javascript·css·html5
前端李易安1 小时前
ajax的原理,使用场景以及如何实现
前端·ajax·okhttp
汪子熙1 小时前
Angular 服务器端应用 ng-state tag 的作用介绍
前端·javascript·angular.js
Envyᥫᩣ2 小时前
《ASP.NET Web Forms 实现视频点赞功能的完整示例》
前端·asp.net·音视频·视频点赞
Мартин.6 小时前
[Meachines] [Easy] Sea WonderCMS-XSS-RCE+System Monitor 命令注入
前端·xss
昨天;明天。今天。7 小时前
案例-表白墙简单实现
前端·javascript·css