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>

相关推荐

  1. uniapp -- picker民族选择

    2024-06-07 06:54:05       52 阅读
  2. uView Picker 选择

    2024-06-07 06:54:05       67 阅读
  3. 基于uniapp 组件uniform 得自定义picker 选择

    2024-06-07 06:54:05       48 阅读
  4. uniapp picker-view 搜索选择

    2024-06-07 06:54:05       25 阅读
  5. uniapp时间选择

    2024-06-07 06:54:05       70 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-07 06:54:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-07 06:54:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-07 06:54:05       87 阅读
  4. Python语言-面向对象

    2024-06-07 06:54:05       96 阅读

热门阅读

  1. 基于uni-app的 年-月-日 时 时间日期范围控件

    2024-06-07 06:54:05       36 阅读
  2. 个人开发者能用TensorFlow做什么

    2024-06-07 06:54:05       33 阅读
  3. Hutool工具网络文件下载与文件压缩

    2024-06-07 06:54:05       33 阅读
  4. MVC前端怎么写:深入解析与实战指南

    2024-06-07 06:54:05       26 阅读
  5. 【SCSS】use的详细使用规则

    2024-06-07 06:54:05       32 阅读
  6. AR编程入门:解锁虚拟与现实交融的新世界

    2024-06-07 06:54:05       35 阅读
  7. spring boot 之 整合 knife4j 在线接口文档

    2024-06-07 06:54:05       25 阅读
  8. Sass混合宏(Mixins)使用

    2024-06-07 06:54:05       32 阅读