<!--components/single-dropdown-select/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>
/* components/single-dropdown-select/index.wxss */
.ms-content-box {
width: 300rpx;
}
.ms-content {
/* border: 1px solid #e2e2e2;
background: white; */
font-size: 25rpx;
position: relative;
height: 40px;
line-height: 30px;
}
.ms-text {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
padding: 0 40px 0 6px;
font-size: 30rpx;
}
.ms-options {
background: white;
width: 100%;
position: absolute;
border: 1px solid #e2e2e2;
border-top: none;
box-sizing: border-box;
z-index: 111111111;
/* max-height: 120px; */
overflow: auto;
}
.ms-option {
height: 40px;
line-height: 40px;
border-top: 1px solid #e2e2e2;
padding: 0 6px;
text-align: left;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
font-size: 25rpx;
}
.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);
}
//index.js
Component({
/**
* 组件的属性列表
*/
properties: {
propArray: {
type: Array,
}
},
/**
* 组件的初始数据
*/
data: {
selectShow: false, //初始option不显示
selectText: wx.getStorageSync('selectText') ? wx.getStorageSync('selectText') :'全部订单', //初始内容
},
lifetimes: {
attached: function () {
console.log(this.data.propArray);
}
},
/**
* 组件的方法列表
*/
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])
}
}
})
{
"usingComponents": {},
"component": true
}
Componente pasando por valor
<view style="background-color: #ededed; position: fixed;top: 0;z-index: 11111111;width: 100%;">
<single-dropdown-select prop-array='{
{itemListArry}}' bind:select='select'></single-dropdown-select>
</view>
en el archivo json
{
"usingComponents": {
"tabbar": "../../custom-tab-bar/index",
"single-dropdown-select": "/components/single-dropdown-select/index"
}
}
Transferencia de datos de archivos JS (esto necesita pasar valores de acuerdo a sus propias necesidades, aquí es solo una referencia)
this.data.itemList.push({
all: '全部订单' + '·' + res.data.data.orderTotalNum,
finishedNum: '已完成' + '·' + res.data.data.finishedNum,
unpaidNum: '待支付费用' + '·' + res.data.data.unpaidNum,
signedNum: ' 待签署协议' + '·' + res.data.data.signedNum,
})
this.setData({
itemListObj: this.data.itemList[0]
})
this.setData({
itemList: Object.values(this.data.itemListObj),//只需要对象的值
})
console.log(this.data.itemList);
if(this.data.itemList && this.data.itemList.length>0){
wx.setStorageSync('selectText', this.data.itemList[0])//传值下拉框显示的初始值
}
let arryItem = []
this.data.itemList.forEach((v, i) => {
arryItem.push({
'value': v,
'id': i
})
})
this.setData({
itemListArry: arryItem,
showOrderName: this.data.itemList[0]
})
El efecto final (el estilo puede ser modificado por ti mismo)
Para el método de inserción de matriz en el applet, no se puede usar directamente. Necesita usar this.setData({}) para reasignar el valor para que tenga efecto
Link de referencia:
Cómo usar el componente de cuadro desplegable del subprograma WeChat: se busca programador