在uniapp中实现下拉筛选菜单功能组件,可以使用picker
组件配合多个radio-group
和checkbox-group
来实现。以下是一个简单的示例:
<template>
<view class="container">
<view class="picker-view">
<picker mode="selector" :range="pickerRange" @change="onPickerChange">
{{pickerValue}}
</picker>
</view>
<view class="radio-group-view">
<radio-group v-for="(item, index) in radioGroups" :key="index" @change="onRadioChange(index, ...arguments)">
<label v-for="(radio, radioIndex) in item" :key="radioIndex">
<radio :value="radioIndex" :checked="radio.checked">{{radio.name}}</radio>
</label>
</radio-group>
</view>
<view class="checkbox-group-view">
<checkbox-group v-for="(item, index) in checkboxGroups" :key="index" @change="onCheckboxChange(index, ...arguments)">
<label v-for="(checkbox, checkboxIndex) in item" :key="checkboxIndex">
<checkbox :value="checkboxIndex" :checked="checkbox.checked">{{checkbox.name}}</checkbox>
</label>
</checkbox-group>
</view>
</view>
</template>
<script>
export default {
data() {
return {
pickerRange: ['筛选1', '筛选2', '筛选3'],
pickerValue: '请选择',
radioGroups: [
[
{ name: '选项1', checked: false },
{ name: '选项2', checked: false }
],
// ... 可以添加更多的radio组
],
checkboxGroups: [
[
{ name: '选项A', checked: false },
{ name: '选项B', checked: false }
],
// ... 可以添加更多的checkbox组
]
};
},
methods: {
onPickerChange(e) {
this.pickerValue = this.pickerRange[e.detail.value];
},
onRadioChange(groupIndex, e) {
this.radioGroups[groupIndex].forEach((radio, index) => {
radio.checked = index === e.detail.value;
});
},
onCheckboxChange(groupIndex, e) {
this.checkboxGroups[groupIndex].forEach((checkbox, index) => {
checkbox.checked = e.detail.value.includes(index);
});
}
}
};
</script>
<style>
.container {
display: flex;
flex-direction: column;
align-items: flex-start;
}
.picker-view, .radio-group-view, .checkbox-group-view {
margin-bottom: 10px;
}
</style>
这个组件包含一个picker
组件用于展示顶部的下拉菜单,下面是多个radio-group
和checkbox-group
用于展示筛选条件。每当用户更改选择,对应的数据将会更新,以反映用户的选择。这个简单的例子可以扩展为包括更复杂的