当前位置:网站首页>Wechat applet custom multi selector
Wechat applet custom multi selector
2022-06-29 09:17:00 【swag_ Special actor】
demand

How to choose more than one ? Selector has multiple columns 、 Single item but no multiple items , Do it yourself , Have ample food and clothing !
The effect that needs to be achieved
Print the confirmed value on the import page 


Realize the idea
- To have a mask layer , Mask layer and selector Don't nest , Otherwise, clicking the selector may trigger a mask layer event .
- The mask layer is a fixed layout , All over the screen ; Selectors are absolute layouts , Take up... Of the screen 60%.
- Selectors The toolbar should be fixed , Under the toolbar The options should be scrollable .
- Click options to You can choose , Click again on the Cancel .
- Click... In the toolbar Cancel , determine ; Click on mask Want to have
triggerEvent - How to determine whether an option has been selected ? Here I use arrays
selectedArrayTo select an option, index the optionpushgo in , Judge whether it is on the selected array on the node , On displayselectedstyle , Show when you're not therenormalstyle . Be careful : ArrayselectedArrayWhat is stored is the index of the element in the selector data source . - Clear after data transfer
selectedArray
difficulty
- How to judge whether the element is in the selected array on the node ?
Wechat does not vue Filter , But there arewxs, This can be used as a filter .


Be careful : Be sure to create a new wxs File re import for use

- How to click to select , Click Cancel again ?
First determine whether it is in the selected array , If you do, cancel , That is, stack from the selected array , If you are not there, choose , That is, the stack array .
The page introduce


Some fake data 
Source code
mpx frame
Components :
<template>
<view wx:if="{
{show}}" class="overlay" bindtap="toClose"></view>
<view class="container" wx:if="{
{show}}">
<view class="toolbar">
<view class="cancel" catchtap="toClose"> Cancel </view>
<view class="title">{
{
title}}</view>
<view class="confirm" catchtap="toConfirm"> confirm </view>
</view>
<view class="contentBox">
<block wx:for="{
{option}}" wx:key="index">
<view
class="contentItem {
{common.isContain(selectedArray, index) ? 'selected' : 'normal' }}"
catchtap="select(index)"
>{
{
item.text}}</view>
</block>
</view>
</view>
<wxs module="common" src="../../wxs/common.wxs" />
</template>
<script>
import {
createComponent } from '@mpxjs/core'
createComponent({
data: {
show: false, // Control whether to show
selectedArray: [0] // The first... Is selected by default
},
properties: {
title: {
type: String,
value: ''
},
show: {
type: Boolean,
value: false,
observer: function(val) {
this.setData({
show: val
})
}
},
option: {
type: Array,
value: []
}
},
methods: {
// Turn off the pop-up layer
toClose() {
this.setData({
// Clear the selected array
selectedArray: []
})
this.triggerEvent('close')
},
// confirm
toConfirm() {
let object = {
}
let array = []
this.selectedArray.forEach(element => {
array.push(this.option[element])
})
object.value = array
this.setData({
// Clear the selected array
selectedArray: []
})
this.triggerEvent('confirm', object)
},
// choice
select(index) {
if (this.selectedArray.includes(index)) {
// Already in the selected array
let position = this.selectedArray.indexOf(index)
this.selectedArray.splice(position, 1) // Out of the stack
} else {
// Not in the selected array
this.selectedArray.push(index) // Into the stack
}
}
}
})
</script>
<style lang="stylus" scoped>
.overlay
background rgba(0, 0, 0, 0.7)
position fixed
top 0
left 0
width 100%
height 100%
z-index 20
.container
z-index 21
position absolute
background #fff
bottom 0
left 0
width 100%
height 60%
box-sizing border-box
border-radius 20rpx 20rpx 0 0
padding 30rpx 32rpx 40rpx 32rpx
.toolbar
width 100%
height 10%
display flex
justify-content space-between
align-items center
padding 0 0 30rpx 0
.cancel
font-size 26rpx
color #909090
text-align left
.confirm
color #576b95
font-size 26rpx
text-align right
.title
color #242424
font-size 32rpx
font-weight 500
.contentBox
width 100%
height 90%
overflow scroll
.contentItem
width 100%
height 60rpx
border-radius 6rpx
font-size 32rpx
color #242424
text-align center
line-height 56rpx
margin-bottom 30rpx
.normal
border 1px solid #ddd
.selected
border 1px solid #6ed854
</style>
<script type="application/json">
{
"component": true,
"usingComponents": {
}
}
</script>
wxs:
// Is it in an array
var isContain = function (array, item) {
return array.indexOf(item) === -1 ? false : true
}
module.exports = {
isContain: isContain
}
边栏推荐
猜你喜欢

Wechat applet determines the file format of URL

调试H5页面-weinre及spy-debugger真机调试

Uni app gets the route URL of the current page

pytorch总结—自动求梯度

Open3D 最远点采样(FPS)

LFFD:一种用于边缘检测的轻量化快速人脸检测器

Universal target detection based on region attention

pytorch总结学习系列-操作
![[to.Net] C data model, from Entity Framework core to LINQ](/img/98/6a8b295d1465697945e01b8c48ec52.png)
[to.Net] C data model, from Entity Framework core to LINQ

手写VirtualDOM
随机推荐
调试H5页面-weinre及spy-debugger真机调试
JS获取图片或base64的宽高等基本信息
Uniapp wechat applet reports an error typeerror: cannot read property 'call' of undefined
Training kernel switching using GPU
uniapp引入组件不生效解决方法
uniapp微信小程序报错 TypeError: Cannot read property ‘call‘ of undefined
promise方法的简单使用
What exactly does Devops mean?
Handwritten virtualdom
Redo after JS rotation view (longer full version, can be run)
Wechat applet sub components transfer values to the page (communication between parent and child components) with source code
微信小程序项目:tab导航栏
Pointnet的网络学习
Cdga | what is the core of digital transformation in the transportation industry?
Abstract classes and interfaces
基于区域注意的通用目标检测
Debugging H5 page -weinre and spy debugger real machine debugging
什么是超融合?与传统架构有什么区别?.
Let you know today that the passing rate of the PMP Exam is 97%, is it believable
Scenario analysis of deadlock during MySQL insert