在前端Web開發(fā)中鳖悠,下拉篩選功能是一種非常常見的交互方式懦砂,它可以幫助用戶快速選擇所需的選項娜氏。本文將介紹如何利用Vue.js和uni-app框架來實現(xiàn)一個高效的下拉篩選功能贵白。通過使用這兩個強大的前端框架,我們可以輕松地創(chuàng)建具有響應式用戶操作的下拉篩選組件倍谜。
## 1. 項目設置
首先迈螟,我們需要創(chuàng)建一個新的Vue.js項目,并引入uni-app的相關組件和API尔崔。這樣可以方便地將我們的代碼集成到uni-app應用中答毫。在項目中,我們可以使用Vuex來管理數(shù)據狀態(tài)季春,以及使用Vue Router來處理頁面路由洗搂。
## 2. 數(shù)據準備
在模板中,我們需要定義一些用于篩選的數(shù)據和默認的選擇序列。這些數(shù)據可以通過v-model指令來實現(xiàn)雙向綁定蚕脏。同時侦副,在methods中,我們需要定義一個方法來處理用戶的選擇驼鞭。當用戶選擇一個新的篩選項時秦驯,這個方法會被調用。在這個方法中挣棕,我們首先獲取用戶選擇的數(shù)據译隘,然后更新res變量的值。最后洛心,我們使用uni-app的showModal方法來顯示一個模態(tài)框固耘,告訴用戶他們選擇了哪些數(shù)據。附源碼下載地址:https://ext.dcloud.net.cn/plugin?id=12421
效果圖如下:
# 下拉框使用方法
#### HTML代碼部分
```html
<template>
<view class="content">
<view style="margin-top: 16px;">
<view style="width: 100vw; height: 40px; background-color: white;">
<!-- filterData:篩選數(shù)據? defaultIndex: 默認選擇序列 @onSelected:選擇事件 返回選擇的值-->
<chenchuang-CCDropDownFilter :filterData='filterData' :defaultIndex='defaultIndex'
@onSelected='onSelected'></chenchuang-CCDropDownFilter>
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
filterData: [
[{
name: '全省',
value: ''
}],
[{
name: '美食',
value: ''
},
{
name: '湘菜',
value: '1'
},
{
name: '川菜',
value: '2'
},
{
name: '火鍋',
value: '3'
}
],
[{
name: '排序',
value: ''
},
{
name: '好評優(yōu)先',
value: '1'
},
{
name: '銷量優(yōu)先',
value: '2'
},
{
name: '低價優(yōu)先',
value: '3'
}
],
[{
name: '篩選',
value: ''
},
{
name: '篩選1',
value: '1'
},
{
name: '篩選2',
value: '2'
}
],
],
defaultIndex: [0, 0, 0, 0]
}
},
mounted() {
let cityArr = ['廣州市', '深圳市', '佛山市', '東莞市', '中山市', '珠海市', '江門市', '肇慶市', '惠州市', '汕頭市', '潮州市', '揭陽市', '汕尾市',
'湛江市', '茂名市', '陽江市', '云浮市', '韶關市', '清遠市', '梅州市', '河源市'
]
for (let s of cityArr) {
this.filterData[0].push({
name: s,
value: s
});
}
},
methods: {
onSelected(res) {
console.log('選擇res = ' + JSON.stringify(res));
uni.showModal({
title: '下拉篩選選擇數(shù)據',
content: '所選擇數(shù)據 = ' + JSON.stringify(res)
})
},
}
}
</script>
<style scoped>
page {
background-color: '#F6F7FA';
}
.content {
display: flex;
flex-direction: column;
}
.mui-content-padded {
margin: 6px 14px;
}
.lineV {
margin-top: 0px;
margin-left: 15px;
width: calc(100vw - 30px);
height: 1px;
background-color: #F5F5F5;
}
.hotSearchTitV {
margin-left: 14px;
margin-top: 4px;
width: 170px;
height: 22px;
font-size: 14px;
font-family: PingFangSC-Medium, PingFang SC;
font-weight: 500;
color: #161616;
line-height: 22px;
}
.upView {
display: flex;
flex-direction: row;
height: 26px;
margin-left: 0px;
}
.cellView {
margin-top: 4px;
margin-left: 5.8px;
height: 18px;
line-height: 18px;
text-align: center;
border-radius: 2px;
padding: 0px 4px !important;
font-size: 10px;
background-color: #f5f5f5;
color: #818183;
}
.moreBtn {
height: 30px;
text-align: center;
font-size: 12px;
line-height: 30px;
color: #888888;
}
</style>
```