问题描述:由于小程序没有自带的select下拉框,所以自定义了个公共组件
效果展示:
1、在与page同级的component下新建一个select文件夹
select.jxml
<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\' jd:if="{{selectShow}}"> <view jd:for="{{propArray}}" data-index="{{index}}" jd:key=\'index\' class=\'ms-option\' bindtap=\'setText\'>{{item.name || item.value || item}}</view> </view> </view>
select.jxss
.ms-content-box { width: 320rpx; } .ms-content { border: 1px solid #e2e2e2; background: white; font-size: 16px; position: relative; height: 56rpx; line-height: 56rpx; border-radius: 6rpx; color: #9e9e9e; padding: 6rpx 6rpx ; margin-bottom: 6rpx; } .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; min-height: 62rpx; overflow: auto; max-height: 380rpx; } .ms-option { height: 60rpx; line-height: 60rpx; border-top: 1px solid #e2e2e2; padding: 8rpx 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: 16rpx; } .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); }
select.js
Component({ /** * 组件的属性列表 */ properties: { propArray: { type: Array, }, selectText: String, }, /** * 组件的初始数据 */ 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].name || nowData[nowIdx].value || nowData[nowIdx];//当前点击的内容 this.setData({ selectShow: false, selectText: nowText, }) this.triggerEvent(\'select\', nowData[nowIdx]) } } })
select.json
{ "component": true, "usingComponents": {} }
2、在想要引入的页面使用
index.jxml
<view class="select_item" > <select prop-array=\'{{selectArray1}}\' selectText="{{selectText1}}" bind:select=\'select1\'></select> <select prop-array=\'{{selectArray2}}\' selectText="{{selectText2}}" bind:select=\'select2\'></select> </view>
index.css
.select_item{ display: flex; justify-content: space-around; margin-top: 160rpx; }
index.js
Page({ data:{ selectArray1: [ {id:1,text:\'苹果\'}, {id:2,text:\'梨子\'} ], selectArray2: [ {id:1,text:\'语文\'}, {id:2,text:\'数学\'} ], selectText1: \'品牌\', selectText2: \'类别\', },
// 下拉框选中
select1(e){
console.log(e)
....
},
select2(e){
console.log(e)
.......
},
})
index.json
{ "navigationBarTitleText": "", "navigationBarBackgroundColor": "#fff", "usingComponents": { "select": "/component/select/select" } }
总结,主要就是父子组件之间传递数据和方法的问题