uv-drop-down 下拉筛选 vue3写法

<template>
    <view>
        <uv-drop-down ref="dropDown" sign="dropDown_1" text-active-color="#3c9cff"
                        :extra-icon="{name:'arrow-down-fill',color:'#666',size:'26rpx'}"
                        :extra-active-icon="{name:'arrow-up-fill',color:'#3c9cff',size:'26rpx'}"
                        :defaultValue="defaultValue" :custom-style="{padding: '0 30rpx'}"                         @click="selectMenu">
              <uv-drop-down-item name="order" type="2" :label="dropdownMenuData.order.label" :value="dropdownMenuData.order.value">
                        </uv-drop-down-item>
                        <uv-drop-down-item name="type" type="2" :label="dropdownMenuData.type.label" :value="dropdownMenuData.type.value">
                        </uv-drop-down-item>
                    </uv-drop-down>
                    <uv-drop-down-popup sign="dropDown_1" :click-overlay-on-close="true"
                        :currentDropItem="currentDropItem" @clickItem="clickItem"></uv-drop-down-popup>
    </view>
</template>
<script setup lang="ts">
    const activeName = ref('order')
    // 表示value等于这些值,就属于默认值
    const defaultValue = [0, 'all', '0']
    const dropdownMenuData = reactive({
        order: {
            label: '综合排序',
            value: 'all',
            activeIndex: 0,
            color: '#333',
            activeColor: '#2878ff',
            child: [{
                label: '综合排序',
                value: 'all'
            }, {
                label: '最新发布',
                value: 'new'
            }, {
                label: '低价优先',
                value: 'money'
            }]
        },
        type: {
            label: '文档格式',
            value: 'all',
            activeIndex: 0,
            color: '#333',
            activeColor: '#2878ff',
            child: [{
                label: '全部',
                value: 'all'
            }, {
                label: 'PDF',
                value: 'pdf'
            }, {
                label: 'WROD',
                value: 'word'
            }, {
                label: 'PPT',
                value: 'ppt'
            }]
        }
    })
    // 获取当前下拉筛选项
    const currentDropItem = computed(() => {
        // @ts-ignore
        return dropdownMenuData[activeName.value];
    })
    /**
     * 点击每个筛选项回调
     * @param {Object} e { name, active, type } = e
     */
    function selectMenu(e) {
        const { name } = e;
        activeName.value = name;
    }
    /**
     * 点击菜单回调处理
     * @param {Object} e 选中项 { label,value } = e
     */
    function clickItem(e) {
        // 下面有重新赋值,所以用let
        const { label, value } = e;
        console.log(value);
        if (defaultValue.indexOf(value) == -1) {
            // 不是默认值 替换label
            // @ts-ignore
            dropdownMenuData[activeName.value].label = label
            // @ts-ignore
            dropdownMenuData[activeName.value].value = value
        }else{
            // @ts-ignore
            dropdownMenuData[activeName.value].label = activeName.value == 'order' ? '综合排序' : '文档格式'
            // @ts-ignore
            dropdownMenuData[activeName.value].value = 'all'
        }
        // TODO 接口获取数据逻辑
    }
</script>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js 3中实现自动完成下拉列表的方法很简单,可以使用Vue.js提供的指令和组件的功能来实现。 首先,你需要引入ElementUI库,它是一个基于Vue.js的组件库,提供了非常好用的组件和指令。其中el-autocomplete是用来实现输入框的自动完成下拉列表的组件。 接下来,你需要在Vue.js的template(模板)中使用el-autocomplete组件,最基本的使用方法如下: ```html <template> <el-autocomplete v-model="value" :fetch-suggestions="querySearch" placeholder="请输入" /> </template> ``` 在这里,v-model用来绑定输入框的值,placeholder用来设置输入框的提示语,:fetch-suggestions用来绑定一个函数,该函数会在输入框输入时被调用,它的返回值是一个数组,用来显示下拉列表中的选项。 接下来,你需要在Vue.js的script(脚本)中实现querySearch函数,该函数需要返回一个Promise对象,用来异步获取输入框输入后的联想词,例如: ```javascript <script> import { ref } from 'vue'; import { fetchSuggestions } from '@/api/suggestion.js'; export default { setup() { const value = ref(''); const querySearch = async (queryString) => { return await fetchSuggestions(queryString); }; return { value, querySearch, }; }, }; </script> ``` 在这里,我们用了Vue.js 3中新增的setup函数,在该函数中,我们使用了ref定义了value变量,并定义了querySearch函数,该函数使用async/await实现异步调用了一个api/suggestion.js的函数fetchSuggestions,该函数的接口可以根据自己的实际需求进行实现。 最后,你需要在Vue.js的style(样式)中引入ElementUI的样式,以确保el-autocomplete组件正常显示: ```css @import "element-plus/packages/theme-chalk/src/index.scss"; ``` 综上所述,使用Vue.js 3和ElementUI库实现输入框的自动完成下拉列表功能就这么简单,你只需要引入库、定义组件和指令、实现函数即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值