微信小程序 选择学期控件 自定义datePicker组件 不复杂

我的时间选择组件在common文件夹里

datePicker组件代码

javascript 复制代码
html:
<view class="date_bg_view">
</view>
<view class="date_content">
  <view class="date_title">
    <image src="/image/icon_close_black.png" class="close" bindtap="onClose"></image>
    <text class="date_title_txt">选择时间</text>
  </view>
  <view class="line"></view>
  <!-- 快速选择 -->
  <view class="date_quick_choose">
    <text class="date_quick_txt">快速选择</text>
    <view class="date_quick_show">
      <view class="{{item.flag ? 'quick_cell_flag' : 'quick_cell'}}" wx:for="{{dateList}}" wx:key="id" bindtap="handleQuickChoose" data-index="{{index}}">
        {{item.name}}
      </view>
    </view>
  </view>
  <!-- 自定义时间 -->
  <view class="date_quick_choose">
    <text class="date_quick_txt">自定义时间</text>
    <view class="date_picker">
      <picker mode="date" value="{{startDate}}" start="2015-09-01" end="2999-12-31" bindchange="bindDateChange" data-type="1"  mask-style=" color='#0A3E79'">
        <view class="picker">
        {{startDate?startDate:'开始时间'}}
        </view>
      </picker> 
      <text style="width: 20rpx;height: 1rpx;background-color: #999;"></text>
      <picker mode="date" value="{{endDate}}" start="2015-09-01" end="2999-12-31" bindchange="bindDateChange"  data-type="2">
        <view class="picker">
          {{endDate?endDate:'结束时间'}}
        </view>
      </picker>
    </view>
  </view>
  <button class="sure_btn" bindtap="handleSure"> 确定 </button>
</view>

css:
.date_bg_view{
  position: absolute;
  top: 0;
  left: 0;
  z-index: 10;
  width: 100%;
  height: 100%;
  background-color: #000;
  opacity: 0.3;  
}
.date_content{  
  position: fixed;
  bottom: 0;  
  left: 0;  
  z-index: 11;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: flex-start;
  width: 100%;
  height: 75%;
  background-color: #fff;      
  border-top-left-radius: 20rpx;
  border-top-right-radius: 20rpx;
}
.date_title{
  z-index: 11;
  width: 100%;
  display: flex;
  justify-content: center;
  height: 90rpx;
  line-height: 90rpx;
}
.close{  
  position: absolute;
  top: 20rpx;
  right: 20rpx;
  width: 30rpx;
  height: 30rpx;
}
.date_title_txt{
  font-size: 35rpx;
  font-weight: bold;
}
.line{
  margin: 10rpx;
  width: 100%;  
  height: 1rpx;
  background-color: #eee;
}
.date_quick_choose{
  z-index: 11;
  padding: 20rpx;
  width: 100%;  
}
.date_quick_txt{
  z-index: 12;
  margin: 20rpx;    
  font-size: 30rpx;
  color: #666;  
}
.date_quick_show{
  z-index: 12;  
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
  flex-direction: row;
  align-items: center;  
  width: 100%;  
}
.quick_cell{
  z-index: 12;
  margin: 2%;
  padding: 5rpx;
  width: 29%;  
  height: 60rpx;
  text-align: center;
  line-height: 60rpx;      
  font-size: 28rpx;
  background-color: #F7F7F7;
  color: #999;
  border: 1rpx solid #F7F7F7;
  border-radius: 10rpx;
}
.quick_cell_flag{
  z-index: 12;
  margin: 2%;
  padding: 5rpx;
  width: 29%;  
  height: 60rpx;
  line-height: 60rpx;
  font-size: 28rpx;
  text-align: center;      
  background-color: #fff;
  color: #19b2ff;
  border: 1rpx solid #19b2ff;
  border-radius: 10rpx;
}
.date_picker{  
  margin-top: 30rpx;
  margin-left: 10%;
  display: flex;    
  justify-content: space-between;
  align-items: center;
  width: 80%;
}
.picker{  
  padding: 10rpx 40rpx;
  background-color: #fff;
  font-size: 30rpx;
  color: #19b2ff;
  font-weight: bold;
}
.sure_btn{
  margin-top: 80rpx;  
  font-size: 28rpx;
  width: 90%;    
  height: 80rpx;
  text-align: center;
  line-height: 80rpx;
  background-color: #19b2ff;
  color: #fff;  
}


js:

