微信小程序下拉框組件使用方法
小程序有時需要使用下拉框選項,通常我會使用 picker 組件實現(xiàn)。pick 組件使用 mode 來區(qū)分類別,默認使用普通選擇器就行。
除了上述方式,我們也可以通過自定義組件實現(xiàn),代碼如下:
//index.js
Component({
? /**
? ?* 組件的屬性列表
? ?*/
? properties: {
? ? propArray: {
? ? ? type: Array,
? ? }
? },
? /**
? ?* 組件的初始數(shù)據(jù)
? ?*/
? data: {
? ? selectShow: false,//初始option不顯示
? ? selectText: "請選擇",//初始內(nèi)容
? },
? /**
? ?* 組件的方法列表
? ?*/
? methods: {
? ? //option的顯示與否
? ? selectToggle: function () {
? ? ? var nowShow = this.data.selectShow;//獲取當前option顯示的狀態(tài)
?
? ? ? this.setData({
? ? ? ? selectShow: !nowShow
? ? ? })
? ? },
? ? //設(shè)置內(nèi)容
? ? setText: function (e) {
? ? ? var nowData = this.properties.propArray;//當前option的數(shù)據(jù)是引入組件的頁面?zhèn)鬟^來的,所以這里獲取數(shù)據(jù)只有通過this.properties
? ? ? var nowIdx = e.target.dataset.index;//當前點擊的索引
? ? ? var nowText = nowData[nowIdx].text || nowData[nowIdx].value || nowData[nowIdx];//當前點擊的內(nèi)容
? ? ? //再次執(zhí)行動畫,注意這里一定,一定,一定是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>樣式實現(xiàn):
/* 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>同時傳入數(shù)據(jù)和監(jiān)聽子組件向父組件傳遞的 select 方法。
Page({
? data: {
? ? selectArray: [{
? ? ? "id": "10",
? ? ? "value": "會計類"
? ? }, {
? ? ? "id": "21",
? ? ? "text": "工程類"
? ? }, '技術(shù)類', {'value': '其他'}]
? },
? select: function(e) {
? ? console.log(e.detail)
? }
})以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS動態(tài)增加刪除UL節(jié)點LI及相關(guān)內(nèi)容示例
這篇文章主要介紹了JS如何動態(tài)增加刪除UL節(jié)點LI及相關(guān)內(nèi)容,需要的朋友可以參考下2014-05-05
詳解關(guān)于微信setData回調(diào)函數(shù)中的坑
這篇文章主要介紹了詳解關(guān)于微信setData回調(diào)函數(shù)中的坑,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02
基于aotu.js實現(xiàn)微信自動添加通訊錄中的聯(lián)系人功能
這篇文章主要介紹了利用aotu.js實現(xiàn)微信自動添加通訊錄中的聯(lián)系人,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05

