微信小程序下拉框組件使用方法
小程序有時(shí)需要使用下拉框選項(xiàng),通常我會使用 picker 組件實(shí)現(xiàn)。pick 組件使用 mode 來區(qū)分類別,默認(rèn)使用普通選擇器就行。
除了上述方式,我們也可以通過自定義組件實(shí)現(xiàn),代碼如下:
//index.js Component({ ? /** ? ?* 組件的屬性列表 ? ?*/ ? properties: { ? ? propArray: { ? ? ? type: Array, ? ? } ? }, ? /** ? ?* 組件的初始數(shù)據(jù) ? ?*/ ? data: { ? ? selectShow: false,//初始o(jì)ption不顯示 ? ? selectText: "請選擇",//初始內(nèi)容 ? }, ? /** ? ?* 組件的方法列表 ? ?*/ ? methods: { ? ? //option的顯示與否 ? ? selectToggle: function () { ? ? ? var nowShow = this.data.selectShow;//獲取當(dāng)前option顯示的狀態(tài) ? ? ? ? this.setData({ ? ? ? ? selectShow: !nowShow ? ? ? }) ? ? }, ? ? //設(shè)置內(nèi)容 ? ? setText: function (e) { ? ? ? var nowData = this.properties.propArray;//當(dāng)前option的數(shù)據(jù)是引入組件的頁面?zhèn)鬟^來的,所以這里獲取數(shù)據(jù)只有通過this.properties ? ? ? var nowIdx = e.target.dataset.index;//當(dāng)前點(diǎn)擊的索引 ? ? ? var nowText = nowData[nowIdx].text || nowData[nowIdx].value || nowData[nowIdx];//當(dāng)前點(diǎn)擊的內(nèi)容 ? ? ? //再次執(zhí)行動(dòng)畫,注意這里一定,一定,一定是this.animation來使用動(dòng)畫 ? ? ? 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>
樣式實(shí)現(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í)傳入數(shù)據(jù)和監(jiān)聽子組件向父組件傳遞的 select 方法。
Page({ ? data: { ? ? selectArray: [{ ? ? ? "id": "10", ? ? ? "value": "會計(jì)類" ? ? }, { ? ? ? "id": "21", ? ? ? "text": "工程類" ? ? }, '技術(shù)類', {'value': '其他'}] ? }, ? select: function(e) { ? ? console.log(e.detail) ? } })
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS動(dòng)態(tài)增加刪除UL節(jié)點(diǎn)LI及相關(guān)內(nèi)容示例
這篇文章主要介紹了JS如何動(dòng)態(tài)增加刪除UL節(jié)點(diǎn)LI及相關(guān)內(nèi)容,需要的朋友可以參考下2014-05-05詳解關(guān)于微信setData回調(diào)函數(shù)中的坑
這篇文章主要介紹了詳解關(guān)于微信setData回調(diào)函數(shù)中的坑,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02基于aotu.js實(shí)現(xiàn)微信自動(dòng)添加通訊錄中的聯(lián)系人功能
這篇文章主要介紹了利用aotu.js實(shí)現(xiàn)微信自動(dòng)添加通訊錄中的聯(lián)系人,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05javascript實(shí)現(xiàn)的登陸遮罩效果匯總
小編給大家推薦幾款使用Javascript實(shí)現(xiàn)的遮罩效果登陸框,其實(shí)這種效果是很常見的,在許多互動(dòng)的社區(qū)及其它的一些地方,彈出框應(yīng)用想當(dāng)流行,在不妨礙網(wǎng)頁運(yùn)行的情況下,用戶可以輸入登錄信息,實(shí)現(xiàn)完美登錄。2015-11-11原生js仿jq判斷當(dāng)前瀏覽器是否為ie,精確到ie6~8
這篇文章主要介紹了原生js仿jq判斷當(dāng)前瀏覽器是否為ie,精確到ie6~8,需要的朋友可以參考下2014-08-08