const util = require('../../utils/util.js');
import {
  formatDate
} from "../../utils/date";
const app = getApp();
Component({
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      var currentDate = new Date();

      // 获取当前年份和月份
      var currentYear = currentDate.getFullYear();
    
      // 计算五年内的上学期和下学期
      var semesters = [];
      for (var i = 0; i < 5; i++) {
        var years = currentYear - i;
        semesters.push({
          name: years + '下半学年',
          dates: [years + '-09-01', (years + 1) + '-01-31'],
          flag: false
        });     
        semesters.push({
          name: years + '上半学年',
          dates: [years + '-02-01', years + '-06-30'],
          flag: false
        });        
      }

      // 输出结果
      this.setData({
        dateList: semesters
      })
    },

  },
  properties: {},

  /**
   * 组件的初始数据
   */
  data: {
    dateList: [],
    startDate: '',
    endDate: '',
    currentTime: formatDate(new Date()),
    oidx: null,
  },

  methods: {
    /**
     * 快速选择
     * @param {*} e 
     */
    handleQuickChoose(e) {
      let that = this;
      let oidx = e.currentTarget.dataset.index;
      let odateList = that.data.dateList
      
      // 遍历数组,并修改flag属性为false
      odateList.forEach(item => {
        item.flag = false;
      });
      odateList[oidx].flag = true
      that.setData({
        startDate: odateList[oidx].dates[0],
        endDate: odateList[oidx].dates[1],
        oidx,
        dateList: odateList
      })      
    },
    bindDateChange(e) {
      //console.log('picker发送选择改变,携带值为', e)
      let that = this;
      let type = e.currentTarget.dataset.type
      if (type == 1) {
        that.setData({
          startDate: e.detail.value
        })
      } else {
        that.setData({
          endDate: e.detail.value
        })
      }
    },
    /**
     * 顶部关闭按钮
     */
    onClose() {
      this.triggerEvent('sync', {
        show: false
      })
    },

    /**
     * 确定
     */
    handleSure() {
      let that = this;
      if (!that.data.startDate || !that.data.endDate) {
        util.alert('学期选择不能为空!');
        return false;
      }
      if (that.data.startDate <= that.data.endDate) {
        that.triggerEvent('sync', {
          show: false,
          startDate: that.data.startDate,
          endDate: that.data.endDate
        })
      } else {
        util.alert('结束时间不能小于开始时间');
        return false;
      }

    }
  },

})


json:
{
  "component": true,
  "usingComponents": {}
}

调用的页面:

javascript 复制代码
json:
"usingComponents": {
  "datePicker":"../../common/datePicker/index"
},


html:
      <view class="check-list">
        <view class="check-list-lef">学期选择</view>
        <view class="check-list-rig">
          <view class="picker" bind:tap="openDatePicker">{{ startDate && endDate ? (startDate + ' - ' + endDate) : '请选择学期'}}</view>
          <!-- 时间选择组件 bind:sync 子类回传数据的方法-->
          <datePicker wx:if="{{show}}" bind:sync="syncGetDate"></datePicker>                    
        </view>
      </view>


js:
data:{
  startDate: '',
  endDate: '',
  show: false,//显示隐藏时间控件
}

  openDatePicker(e) {
    this.setData({
      hiddenChart: true,
      show:true
    })
  },
  syncGetDate(e){
    let that = this;
    let show = e.detail.show; 
    if(!show){ // 子组件点击了关闭   关闭弹窗
      that.setData({
        show:false,
        startDate: e.detail.startDate,
        endDate: e.detail.endDate,
        //hiddenChart: false,
      })
    }
    //console.log("子组件:",e);    
    //this.getStatisticThemeActivitiesTrend()
  },
相关推荐
2501_9159184137 分钟前
iOS 26 查看电池容量与健康状态 多工具组合的工程实践
android·ios·小程序·https·uni-app·iphone·webview
2501_915909062 小时前
iOS 架构设计全解析 从MVC到MVVM与使用 开心上架 跨平台发布 免Mac
android·ios·小程序·https·uni-app·iphone·webview
2501_9160088912 小时前
Web 前端开发常用工具推荐与团队实践分享
android·前端·ios·小程序·uni-app·iphone·webview
2501_9159214312 小时前
“HTTPS 个人化”实战,个人站点与设备调试的部署、验证与抓包排查方法
网络协议·http·ios·小程序·https·uni-app·iphone
菜鸟una12 小时前
【微信小程序 + 消息订阅 + 授权】 微信小程序实现消息订阅流程介绍,代码示例(仅前端)
前端·vue.js·微信小程序·小程序·typescript·taro·1024程序员节
韩立学长14 小时前
【开题答辩实录分享】以《租房小程序的设计和实现》为例进行答辩实录分享
java·spring boot·小程序
從南走到北14 小时前
JAVA国际版一对一视频交友视频聊天系统源码支持H5 + APP
java·微信·微信小程序·小程序·音视频·交友
future_studio15 小时前
聊聊 Unity(小白专享、C# 小程序 之 联机对战)
unity·小程序·c#
隔壁程序员老王15 小时前
基于 Python 的坦克大战小程序,使用 Pygame 库开发
python·小程序·pygame·1024程序员节
從南走到北15 小时前
同城派送小程序
微信·微信小程序·小程序