今天小編給大家分享一下微信小程序下拉框組件如何使用的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
小程序有時需要使用下拉框選項,通常我會使用 picker 組件實現。pick 組件使用 mode 來區分類別,默認使用普通選擇器就行。
除了上述方式,我們也可以通過自定義組件實現,代碼如下:
//index.js
Component({
/**
* 組件的屬性列表
*/
properties: {
propArray: {
type: Array,
}
},
/**
* 組件的初始數據
*/
data: {
selectShow: false,//初始option不顯示
selectText: "請選擇",//初始內容
},
/**
* 組件的方法列表
*/
methods: {
//option的顯示與否
selectToggle: function () {
var nowShow = this.data.selectShow;//獲取當前option顯示的狀態
this.setData({
selectShow: !nowShow
})
},
//設置內容
setText: function (e) {
var nowData = this.properties.propArray;//當前option的數據是引入組件的頁面傳過來的,所以這里獲取數據只有通過this.properties
var nowIdx = e.target.dataset.index;//當前點擊的索引
var nowText = nowData[nowIdx].text || nowData[nowIdx].value || nowData[nowIdx];//當前點擊的內容
//再次執行動畫,注意這里一定,一定,一定是this.animation來使用動畫
this.setData({
selectShow: false,
selectText: nowText,
})
this.triggerEvent('select', nowData[nowIdx])
}
}
})代碼如下:
<view class='ms-content-box'>
<view class='ms-content' bindtap='selectToggle'>
<view class='ms-text'>{{selectText}}</view>
<view class="{{selectShow ? 'icon-up' : 'icon-down'}}"></view>
</view>
<view class='ms-options' wx:if="{{selectShow}}">
<view wx:for="{{propArray}}" data-index="{{index}}" wx:key='index' class='ms-option' bindtap='setText'>{{item.text || item.value || item}}</view>
</view>
</view>樣式實現:
/* components/single-dropdown-select/index.wxss */
.ms-content-box {
width: 120px;
}
.ms-content {
border: 1px solid #e2e2e2;
background: white;
font-size: 16px;
position: relative;
height: 30px;
line-height: 30px;
}
.ms-text {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
padding: 0 40px 0 6px;
font-size: 14px;
}
.ms-options {
background: white;
width: inherit;
position: absolute;
border: 1px solid #e2e2e2;
border-top: none;
box-sizing: border-box;
z-index: 3;
max-height: 120px;
overflow: auto;
}
.ms-option {
height: 30px;
line-height: 30px;
border-top: 1px solid #e2e2e2;
padding: 0 6px;
text-align: left;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
font-size: 14px;
}
.ms-item:first-child {
border-top: none;
}
.icon-right, .icon-down, .icon-up {
display: inline-block;
padding-right: 13rpx;
position: absolute;
right: 20rpx;
top: 10rpx;
}
.icon-right::after, .icon-down::after, .icon-up::after {
content: "";
display: inline-block;
position: relative;
bottom: 2rpx;
margin-left: 10rpx;
height: 10px;
width: 10px;
border: solid #bbb;
border-width: 2px 2px 0 0;
}
.icon-right::after {
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
}
.icon-down::after {
bottom: 14rpx;
-webkit-transform: rotate(135deg);
transform: rotate(135deg);
}
.icon-up::after {
bottom: 0rpx;
-webkit-transform: rotate(-45deg);
transform: rotate(-45deg);
}如何使用呢?首先在引用組件的頁面,引入組件:
{
"usingComponents": {
"single-dropdown-select": "/components/single-dropdown-select/index"
}
}在頁面中,直接使用 引入的組件,代碼如下:
<view>
<single-dropdown-select prop-array='{{selectArray}}' bind:select='select' />
</view>同時傳入數據和監聽子組件向父組件傳遞的 select 方法。
Page({
data: {
selectArray: [{
"id": "10",
"value": "會計類"
}, {
"id": "21",
"text": "工程類"
}, '技術類', {'value': '其他'}]
},
select: function(e) {
console.log(e.detail)
}
})以上就是“微信小程序下拉框組件如何使用”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。