小程序下拉选框

下拉组件

1.组件结构:

2.index.js:

//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])
    }
  }
})

3.index.json:

1 {
2   "component": true,
3   "usingComponents": {}
4 }

4.index.wxml:

<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>

5.index.wxss:

/* 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);
}

使用方式(引用组件的页面):

1.index.js:

Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectArray: [{
      "id": "10",
      "value": "会计类"
    }, {
      "id": "21",
      "text": "工程类"
    }, '技术类', {'value': '其他'}]
  },

  select: function(e) {
    console.log(e.detail)
  }

})

2.pindex.json:

{
  "navigationBarTitleText":"下拉测试",
  "usingComponents": {
    "single-dropdown-select": "/components/single-dropdown-select/index"
  }
}

3.pindex.wxml:

1 <view class="weui-cell">
2     <view class="weui-cell__hd">类型:</view>
3     <view class="weui-cell__bd">
4       <single-dropdown-select prop-array='{{selectArray}}'  bind:select='select' />
6     </view>
7   </view>

4.效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值