index.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // commpents/datetime-picker/index.js
  2. const app = getApp()
  3. const moment = require("../../utils/moment.min");
  4. Component({
  5. /**
  6. * 组件的属性列表
  7. */
  8. options: { multipleSlots: true },
  9. properties: {
  10. datetime: { type: String, value: '' },
  11. fieldValue: { type: String, value: '' }
  12. },
  13. // 生命周期函数,可以为函数,或一个在methods段中定义的方法名
  14. attached: function () { }, // 此处attached的声明会被lifetimes字段中的声明覆盖
  15. ready: function () { },
  16. pageLifetimes: {
  17. // 组件所在页面的生命周期函数
  18. show: function () { },
  19. hide: function () { },
  20. resize: function () { },
  21. },
  22. /**
  23. * 组件的初始数据
  24. */
  25. data: {
  26. dateTimeArray: [
  27. ['2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010'],
  28. ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'],
  29. ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31'],
  30. ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23'],
  31. ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59'],
  32. ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59'],
  33. ],
  34. // 计算日期
  35. year: '',
  36. moneh: '',
  37. },
  38. /**
  39. * 组件的方法列表
  40. */
  41. methods: {
  42. // 确认选择
  43. change: function (e) {
  44. const that = this;
  45. let dateTimeArray = that.data.dateTimeArray;
  46. let value = e.detail.value;
  47. let datetime = dateTimeArray[0][value[0]] + '-' + dateTimeArray[1][value[1]] + '-' + dateTimeArray[2][value[2]] + ' ' + dateTimeArray[3][value[3]] + ':' + dateTimeArray[4][value[4]] + ':' + dateTimeArray[5][value[5]];
  48. that.triggerEvent('datetimeChange', { datetime: datetime, fieldValue: that.properties.fieldValue })
  49. },
  50. // 选择月份
  51. columnChange: function (e) {
  52. const that = this;
  53. const { column, value } = e.detail;
  54. let dateTimeArray = that.data.dateTimeArray;
  55. let year = '';
  56. let month = '';
  57. year = that.data.year || dateTimeArray[0][0];
  58. month = that.data.month || dateTimeArray[1][0];
  59. if (column == '0') year = dateTimeArray[0][value];
  60. if (column == '1') month = dateTimeArray[1][value];
  61. that.setData({ year: year, month: month });
  62. // 计算当前选择年月所要显示的日期
  63. let ym = that.data.year + '-' + that.data.month;
  64. let test = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31'];
  65. let monthDay = moment(ym).daysInMonth();
  66. let thr = test.splice(0, monthDay);
  67. that.setData({ 'dateTimeArray[2]': thr })
  68. },
  69. }
  70. })