欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序下拉框組件使用方法

 更新時(shí)間:2022年07月11日 11:49:11   作者:qq_36437172  
這篇文章主要為大家詳細(xì)介紹了微信小程序下拉框組件使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

小程序有時(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)文章

  • ECMAScript6中Map映射的基本概念與常用方法

    ECMAScript6中Map映射的基本概念與常用方法

    Map是ES6中新增的數(shù)據(jù)結(jié)構(gòu),Map類似于對象,但普通對象的 key 必須是字符串或者數(shù)字,而 Map 的 key 可以是任何數(shù)據(jù)類型,這篇文章主要給大家介紹了關(guān)于ECMAScript6中Map映射的基本概念與常用方法,需要的朋友可以參考下
    2021-07-07
  • 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)容示例

    這篇文章主要介紹了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ù)中的坑

    這篇文章主要介紹了詳解關(guān)于微信setData回調(diào)函數(shù)中的坑,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-02-02
  • 詳解javascript數(shù)組去重問題

    詳解javascript數(shù)組去重問題

    這篇文章主要介紹了詳解javascript數(shù)組去重問題,根據(jù)面試時(shí)做的一道數(shù)組去重問題的解題思路整理的,分享給大家。
    2015-11-11
  • 基于aotu.js實(shí)現(xiàn)微信自動(dòng)添加通訊錄中的聯(lián)系人功能

    基于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-05
  • javascript實(shí)現(xiàn)的登陸遮罩效果匯總

    javascript實(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
  • setTimeout函數(shù)的神奇使用

    setTimeout函數(shù)的神奇使用

    setTimeout函數(shù)是一個(gè)原生的javascript函數(shù)。setTimeout函數(shù)會在一個(gè)指定的延遲時(shí)間之后調(diào)用一個(gè)函數(shù)或執(zhí)行一段指定的代碼。它的應(yīng)用非常廣泛,例如我們希望用戶在瀏覽器某個(gè)頁面一段時(shí)間后彈出一個(gè)對話框,或者是鼠標(biāo)點(diǎn)擊某個(gè)元素后隔幾秒鐘在刪除這個(gè)元素。
    2017-02-02
  • 深入淺析js原型鏈和vue構(gòu)造函數(shù)

    深入淺析js原型鏈和vue構(gòu)造函數(shù)

    這篇文章主要介紹了js原型鏈和vue構(gòu)造函數(shù)的相關(guān)知識,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2018-10-10
  • js創(chuàng)建對象的方式總結(jié)

    js創(chuàng)建對象的方式總結(jié)

    這篇文章主要介紹了js創(chuàng)建對象的方式,實(shí)例總結(jié)了3種常用的創(chuàng)建對象的方式,非常實(shí)用,需要的朋友可以參考下
    2015-01-01
  • 原生js仿jq判斷當(dāng)前瀏覽器是否為ie,精確到ie6~8

    原生js仿jq判斷當(dāng)前瀏覽器是否為ie,精確到ie6~8

    這篇文章主要介紹了原生js仿jq判斷當(dāng)前瀏覽器是否為ie,精確到ie6~8,需要的朋友可以參考下
    2014-08-08

最新評論