微信小程序+Vant-自定义选择器组件(多选

实现效果

  • 无筛选,如有需要可参照单选组件中的方法
  • .json文件配置"component": true,
  • columns需要处理成含dictLabeldictValue字段,我是这样处理的:
css 复制代码
let list = arr.map(r => {
  return {
    ...r,
    dictValue: r.xxxId,
    dictLabel: r.xxxName
  }
})

实现代码

组件代码
html 复制代码
<van-popup show="{{ show }}" position="bottom" round custom-style="height:{{height}}%;z-index:2000;" class="relative">
  <view class="pd-30 flex mb-30 fixed pickerTop w100 bg-white">
    <view class="gray-3 fs14" bindtap="close">取消</view>
    <view class="pickerText bold">{{title}}</view>
    <view class="fs14" style="color: {{colorStyle}};" bindtap="getData1">确认</view>
  </view>
  <view class="mb-30 plr-30 mulPicker">
    <van-checkbox-group value="{{ valueList }}" bind:change="onChange">
      <van-cell-group>
        <van-cell wx:for="{{ columns }}" wx:key="index" value-class="value-class" clickable data-index="{{ index }}" bind:click="toggle">
          <van-checkbox catch:tap="noop" class="checkboxes-{{ index }}" shape="square" checked-color="{{colorStyle}}" name="{{ item.dictValue }}">
            {{item.dictLabel}}
          </van-checkbox>
        </van-cell>
      </van-cell-group>
    </van-checkbox-group>
  </view>
</van-popup>
js 复制代码
import { formatTime, formatTime1 } from '../../../../utils/index'

Component({
  options: {
    addGlobalClass: true,
  },
  /**
   * 组件的属性列表
   */
  properties: {
    show: Boolean,// 是否显示弹出层
    height: {
      type: Number,
      value: 80 // 默认80%,百分比0-100
    },
    title: {
      type: String,
      value: '请选择' 
    }, // 弹出层标题
    columns: Array, // 下拉选择的数据
    valueList: Array, // 选中回显
    colorStyle: {
      type: String,
      value: "#3772E9"
    },
  },
  /**
   * 组件的初始数据
   */
  data: {},
  /**
   * 组件的方法列表
   */
  methods: {
    getData1(e) {
      this.triggerEvent("getData1", this.data.valueList);
      this.close();
    },
    onChange(e) {
      this.setData({ valueList: e.detail, });
    },
    toggle(event) {
      const { index } = event.currentTarget.dataset;
      const checkbox = this.selectComponent(`.checkboxes-${index}`);
      checkbox.toggle();
    },
    noop() { },
    close() {
      this.triggerEvent("close");
    },
  },
})
css 复制代码
.pickerText {
  font-size: 16px;
}

.pickerTop {
  border-radius: 50rpx 50rpx 0 0;
  z-index: 2000;
}

.mulPicker {
  margin-top: 120rpx;
}

.value-class {
  flex: none !important;
}
页面使用
html 复制代码
<w-mulpicker show="{{show}}" columns="{{columns}}" title="{{title}}" bindgetData1="getData1" colorStyle="{{colorStyle}}" bindclose="close" />
js 复制代码
  getData1(e) {
    console.log(e.detail);
    this.close();
  },
  close() {
    this.setData({
      show: false,
    })
  },
相关推荐
暖阳_xm11 分钟前
flex布局实现横向滚动
前端·css·微信小程序
咸虾米1 小时前
微信云开发支付配置绑定商户号时,无法发起授权,通过工作流或云开发模版解决支付问题
微信小程序
萌萌哒草头将军4 小时前
🚀惊了,这个国产软件居然这么牛,比 uniapp 还全能❤️
微信小程序·uni-app·小程序·云开发
大叔_爱编程4 小时前
wx219基于ssm+vue+uniapp的教师管理系统小程序
vue.js·小程序·uni-app·毕业设计·ssm·源码·课程设计
qq_424409195 小时前
uniapp的h5,打开的时候,标题会一闪而过应用名称,再显示当前页面的标题
微信小程序·uni-app·html5
f(me)5 小时前
如何保障企业数据的安全?软件开发中的数据安全防护措施
网络安全·小程序
换日线°6 小时前
微信小程序边框容器带三角指向
css·微信小程序
small_fox_dtt6 小时前
微信小程序转为App实践篇 FinClip
微信小程序·小程序
2401_897930066 小时前
微信小程序中大型项目开发实战指南
微信小程序·小程序
小咕聊编程7 小时前
【含文档+PPT+源码】基于微信小程序的学校体育馆操场预约系统的设计与实现
微信小程序·小程序