Uniapp: 下拉选择框 ba-tree-picker

### UniApp 下拉选择框实现单选和多选功能 #### 使用 `picker` 组件实现下拉选择框的单选功能 在 UniApp 中,可以通过内置的 `picker` 组件来轻松实现下拉选择框的功能。对于单选模式,配置相对简单。 ```html <template> <view> <!-- 单选 --> <picker mode="selector" :range="array" @change="bindPickerChange"> <view class="picker"> 当前选择:{{ array[index] }} </view> </picker> </view> </template> <script> export default { data() { return { array: ['选项1', '选项2', '选项3'], index: 0, }; }, methods: { bindPickerChange(e) { this.index = e.detail.value; } } }; </script> ``` 此代码展示了如何通过 `picker` 组件设置一个简单的单选下拉菜单[^1]。 #### 实现带有多选功能的选择器 为了支持多选操作,在 UniApp 中通常会采用更复杂的逻辑处理方式。下面是一个带有底部弹出样式并允许用户进行多选的例子: ```html <template> <view> <!-- 显示已选中的项 --> <view v-for="(item, idx) in selectedItems" :key="idx">{{ item }}</view> <!-- 打开多选对话框按钮 --> <button type="default" @click="showMultiSelector">打开多选</button> <!-- 底部弹出层用于展示所有可选项 --> <uni-popup ref="popup" type="bottom"> <scroll-view scroll-y style="height: 300px;"> <checkbox-group @change="multiSelectChange"> <label v-for="(option, i) in options" :key="i"> <checkbox :value="option" :checked="selectedItems.includes(option)" /> {{ option }} </label> </checkbox-group> </scroll-view> <button @click="confirmSelection">确认</button> </uni-popup> </view> </template> <script> import uniPopup from '@/components/uni-popup/uni-popup.vue'; export default { components: { uniPopup }, data() { return { options: ['苹果', '香蕉', '橙子'], // 可供选择的数据列表 selectedItems: [], // 用户已经选择了哪些项目 }; }, methods: { showMultiSelector() { this.$refs.popup.open(); }, multiSelectChange(event) { const values = event.detail.value; this.selectedItems = [...values]; }, confirmSelection() { console.log('最终选定:', this.selectedItems); this.$refs.popup.close(); } } }; </script> ``` 这段代码实现了当用户点击“打开多选”按钮时,从页面下方滑动出来的窗口中可以选择多个条目,并且这些被选中的条目会在界面上实时更新显示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Monly21

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值