datePicker.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. const App = getApp();
  2. const dateTimePicker = require('../../utils/datePicker.js')
  3. Component({
  4. options: {
  5. addGlobalClass: true,
  6. },
  7. /**
  8. * 组件的属性列表
  9. */
  10. properties: {
  11. params: {
  12. type: Object,
  13. value:{
  14. placeholder: '请选择时间',
  15. startDateTime: '',
  16. endDateTime: '',
  17. pText: ''
  18. }
  19. },
  20. },
  21. /**
  22. * 组件的初始数据
  23. */
  24. data: {
  25. dateTimeArray: null,
  26. dateTime: null,
  27. startDateTime: '',
  28. endDateTime: '',
  29. dateTimeWhole: '',
  30. },
  31. lifetimes: {
  32. attached: function () {
  33. this.setData({
  34. startDateTime: this.data.params.startDateTime,
  35. endDateTime: this.data.params.endDateTime
  36. })
  37. this.initData()
  38. }
  39. },
  40. pageLifetimes: {
  41. show: function() {
  42. this.setData({
  43. startDateTime: this.data.params.startDateTime,
  44. endDateTime: this.data.params.endDateTime
  45. })
  46. this.initData()
  47. },
  48. hide: function() {
  49. // 页面被隐藏
  50. },
  51. resize: function(size) {
  52. // 页面尺寸变化
  53. }
  54. },
  55. /**
  56. * 组件的方法列表
  57. */
  58. methods: {
  59. initData(date) {
  60. // 获取完整的年月日 时分秒,以及默认显示的数组
  61. this.data.unit = ['年', '月', '日', '时', '分']
  62. this.data.dateTimePicker = dateTimePicker.newDateTimePicker(this.data.startDateTime, this.data.endDateTime, this.data.params.pText)
  63. let obj = this.data.dateTimePicker.render();
  64. let lastArray = obj.dateTimeArray;
  65. let lastTime = obj.dateTime;
  66. for (let i = 0; i < lastArray.length; i++) {
  67. lastArray[i] = lastArray[i].map(m => m + this.data.unit[i])
  68. }
  69. this.data.dateTimeArray = lastArray
  70. this.data.dateTime = lastTime
  71. this.setData({
  72. dateTimeArray: this.data.dateTimeArray,
  73. dateTime: this.data.dateTime
  74. })
  75. },
  76. changeDateTime(e) {
  77. this.data.dateTime = e.detail.value
  78. const year = this.data.dateTimeArray[0][this.data.dateTime[0]].replace(/年/, '')
  79. const month = this.data.dateTimeArray[1][this.data.dateTime[1]].replace(/月/, '')
  80. const day = this.data.dateTimeArray[2][this.data.dateTime[2]].replace(/日/, '')
  81. const hour = this.data.dateTimeArray[3][this.data.dateTime[3]].replace(/时/, '')
  82. const minute = this.data.dateTimeArray[4][this.data.dateTime[4]].replace(/分/, '')
  83. // const second = this.data.dateTimeArray[5][this.data.dateTime[4]].replace(/秒/, '')
  84. this.data.dateTimeWhole = `${year}-${month}-${day} ${hour}:${minute}`
  85. // this.data.dateTimeWhole = `${year}-${month}-${day}`
  86. this.setData({
  87. dateTimeWhole: this.data.dateTimeWhole,
  88. })
  89. // console.log(this.data.dateTimeWhole)
  90. this.triggerEvent('getDateString', this.data.dateTimeWhole)
  91. },
  92. changeDateTimeColumn(e) {
  93. const { column, value } = e.detail
  94. // this.$set(this.data.dateTime, column, value)
  95. let dateTimeTemp = 'dateTime['+column+']'
  96. this.setData({
  97. [dateTimeTemp]: value
  98. })
  99. this.data.dateTimePicker.setValue({ dateTimeArray: this.data.dateTimeArray, dateTime: this.data.dateTime })
  100. for (let i = 1; i < this.data.dateTime.length; i++) {
  101. if (column == i - 1) {
  102. for (let j = i; j < this.data.dateTime.length; j++) {
  103. // this.$set(this.data.dateTime, j, 0)
  104. let temp = 'dateTime['+j+']'
  105. this.setData({
  106. [temp]: 0
  107. })
  108. }
  109. }
  110. let arr = this.data.dateTimePicker.dispatch(i).map(m => m + this.data.unit[i])
  111. // this.$set(this.data.dateTimeArray, i, arr)
  112. let temp1 = 'dateTimeArray['+i+']'
  113. this.setData({
  114. [temp1]: arr
  115. })
  116. }
  117. this.setData({
  118. dateTimeArray: this.data.dateTimeArray,
  119. dateTime: this.data.dateTime
  120. })
  121. },
  122. }
  123. })