资讯专栏INFORMATION COLUMN

小程序筛选

awokezhou / 969人阅读

摘要:预览实现过程中添加取消确认篮球鞋时尚篮球鞋运动鞋板鞋跑步鞋品牌阿迪达斯耐克皮尔卡丹尺码搜索关键词上个页面传递搜索关键词数组,目前在里设置搜索关键词获取搜索选项异步请求

预览

实现过程

app.json中添加

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

filter/index.wxml


  
    
      {{p.screenKey}}
      
        
          {{g.value}}
        
      
    
  
  
    取消
    确认
  

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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/100814.html

相关文章

  • 数据库里的知识❤️

    DQL 查询 1. 查询所有得字段 select * from 表名 2.查询指定字段 select 字段1,字段2....from 表名 - 给字段起别名 select 字段1 as 别名,字段2... from 表名 - 注意点: 关于起别名得问题,a...

    陈江龙 评论0 收藏0
  • 程序大转盘红包雨营销组件

    前言 商城没几个营销活动能叫商城吗?所以就来几个组件吧,写的不好轻踩,对你有帮助记得给个小星星哦直接上链接github链接 线上体验 showImg(https://segmentfault.com/img/bVbwgDc?w=389&h=306); 运行例子 git clone https://github.com/sunnie1992/soul-weapp.git 微信开发者工具打开项目 营销...

    senntyou 评论0 收藏0

发表评论

0条评论

awokezhou

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<