• uni-app yrkDataPicker 日期和时间选择控件


    uni-app 选择日期时间控件有 2 月份有 31 天的问题,一直没有修复,uni-calendar 苹果有选择年份和月份后无法显示问题。自己写了一个,只支持 H5 和微信小程序,其他没有试过。

    1. <template>
    2. <view class="yrk-data-picker">
    3. <uni-popup ref="pickerView" type="bottom" style="z-index: 9999999;">
    4. <view class="popup-view">
    5. <view class="popup-view-header">
    6. <view class="popup-view-cancel" @click="pickerCancel">取消</view>
    7. <view class="popup-view-confirm" @click="pickerConfirm">完成</view>
    8. </view>
    9. <picker-view v-if="visible" :indicator-style="indicatorStyle" :value="values" @change="bindChange" class="picker-view">
    10. <picker-view-column :style="dateStyle">
    11. <view class="item" v-for="(item,index) in years" :key="index">{{formatTime(item)}}{{labels[0]}}</view>
    12. </picker-view-column>
    13. <picker-view-column :style="dateStyle">
    14. <view class="item" v-for="(item,index) in months" :key="index">{{formatTime(item)}}{{labels[1]}}</view>
    15. </picker-view-column>
    16. <picker-view-column :style="dateStyle">
    17. <view class="item" v-for="(item,index) in days" :key="index">{{formatTime(item)}}{{labels[2]}}</view>
    18. </picker-view-column>
    19. <picker-view-column :style="timeStyle">
    20. <view class="item" v-for="(item,index) in hours" :key="index">{{formatTime(item)}}{{labels[3]}}</view>
    21. </picker-view-column>
    22. <picker-view-column :style="timeStyle">
    23. <view class="item" v-for="(item,index) in minutes" :key="index">{{formatTime(item)}}{{labels[4]}}</view>
    24. </picker-view-column>
    25. </picker-view>
    26. </view>
    27. </uni-popup>
    28. </view>
    29. </template>
    30. <script>
    31. const da = new Date()
    32. /**
    33. * yrkDataPicker 日期和时间选择控件
    34. * @description 日期和时间选择控件
    35. * @copyright https://my.oschina.net/lovelong1/
    36. * @property {Number} startYear 年份开始日期,默认1900
    37. * @property {Number} endYear 年份结束日期 默认当前年份
    38. * @property {String} mode = ['date','time','datetime'] 选择模式,日期,时间,日期时间,不包含秒针。
    39. * @property {String} value 默认日期 格式,'1900-01-01 01:01:01' 默认当前,目前只支持全
    40. * @event {Function} change 控件更换 参数 event 参考 picker-view change
    41. * @event {Function} cancel 选择取消按钮
    42. * @event {Function} confirm 选择确认按钮 参数 格式化日期 Date数据
    43. * @example <yrk-data-picker ref="pickerView" mode="time" value="1981-05-01 23:01:01" @confirm="pickerConfirm"></yrk-data-picker>
    44. * 显示控件 this.$refs.pickerView.open('bottom');
    45. * 关闭控件 this.$refs.pickerView.close();
    46. * pickerConfirm(dateStr,da){ console.log(dataStr,da); }
    47. */
    48. export default {
    49. name:'yrkDataPicker',
    50. props: {
    51. startYear: { type: Number, default: 1900 },
    52. endYear: { type: Number, default: da.getFullYear() },
    53. mode: { type: String, default: 'time' },
    54. value: { type: String, default: null },
    55. },
    56. computed: {
    57. // 解决微信不显示问题。
    58. dateStyle() { return this.getStyle(['date','datetime']) },
    59. timeStyle() { return this.getStyle(['time','datetime']) }
    60. },
    61. watch: {
    62. mode:{
    63. immediate:true,
    64. handler(newValue, oldValue){this.init()}
    65. },
    66. value:{
    67. immediate:true,
    68. handler(newValue, oldValue){this.init()}
    69. }
    70. },
    71. data() {
    72. return {
    73. visible: true,
    74. tempvalues:[],//临时保存数据
    75. values:[],
    76. defaults:[
    77. {start:1900,end:da.getFullYear()},
    78. {start:1,end:12},
    79. {start:1,end:31},
    80. {start:0,end:23},
    81. {start:0,end:59}
    82. ],
    83. labels:['年','月','日','点','分'],
    84. keys:['years','months','days','hours','minutes'],
    85. years:[],
    86. months :[],
    87. days :[],
    88. hours:[],
    89. minutes:[],
    90. indicatorStyle: ``,
    91. }
    92. },
    93. created() {
    94. this.defaults[0]={start:this.startYear,end:this.endYear};
    95. this.defaults.forEach((item,index)=>{
    96. for(let i = item.start;i<=item.end;i++){
    97. this[this.keys[index]].push(i);
    98. }
    99. })
    100. },
    101. methods: {
    102. getStyle(modes){
    103. const isShow = modes.includes(this.mode);
    104. return `flex:${isShow?1:0};width:${isShow?'auto':'0px'};`
    105. },
    106. init(){
    107. this.$nextTick(()=>{
    108. let year = da.getFullYear();
    109. let month = da.getMonth();
    110. let day = da.getDate();
    111. let hour = da.getHours();
    112. let minute = da.getMinutes();
    113. if(this.value){
    114. const val = this.parse(this.value);
    115. year = val.getFullYear();
    116. month = val.getMonth();
    117. day = val.getDate();
    118. hour = val.getHours();
    119. minute = val.getMinutes();
    120. }
    121. year = year - this.startYear;
    122. this.values = [0, 0, 0, 0, 0];
    123. setTimeout(()=>{
    124. this.values = [year, month, day-1,hour,minute];
    125. },0)
    126. this.upDays(year, month);
    127. })
    128. },
    129. parse: function(str) {
    130. var a = str.split(/[^0-9]/);
    131. return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
    132. },
    133. open(e){
    134. this.$refs.pickerView.open(e);
    135. },
    136. close(){
    137. this.$refs.pickerView.close();
    138. this.$emit('close')
    139. },
    140. pickerCancel() {
    141. this.$refs.pickerView.close();
    142. this.$emit('cancel')
    143. },
    144. formatTime(t){
    145. return (t<10?`0`:'')+t
    146. },
    147. pickerConfirm() {
    148. this.values = this.tempvalues;
    149. let year = this.formatTime(this.years[this.values[0]]);
    150. let month = this.formatTime(this.months[this.values[1]]) ;
    151. let day = this.formatTime(this.days[this.values[2]]) ;
    152. let hour = this.formatTime(this.hours[this.values[3]]);
    153. let minute = this.formatTime(this.minutes[this.values[4]]) ;
    154. let value = `${year}-${month}-${day} ${hour}:${minute}:00`
    155. if(this.mode==='date'){
    156. value = `${year}-${month}-${day}`
    157. }else if(this.mode==='time'){
    158. value = `${hour}:${minute}`
    159. }
    160. this.$refs.pickerView.close();
    161. // console.log(value ,new Date(year,month-1,day,hour,minute,0))
    162. this.$emit('confirm', value ,new Date(year,month-1,day,hour,minute,0))
    163. },
    164. bindChange(e){
    165. // console.log(e);
    166. if(this.values[1] != e.detail.value[1]){
    167. const year = e.detail.value[0]+this.startYear;
    168. const month = e.detail.value[1]+1;
    169. this.upDays(year, month);
    170. }
    171. this.tempvalues = e.detail.value;
    172. this.$emit('change', e)
    173. },
    174. upDays(year,month){
    175. const monthDays = this.getMonthDaysByYearMonth(year,month);
    176. this.days = [];
    177. for (let i = 1; i <= monthDays; i++) this.days.push(i);
    178. },
    179. getMonthDaysByDate(da){
    180. return this.getMonthDaysByYearMonth(da.getFullYear(), da.getMonth);
    181. },
    182. getMonthDaysByYearMonth(year=1900,month=0){
    183. return new Date(year, month,0).getDate();
    184. }
    185. }
    186. }
    187. </script>
    188. <style lang="scss">
    189. .popup-view {
    190. background-color: #FFFFFF;
    191. .popup-view-header {
    192. display: flex;
    193. flex-direction: row;
    194. justify-content: space-between;
    195. align-items: center;
    196. border-bottom: 1px solid #F5F5F5;
    197. .popup-view-cancel,.popup-view-confirm {
    198. box-sizing: border-box;
    199. padding: 0 28rpx;
    200. font-size: 34rpx;
    201. line-height: 90rpx;
    202. }
    203. .popup-view-cancel{
    204. color: #888888;
    205. }
    206. .popup-view-confirm{
    207. color: #007aff;
    208. }
    209. }
    210. .picker-view {
    211. height: 476rpx;
    212. margin-top: 20rpx;
    213. .item{
    214. text-align: center;
    215. display: flex;
    216. flex-direction: column;
    217. align-items: center;
    218. justify-content: center;
    219. }
    220. }
    221. picker-view-column{
    222. // display: flex;
    223. }
    224. }
    225. </style>

  • 相关阅读:
    Unity开发者3D模型基础
    从0开始python学习-34.pytest常用插件
    卷积输入输出维度计算公式,Conv, Dilation Conv, Padding, Kernel_size, Output的维度计算关系
    uniapp——项目day04
    如何保护人工智能隐私?
    树莓 LUMA-OLED.EXAMPLE使用
    idea如何隐藏background tasks
    【从零开始学习 SystemVerilog】5.1、SystemVerilog 通信—— 进程间通信
    给 Pod 添加 DNS 记录
    Objective-C和C/C++混合编译
  • 原文地址:https://blog.csdn.net/lovelong1981/article/details/133917778