当前位置:网站首页>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
}
边栏推荐
- pytorch总结学习系列-数据操作
- MYSQL行转列例子
- (转)MySQL: ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
- Instance error iopub data rate exceeded
- ThinkPHP 6 使用 mongoDB
- Working for many years, recalling life -- three years in high school
- Picture format -webp
- MySQL row column conversion example
- Pat (basic level) practice (Chinese) 1003 I want to pass! (20 points) C language implementation
- Detailed version of two-stage target detection principle
猜你喜欢

图片格式-webp

Mqtt second session -- emqx high availability cluster implementation

uniapp引入组件不生效解决方法

Wechat applet sub components transfer values to the page (communication between parent and child components) with source code

一般乘法器设计,verilog code

手写VirtualDOM

查找字符串中重复次数最多的元素

微信小程序自定义多项选择器

js轮播图观后重做(较长的完整版,可运行)

promise方法的简单使用
随机推荐
记自定义微信小程序顶部导航栏
(转)MySQL: ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
两阶段目标检测原理详细版
verilog 移位操作符
train_on_batch保存一下loss函数变化的图像
Share code snippets of wechat applet
记微信小程序setData动态修改字段名
verilog 等价操作符
jar包和war包
Verilog 表达式
MYSQL行转列例子
What is the difference between hyperconverged architecture and traditional architecture?
微信小程序自定义多项选择器
微信小程序分享页面,分享到朋友圈
uniapp微信小程序报错 TypeError: Cannot read property ‘call‘ of undefined
微信小程序子组件向页面传值(父子组件间的通信)带源码
Summary of IO streams
npm常用命令
pytorch总结—TENSOR ON GPU
Verilog reduction operator