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


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

    datePicker组件代码

    1. html:
    2. <view class="date_bg_view">
    3. view>
    4. <view class="date_content">
    5. <view class="date_title">
    6. <image src="/image/icon_close_black.png" class="close" bindtap="onClose">image>
    7. <text class="date_title_txt">选择时间text>
    8. view>
    9. <view class="line">view>
    10. <view class="date_quick_choose">
    11. <text class="date_quick_txt">快速选择text>
    12. <view class="date_quick_show">
    13. <view class="{{item.flag ? 'quick_cell_flag' : 'quick_cell'}}" wx:for="{{dateList}}" wx:key="id" bindtap="handleQuickChoose" data-index="{{index}}">
    14. {{item.name}}
    15. view>
    16. view>
    17. view>
    18. <view class="date_quick_choose">
    19. <text class="date_quick_txt">自定义时间text>
    20. <view class="date_picker">
    21. <picker mode="date" value="{{startDate}}" start="2015-09-01" end="2999-12-31" bindchange="bindDateChange" data-type="1" mask-style=" color='#0A3E79'">
    22. <view class="picker">
    23. {{startDate?startDate:'开始时间'}}
    24. view>
    25. picker>
    26. <text style="width: 20rpx;height: 1rpx;background-color: #999;">text>
    27. <picker mode="date" value="{{endDate}}" start="2015-09-01" end="2999-12-31" bindchange="bindDateChange" data-type="2">
    28. <view class="picker">
    29. {{endDate?endDate:'结束时间'}}
    30. view>
    31. picker>
    32. view>
    33. view>
    34. <button class="sure_btn" bindtap="handleSure"> 确定 button>
    35. view>
    36. css:
    37. .date_bg_view{
    38. position: absolute;
    39. top: 0;
    40. left: 0;
    41. z-index: 10;
    42. width: 100%;
    43. height: 100%;
    44. background-color: #000;
    45. opacity: 0.3;
    46. }
    47. .date_content{
    48. position: fixed;
    49. bottom: 0;
    50. left: 0;
    51. z-index: 11;
    52. display: flex;
    53. flex-direction: column;
    54. justify-content: flex-start;
    55. align-items: flex-start;
    56. width: 100%;
    57. height: 75%;
    58. background-color: #fff;
    59. border-top-left-radius: 20rpx;
    60. border-top-right-radius: 20rpx;
    61. }
    62. .date_title{
    63. z-index: 11;
    64. width: 100%;
    65. display: flex;
    66. justify-content: center;
    67. height: 90rpx;
    68. line-height: 90rpx;
    69. }
    70. .close{
    71. position: absolute;
    72. top: 20rpx;
    73. right: 20rpx;
    74. width: 30rpx;
    75. height: 30rpx;
    76. }
    77. .date_title_txt{
    78. font-size: 35rpx;
    79. font-weight: bold;
    80. }
    81. .line{
    82. margin: 10rpx;
    83. width: 100%;
    84. height: 1rpx;
    85. background-color: #eee;
    86. }
    87. .date_quick_choose{
    88. z-index: 11;
    89. padding: 20rpx;
    90. width: 100%;
    91. }
    92. .date_quick_txt{
    93. z-index: 12;
    94. margin: 20rpx;
    95. font-size: 30rpx;
    96. color: #666;
    97. }
    98. .date_quick_show{
    99. z-index: 12;
    100. display: flex;
    101. justify-content: flex-start;
    102. flex-wrap: wrap;
    103. flex-direction: row;
    104. align-items: center;
    105. width: 100%;
    106. }
    107. .quick_cell{
    108. z-index: 12;
    109. margin: 2%;
    110. padding: 5rpx;
    111. width: 29%;
    112. height: 60rpx;
    113. text-align: center;
    114. line-height: 60rpx;
    115. font-size: 28rpx;
    116. background-color: #F7F7F7;
    117. color: #999;
    118. border: 1rpx solid #F7F7F7;
    119. border-radius: 10rpx;
    120. }
    121. .quick_cell_flag{
    122. z-index: 12;
    123. margin: 2%;
    124. padding: 5rpx;
    125. width: 29%;
    126. height: 60rpx;
    127. line-height: 60rpx;
    128. font-size: 28rpx;
    129. text-align: center;
    130. background-color: #fff;
    131. color: #19b2ff;
    132. border: 1rpx solid #19b2ff;
    133. border-radius: 10rpx;
    134. }
    135. .date_picker{
    136. margin-top: 30rpx;
    137. margin-left: 10%;
    138. display: flex;
    139. justify-content: space-between;
    140. align-items: center;
    141. width: 80%;
    142. }
    143. .picker{
    144. padding: 10rpx 40rpx;
    145. background-color: #fff;
    146. font-size: 30rpx;
    147. color: #19b2ff;
    148. font-weight: bold;
    149. }
    150. .sure_btn{
    151. margin-top: 80rpx;
    152. font-size: 28rpx;
    153. width: 90%;
    154. height: 80rpx;
    155. text-align: center;
    156. line-height: 80rpx;
    157. background-color: #19b2ff;
    158. color: #fff;
    159. }
    160. js:
    161. const util = require('../../utils/util.js');
    162. import {
    163. formatDate
    164. } from "../../utils/date";
    165. const app = getApp();
    166. Component({
    167. lifetimes: {
    168. attached: function () {
    169. // 在组件实例进入页面节点树时执行
    170. var currentDate = new Date();
    171. // 获取当前年份和月份
    172. var currentYear = currentDate.getFullYear();
    173. // 计算五年内的上学期和下学期
    174. var semesters = [];
    175. for (var i = 0; i < 5; i++) {
    176. var years = currentYear - i;
    177. semesters.push({
    178. name: years + '下半学年',
    179. dates: [years + '-09-01', (years + 1) + '-01-31'],
    180. flag: false
    181. });
    182. semesters.push({
    183. name: years + '上半学年',
    184. dates: [years + '-02-01', years + '-06-30'],
    185. flag: false
    186. });
    187. }
    188. // 输出结果
    189. this.setData({
    190. dateList: semesters
    191. })
    192. },
    193. },
    194. properties: {},
    195. /**
    196. * 组件的初始数据
    197. */
    198. data: {
    199. dateList: [],
    200. startDate: '',
    201. endDate: '',
    202. currentTime: formatDate(new Date()),
    203. oidx: null,
    204. },
    205. methods: {
    206. /**
    207. * 快速选择
    208. * @param {*} e
    209. */
    210. handleQuickChoose(e) {
    211. let that = this;
    212. let oidx = e.currentTarget.dataset.index;
    213. let odateList = that.data.dateList
    214. // 遍历数组,并修改flag属性为false
    215. odateList.forEach(item => {
    216. item.flag = false;
    217. });
    218. odateList[oidx].flag = true
    219. that.setData({
    220. startDate: odateList[oidx].dates[0],
    221. endDate: odateList[oidx].dates[1],
    222. oidx,
    223. dateList: odateList
    224. })
    225. },
    226. bindDateChange(e) {
    227. //console.log('picker发送选择改变,携带值为', e)
    228. let that = this;
    229. let type = e.currentTarget.dataset.type
    230. if (type == 1) {
    231. that.setData({
    232. startDate: e.detail.value
    233. })
    234. } else {
    235. that.setData({
    236. endDate: e.detail.value
    237. })
    238. }
    239. },
    240. /**
    241. * 顶部关闭按钮
    242. */
    243. onClose() {
    244. this.triggerEvent('sync', {
    245. show: false
    246. })
    247. },
    248. /**
    249. * 确定
    250. */
    251. handleSure() {
    252. let that = this;
    253. if (!that.data.startDate || !that.data.endDate) {
    254. util.alert('学期选择不能为空!');
    255. return false;
    256. }
    257. if (that.data.startDate <= that.data.endDate) {
    258. that.triggerEvent('sync', {
    259. show: false,
    260. startDate: that.data.startDate,
    261. endDate: that.data.endDate
    262. })
    263. } else {
    264. util.alert('结束时间不能小于开始时间');
    265. return false;
    266. }
    267. }
    268. },
    269. })
    270. json:
    271. {
    272. "component": true,
    273. "usingComponents": {}
    274. }

    调用的页面:

    1. json:
    2. "usingComponents": {
    3. "datePicker":"../../common/datePicker/index"
    4. },
    5. html:
    6. <view class="check-list">
    7. <view class="check-list-lef">学期选择view>
    8. <view class="check-list-rig">
    9. <view class="picker" bind:tap="openDatePicker">{{ startDate && endDate ? (startDate + ' - ' + endDate) : '请选择学期'}}view>
    10. <datePicker wx:if="{{show}}" bind:sync="syncGetDate">datePicker>
    11. view>
    12. view>
    13. js:
    14. data:{
    15. startDate: '',
    16. endDate: '',
    17. show: false,//显示隐藏时间控件
    18. }
    19. openDatePicker(e) {
    20. this.setData({
    21. hiddenChart: true,
    22. show:true
    23. })
    24. },
    25. syncGetDate(e){
    26. let that = this;
    27. let show = e.detail.show;
    28. if(!show){ // 子组件点击了关闭 关闭弹窗
    29. that.setData({
    30. show:false,
    31. startDate: e.detail.startDate,
    32. endDate: e.detail.endDate,
    33. //hiddenChart: false,
    34. })
    35. }
    36. //console.log("子组件:",e);
    37. //this.getStatisticThemeActivitiesTrend()
    38. },

  • 相关阅读:
    QFontDialog开发详解
    全面解析缓存应用经典问题
    基于AOP的动态数据源切换(附源码)
    python 协程 (概念+示例代码)
    使用腾讯云 Cloud studio 实现调度百度AI实现文字识别
    【无需卸载,丝滑关闭奇安信天擎开机自启动(步骤超简单)】
    考研机试题
    MySQL数据库基础(数据类型与表操作)
    vs2017 配置 opencv
    场景应用:用SpringBoot手写一个登录拦截器
  • 原文地址:https://blog.csdn.net/qq_27806371/article/details/132639937