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

实现效果

  • 筛选是filter,搜索框如有显隐需要,需自行添加配置显隐参数
  • 弹出层高度样式需要手动修改,需自行添加配置高度参数
  • .json文件配置"component": true,

实现代码

组件代码
html 复制代码
<van-popup show="{{ show }}" position="bottom" round custom-style="height:100%;z-index:2000;" class="relative">
  <view class="pd-30 mb-30 fixed pickerTop w100 bg-white">
    <view class="flex">
      <view class="gray-3 fs14" bindtap="close">取消</view>
      <view class="pickerText bold">{{title}}</view>
      <view class="fs14" style="color: {{colorStyle}};" bindtap="getData">确认</view>
    </view>
    <view class="pd-20 mt-40 bg border radius-10 flex_l">
      <image src="../../../assets/img/search1.png" style="width: 40rpx;height: 40rpx;" mode="" class="mr-20" />
      <input bindinput="getSearch" placeholder="请输入">
      </input>
    </view>
  </view>
  <view class="plr-30 mulPicker">
    <van-checkbox-group value="{{ value }}" bind:change="onChange">
      <van-cell-group>
        <van-cell wx:for="{{ dataList }}" wx:key="index" value-class="value-class" clickable data-index="{{ index }}" bind:click="toggle">
          <van-checkbox catch:tap="noop" class="checkboxes-{{ index }}" checked-color="{{colorStyle}}" name="{{ item.dictValue * 1 }}">
            {{item.dictLabel}}
          </van-checkbox>
        </van-cell>
      </van-cell-group>
    </van-checkbox-group>
  </view>
</van-popup>
js 复制代码
// pages/menu/components/pickDateTime.js
Component({
  options: {
    addGlobalClass: true, // 可允许外部修改样式
  },
  /**
   * 组件的属性列表
   */
  properties: {
    show: Boolean,// 是否显示弹出层
    title: String, // 弹出层标题
    columns: {
      type: Array,
      value: [],
      observer: function () {
        this.handleData();
      }
    }, // 选择的数据
    value: Array,  // 选中的值
    colorStyle: {
      type: String,
      value: "#3772E9"
    },
    flag: String, // 1-服务对象 2-位置
  },
  /**
   * 组件的初始数据
   */
  data: {
    selectItem: null,
    dataList: [],
  },
  /**
   * 组件的方法列表
   */
  methods: {
    handleData() {
      this.setData({
        dataList: this.data.columns
      })
    },
    getData() {
      this.triggerEvent("getData", this.data.selectItem);
      this.close();
    },
    onChange(e) {
      console.log(e.detail);
      let item
      let list
      if (e.detail.length > 0) {
        if (e.detail.length > 1) {
          list = e.detail.splice(1, 1)
        } else {
          list = e.detail
        }
        item = this.properties.columns.find(r => r.dictValue == list[0])
      } else {
        item = null
        list = []
      }
      this.setData({ value: list, selectItem: item });
    },
    close() {
      console.log("pick");
      this.triggerEvent("close");
    },
    toggle(event) {
      const { index } = event.currentTarget.dataset;
      const checkbox = this.selectComponent(`.checkboxes-${index}`);
      checkbox.toggle();
    },
    noop() { },
    getSearch(event) {
      let { value, cursor, keyCode } = event.detail
      //keyCode 为键值,处理函数可以直接 return 一个字符串,将替换输入框的内容。
      /* wx.http('yourHttpUrl', {
        name: value
      }, 'post').then(data => {
        console.log('getSearch');
        let list = data.map(r => {
          return {
            ...r,
            dictValue: r.olderId,
            dictLabel: r.olderName
          }
        })
        this.setData({
          columns: list
        })
      }) */
      let list = this.data.columns.filter(item => item.nodeName?.includes(value))
      this.setData({
        dataList: value ? list : this.data.columns
      })
    }
  },
})
css 复制代码
/* pages/menu/components/pickDateTime.wxss */
.pickerText {
  font-size: 16px;
}

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

.mulPicker {
  margin-top: 240rpx;
}

.value-class {
  flex: none !important;
}
页面使用
html 复制代码
<w-picker show="{{show}}" columns="{{columns}}" title="{{title}}" bindgetData="getData" colorStyle="{{colorStyle}}" bindclose="close" />
js 复制代码
  getData(e) {
    console.log(e.detail);
    this.close();
  },
  close() {
    this.setData({
      show: false,
    })
  },
相关推荐
2501_916007475 小时前
Python实现HTTPS爬虫的完整指南:使用requests、BeautifulSoup、Selenium和Scrapy
爬虫·python·ios·小程序·https·uni-app·iphone
FungLeo6 小时前
Taro 开发小程序, 把 @ 配成 src 路径别名,webpack / tsconfig / scss 三处同步的正确姿势
webpack·小程序·taro
万亿少女的梦1686 小时前
基于微信小程序、Express与MongoDB的校园失物招领系统设计
mongodb·微信小程序·node.js·express·系统设计
灵枢时代6 小时前
小程序在生鲜配送行业,如何设计夜间预约和次日达的订单处理流程?
数据结构·人工智能·小程序
小徐_233319 小时前
Wot UI 2.3.0 发布:二维码组件来了,Open Wot 与 wot-starter 同步更新
前端·微信小程序·uni-app
2601_953720821 天前
【计算机毕业设计】基于微信小程序的拼车服务系统设计与实现
微信小程序·小程序·课程设计
乐启国际旅行社有限公司1 天前
文旅小程序性能优化:分包加载+地图视口懒加载解决景区卡顿与包超限
性能优化·小程序
FungLeo1 天前
Taro 4 微信小程序:RootPortal CSS 变量继承问题与自建 PagePortal 解决方案
微信小程序·taro·css 变量
万岳科技系统开发2 天前
校园跑腿外卖搭建助力高校周边商家拓展线上业务
大数据·人工智能·小程序
微三云 - 廖会灵 (私域系统开发)2 天前
消费返物业费平台搭建:美家时代商业模式拆解与系统开发选型指南
小程序·系统架构