小程序筛选

预览

图片描述

实现过程

app.json中添加

"pages": [
    "pages/filter/index"
  ],

filter/index.wxml

<view class="container">
  <view class="list">
    <view class="search-cat" wx:for="{{searchList}}" wx:for-item="p" wx:for-index="pIndex" wx:key="">
      <view class="search-title">{{p.screenKey}}</view>
      <view class="search-items">
        <view bindtap="onChange" wx:for="{{p.screenValue}}" wx:for-item="g" data-parent-index="{{ pIndex }}" data-index="{{ index }}"
          data-item="{{ p }}" class="item {{ g.checked ? 'active' : '' }}" wx:key=""  >
          {{g.value}}
        </view>
      </view>
    </view>
  </view>
  <view class="search-bottom">
    <view class="cancel" bindtap="doCancel">取消</view>
    <view class="confirm" bindtap="doSubmit">确认</view>
  </view>
</view>

filter/index.wxss

.container {
    padding: 0 0 15px 15px;
    background: #fff;
    min-height: 100vh;
}

.search-title {
    padding: 19px 0;
    font-size: 16px;
    font-weight: 600;
}

.search-items {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
}

.search-items .item {
    min-width: 60px;
    box-sizing: border-box;
    padding: 0 15px;
    height: 25px;
    line-height: 25px;
    text-align: center;
    font-weight: 500;
    border-radius: 3px;
    margin: 0 20px 15px 0;
    font-size: 14px;
    background: #f3f3f6;
}

.search-items .item.active {
    background: #ff5000;
    color: #fff;
}

.search-bottom {
    position: fixed;
    bottom: 0;
    left: 0;
    height: 50px;
    line-height: 50px;
    width: 100%;
    display: flex;
    justify-content: center;
    width: 100%;
    font-weight: 600;
    font-size: 17px;
    text-align: center;
}

.search-bottom .cancel {
    background: #ececf0;
    color: #8D8D96;
    flex: 1;
}

.search-bottom .confirm {
    background: #FF5000;
    color: #fff;
    flex: 1;
}

filter/index.js

const App = getApp()
Page({
  data: {
    specialId: '',
    query: ['篮球鞋','36'], 
    searchList: [
      {
        type: 'radio',
        screenKey: '时尚',
        screenValue: ['篮球鞋', '运动鞋', '板鞋', '跑步鞋']
      },
      {
        type: 'radio',
        screenKey: '品牌',
        screenValue: ['阿迪达斯', '耐克', '皮尔卡丹']
      },
      {
        type: 'checkbox',
        screenKey: '尺码',
        screenValue: [
          '36',
          '36.5',
          '37',
          '37.5',
          '38',
          '38.5',
          '39',
          '39.5',
          '40',
          '40.5',
          '41',
          '41.5',
          '42',
          '42.5',
          '43',
          '43.5'
        ]
      }
    ] // 搜索关键词
  },
  onLoad: function(options) {
    console.log(options)
    // 上个页面传递搜索关键词数组,目前在data里query设置
    // this.data.query = options.query
    // 搜索关键词
    this.getSearchItems()
  },
  // 获取搜索选项
  getSearchItems() {
    const _this = this
    // 异步请求数据后处理,这里直接拿假数据
    const searchItems = this.data.searchList.map(n => {
      return Object.assign({}, n, {
        screenValue: n.screenValue.map(m =>
          Object.assign(
            {},
            {
              checked: _this.data.query.includes(m), // 回显query里有返回true
              value: m
            }
          )
        )
      })
    })
    this.setData({
      searchList: searchItems
    })
  },
 // 点击筛选项
  onChange(e) {
    const { parentIndex, item, index } = e.currentTarget.dataset
    // 如果选中状态
    if (item.screenValue[index].checked) {
      item.screenValue[index].checked = false // 取消选择
    } else {
      // 如果不是多选
      if (item.type != 'checkbox') {
        // 全部重置为未选择状态
        item.screenValue.map(n => (n.checked = false))
      }
      // 将点击的设置为选中
      item.screenValue[index].checked = true
    }
    this.setData({
      [`searchList[${parentIndex}]`]: item
    })
  },
  // 取消,清空数据返回上一个页面
  doCancel() {
    // var pages = getCurrentPages() // 获取页面栈
    // var prevPage = pages[pages.length - 2] // 前一个页面
    // prevPage.setData({
    //   query: [], // 重置
    //   isBack: true
    // })
    // // 返回登录之前的页面
    // wx.navigateBack({
    //   delta: 1
    // })
  },
  // 提交,携带数据返回上一个页面
  doSubmit() {
    let selected = []
    // 获取所有选中
    this.data.searchList.map(n => {
      n.screenValue.map(m => {
        if (m.checked == true) {
          selected.push(m.value)
        }
      })
    })
    console.log(selected)
    // var pages = getCurrentPages() // 获取页面栈
    // var prevPage = pages[pages.length - 2] // 前一个页面
    // // 携带数据,返回登录之前的页面
    // prevPage.setData({
    //   query: selected,
    //   isBack: true
    // })
    // wx.navigateBack({
    //   delta: 1
    // })
  }
})

总结

领导要求写在新的页面,就没有在页面写组件,后续如果再改写成组件,实现的过程相对简单,有什么问题可以留言交流
完整代码链接github

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值