微信小程序单双周选择排序有效果图

效果图

.wxml

复制代码
<view class="group-box">
  <label class="radio" wx:for="{{['单周','双周','全选']}}" wx:key="index" bind:tap="radioChange"
  data-index="{{index}}">
      <radio checked="{{index === zcTem.index}}"/>{{item}}
  </label>
</view>
<view class="week-box">
  <view wx:for="{{zcArray}}" wx:key="index" class="week-item {{zcTem.arr[index]?'active':''}}"
  bind:tap="zcSelect" data-index="{{index}}">
    {{item}}
  </view>
</view>
<view class="label">{{zcTem.label}}</view>

.wxss

复制代码
view{box-sizing: border-box;}
.group-box{
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 30rpx 0;
}
.group-box>label{
  display: flex;
  align-items: center;
}
.group-box>label:nth-child(2){
  margin: 0 50rpx;
}
.week-box{
  padding: 0 50rpx;
}
.week-item{
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: calc((100% - 64rpx) / 5);
  height: 68rpx;
  margin: 10rpx 0 0 16rpx;
  background-color: #F3F5F7;
  border-radius: 10rpx;
  transition: background-color 0.3s;
}
.week-item:nth-child(5n + 1){
  margin-left: 0;
}
.active{
  background-color:#07C160;
  color: white;
}
.label{
  text-align: center;
  padding: 30rpx;
}

.js

复制代码
Page({
  data: {
    zcCount:25,
    zcTem:{
      label: '',
      index: -1,
      arr: []
    },
    zcArray:[],
    zcSingle:[],
    zcDouble:[],
  },
  onLoad(){
    const arr = []
    const single = []
    const double = []
    const zcCount = this.data.zcCount
    const zcTem = this.data.zcTem
    for (let i = 1; i <= zcCount; i++) {
      arr.push(`${i}`)
      zcTem.arr.push('')
      if (i % 2 !== 0) {
        single.push(`${i}`)
        double.push('')
        continue
      }
      single.push('')
      double.push(`${i}`)
    }
    this.setData({
      zcTem,
      zcArray:arr,
      zcSingle:single,
      zcDouble:double,
    })
  },
  radioChange(e){
    const index = e.currentTarget.dataset.index
    const o = this.data.zcTem
    if (index === o.index) {
      o.index = -1
      o.label = ''
      o.arr = []
      this.setData({
        zcTem:o,
      })
      return
    }
    o.index = index
    let tem
    if (index === 0) {
      const d = this.data.zcSingle
      o.label = `第${d[0]}-${d[d.length - 1]}周(单周)`
      tem = JSON.stringify(d)
    } else if (index === 1) {
      const d = this.data.zcDouble
      o.label = `第${d[1]}-${d[d.length - 2]}周(双周)`
      tem = JSON.stringify(d)
    } else {
      const d = this.data.zcArray
      o.label = `第1-${d[d.length - 1]}周`
      tem = JSON.stringify(d)
    }
    o.arr = JSON.parse(tem)
    this.setData({
      zcTem:o,
    })
  },
  zcSelect(e){
    const index = e.currentTarget.dataset.index
    const o = this.data.zcTem
    o.arr[index] = o.arr[index] ? '' : `${index + 1}`
    if (JSON.stringify(o.arr) === JSON.stringify(this.data.zcArray)) {
      o.index = 2
      const d = this.data.zcArray
      o.label = `第1-${d[d.length - 1]}周`
      this.setData({
        zcTem:o
      })
      return
    }
    if (JSON.stringify(o.arr) === JSON.stringify(this.data.zcSingle)) {
      o.index = 0
      const d = this.data.zcSingle
      o.label = `第${d[0]}-${d[d.length - 1]}周(单周)`
      this.setData({
        zcTem:o
      })
      return
    }
    if (JSON.stringify(o.arr) === JSON.stringify(this.data.zcDouble)) {
      o.index = 1
      const d = this.data.zcDouble
      o.label = `第${d[1]}-${d[d.length - 2]}周(双周)`
      this.setData({
        zcTem:o
      })
      return
    }
    const len = o.arr.length
    let arr = ''; let start = ''; let end = '周'; let or = 0; let name = ''
    for (let i = 0; i < len; i++) {
      if (o.arr[i]) {
        if (end === '周') {
          name = parseInt(o.arr[i]) % 2 !== 0 ? '单周' : '双周'
          start = arr ? `,第${o.arr[i]}` : `第${o.arr[i]}`
        }
        if (o.arr[i + 1]) {
          if (or === 2) {
            or = 0
            arr += start + end
            start = ''
            end = '周'
            continue
          }
          or = 1
          end = `-${o.arr[i + 1]}周`
          continue
        }
        if (or !== 1 && o.arr[i + 2]) {
          i += 1
          or = 2
          end = `-${o.arr[i + 1]}${name}`
          continue
        }
        i += or === 1 ? 1 : 2
        or = 0
        if (start) {
          arr += start + end
          start = ''
          end = '周'
        }
      }
    }
    o.index = -1
    o.label = arr
    this.setData({
      zcTem:o
    })
  }
})

遇到问题可以看我主页加我Q,很少看博客,对你有帮助别忘记点赞收藏。

相关推荐
喝牛奶的小蜜蜂43 分钟前
个人小程序:不懂后台,如何做数据交互
前端·微信小程序·小程序·云开发
2501_918941052 小时前
旅游微信小程序制作指南
微信小程序·小程序·旅游
邹荣乐3 小时前
微信小程序动态tabBar实现:基于自定义组件,灵活支持不同用户角色与超过5个tab自由组合
前端·微信小程序·uni-app
半兽先生7 小时前
uniapp微信小程序视频实时流+pc端预览方案
微信小程序·uni-app·音视频
Uyker8 小时前
空间利用率提升90%!小程序侧边导航设计与高级交互实现
前端·微信小程序·小程序
小二·1 天前
Vue前端篇——Vue 3的watch深度解析
微信小程序·小程序
加油乐1 天前
uniapp开发微信小程序---分包
前端·微信小程序·uni-app
侑虎科技1 天前
如何优化微信小游戏在iOS机器上Shader变体预热特别慢的问题
性能优化·微信小程序
三脚猫的喵1 天前
微信小程序实现运动能耗计算
javascript·微信小程序·小程序
疯狂的沙粒2 天前
uniapp 开发企业微信小程序,如何区别生产环境和测试环境?来处理不同的服务请求
微信小程序·uni-app·notepad++