datePicker.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. if(!e){
  78. this.setData({
  79. dateTimeWhole: '',
  80. "params.pText":''
  81. })
  82. this.triggerEvent('getDateString', this.data.dateTimeWhole)
  83. return;
  84. }
  85. this.data.dateTime = e.detail.value
  86. const year = this.data.dateTimeArray[0][this.data.dateTime[0]].replace(/年/, '')
  87. const month = this.data.dateTimeArray[1][this.data.dateTime[1]].replace(/月/, '')
  88. const day = this.data.dateTimeArray[2][this.data.dateTime[2]].replace(/日/, '')
  89. const hour = this.data.dateTimeArray[3][this.data.dateTime[3]].replace(/时/, '')
  90. const minute = this.data.dateTimeArray[4][this.data.dateTime[4]].replace(/分/, '')
  91. // const second = this.data.dateTimeArray[5][this.data.dateTime[4]].replace(/秒/, '')
  92. this.data.dateTimeWhole = `${year}-${month}-${day} ${hour}:${minute}`
  93. // this.data.dateTimeWhole = `${year}-${month}-${day}`
  94. this.setData({
  95. dateTimeWhole: this.data.dateTimeWhole,
  96. })
  97. // console.log(this.data.dateTimeWhole)
  98. this.triggerEvent('getDateString', this.data.dateTimeWhole)
  99. },
  100. changeDateTimeColumn(e) {
  101. const { column, value } = e.detail
  102. // this.$set(this.data.dateTime, column, value)
  103. let dateTimeTemp = 'dateTime['+column+']'
  104. this.setData({
  105. [dateTimeTemp]: value
  106. })
  107. this.data.dateTimePicker.setValue({ dateTimeArray: this.data.dateTimeArray, dateTime: this.data.dateTime })
  108. for (let i = 1; i < this.data.dateTime.length; i++) {
  109. if (column == i - 1) {
  110. for (let j = i; j < this.data.dateTime.length; j++) {
  111. // this.$set(this.data.dateTime, j, 0)
  112. let temp = 'dateTime['+j+']'
  113. this.setData({
  114. [temp]: 0
  115. })
  116. }
  117. }
  118. let arr = this.data.dateTimePicker.dispatch(i).map(m => m + this.data.unit[i])
  119. // this.$set(this.data.dateTimeArray, i, arr)
  120. let temp1 = 'dateTimeArray['+i+']'
  121. this.setData({
  122. [temp1]: arr
  123. })
  124. }
  125. this.setData({
  126. dateTimeArray: this.data.dateTimeArray,
  127. dateTime: this.data.dateTime
  128. })
  129. },
  130. }
  131. })