|
@@ -1,86 +1,88 @@
|
|
|
-// commpents/datetime-picker/index.js
|
|
|
-const app = getApp()
|
|
|
+const App = getApp();
|
|
|
+const dateTimePicker = require('../../utils/datePicker');
|
|
|
const moment = require("../../utils/moment.min");
|
|
|
Component({
|
|
|
- /**
|
|
|
- * 组件的属性列表
|
|
|
- */
|
|
|
- options: { multipleSlots: true },
|
|
|
+ options: { addGlobalClass: true },
|
|
|
properties: {
|
|
|
datetime: { type: String, value: '' },
|
|
|
name: { type: String, value: '' }
|
|
|
},
|
|
|
- // 生命周期函数,可以为函数,或一个在methods段中定义的方法名
|
|
|
- attached: function () { }, // 此处attached的声明会被lifetimes字段中的声明覆盖
|
|
|
- ready: function () { },
|
|
|
+ data: {
|
|
|
+ // 时间列表
|
|
|
+ dateTimeArray: null,
|
|
|
+ // 默认显示时间
|
|
|
+ valueDateTime: null,
|
|
|
+ // 开始时间
|
|
|
+ startDateTime: '',
|
|
|
+ // 结束时间
|
|
|
+ endDateTime: '',
|
|
|
+ },
|
|
|
pageLifetimes: {
|
|
|
- // 组件所在页面的生命周期函数
|
|
|
show: function () {
|
|
|
const that = this;
|
|
|
that.search();
|
|
|
+
|
|
|
+
|
|
|
},
|
|
|
hide: function () { },
|
|
|
- resize: function () { },
|
|
|
- },
|
|
|
- /**
|
|
|
- * 组件的初始数据
|
|
|
- */
|
|
|
- data: {
|
|
|
- dateTimeArray: [],
|
|
|
- // 计算日期
|
|
|
- year: '',
|
|
|
- moneh: '',
|
|
|
+ resize: function (size) { }
|
|
|
},
|
|
|
/**
|
|
|
* 组件的方法列表
|
|
|
*/
|
|
|
methods: {
|
|
|
- // 查询时间
|
|
|
search: function () {
|
|
|
const that = this;
|
|
|
- // 计算年份
|
|
|
- let year = [];
|
|
|
- let startYear = moment().format('YYYY');
|
|
|
- let endYear = Number(startYear) + 100;
|
|
|
- for (var i = startYear; i <= endYear; i++) { year.push(String(i)) }
|
|
|
- // 数组数据, 年, 月, 日, 时, 分, 秒;
|
|
|
- let dateTimeArray = [
|
|
|
- [...year],
|
|
|
- ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'],
|
|
|
- ['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'],
|
|
|
- ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23'],
|
|
|
- ['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'],
|
|
|
- ['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'],
|
|
|
- ]
|
|
|
- that.setData({ dateTimeArray })
|
|
|
+ // 计算时间区间
|
|
|
+ let startDateTime = moment().format('YYYY-MM-DD HH:mm');
|
|
|
+ let endDateTime = moment().add(100, 'years').format('YYYY-MM-DD HH:mm');
|
|
|
+ that.setData({ startDateTime: startDateTime, endDateTime: endDateTime })
|
|
|
+ // 获取完整的年月日 时分秒,以及默认显示的数组
|
|
|
+ that.data.unit = ['年', '月', '日', '时', '分']
|
|
|
+ that.data.dateTimePicker = dateTimePicker.newDateTimePicker(startDateTime, endDateTime)
|
|
|
+ let obj = that.data.dateTimePicker.render();
|
|
|
+ let lastArray = obj.dateTimeArray;
|
|
|
+ let lastTime = obj.dateTime;
|
|
|
+ for (let i = 0; i < lastArray.length; i++) {
|
|
|
+ lastArray[i] = lastArray[i].map(m => m + this.data.unit[i])
|
|
|
+ }
|
|
|
+ that.setData({ dateTimeArray: lastArray, valueDateTime: lastTime })
|
|
|
},
|
|
|
// 确认选择
|
|
|
- change: function (e) {
|
|
|
+ change(e) {
|
|
|
const that = this;
|
|
|
- let dateTimeArray = that.data.dateTimeArray;
|
|
|
- let value = e.detail.value;
|
|
|
- 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]];
|
|
|
- that.triggerEvent('datetimeChange', { datetime: datetime, name: that.properties.name })
|
|
|
+ const valueDateTime = that.data.valueDateTime;
|
|
|
+ const year = that.data.dateTimeArray[0][valueDateTime[0]].replace(/年/, '')
|
|
|
+ const month = that.data.dateTimeArray[1][valueDateTime[1]].replace(/月/, '')
|
|
|
+ const day = that.data.dateTimeArray[2][valueDateTime[2]].replace(/日/, '')
|
|
|
+ const hour = that.data.dateTimeArray[3][valueDateTime[3]].replace(/时/, '')
|
|
|
+ const minute = that.data.dateTimeArray[4][valueDateTime[4]].replace(/分/, '')
|
|
|
+ let dateTimeWhole = `${year}-${month}-${day} ${hour}:${minute}`;
|
|
|
+ that.triggerEvent('datetimeChange', { datetime: dateTimeWhole, name: that.properties.name })
|
|
|
},
|
|
|
- // 选择月份
|
|
|
+ // 选择时间
|
|
|
columnChange: function (e) {
|
|
|
const that = this;
|
|
|
- const { column, value } = e.detail;
|
|
|
- let dateTimeArray = that.data.dateTimeArray;
|
|
|
- let year = '';
|
|
|
- let month = '';
|
|
|
- year = that.data.year || dateTimeArray[0][0];
|
|
|
- month = that.data.month || dateTimeArray[1][0];
|
|
|
- if (column == '0') year = dateTimeArray[0][value];
|
|
|
- if (column == '1') month = dateTimeArray[1][value];
|
|
|
- that.setData({ year: year, month: month });
|
|
|
- // 计算当前选择年月所要显示的日期
|
|
|
- let ym = that.data.year + '-' + that.data.month;
|
|
|
- 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'];
|
|
|
- let monthDay = moment(ym).daysInMonth();
|
|
|
- let thr = test.splice(0, monthDay);
|
|
|
- that.setData({ 'dateTimeArray[2]': thr })
|
|
|
+ // 时间列表
|
|
|
+ const dateTimeArray = that.data.dateTimeArray;
|
|
|
+ const { column, value } = e.detail
|
|
|
+ let dateTimeTemp = 'valueDateTime[' + column + ']'
|
|
|
+ that.setData({ [dateTimeTemp]: value })
|
|
|
+ // 当前选择的值
|
|
|
+ const valueDateTime = that.data.valueDateTime;
|
|
|
+ that.data.dateTimePicker.setValue({ dateTimeArray: dateTimeArray, dateTime: valueDateTime })
|
|
|
+ for (let i = 1; i < valueDateTime.length; i++) {
|
|
|
+ if (column == i - 1) {
|
|
|
+ for (let j = i; j < valueDateTime.length; j++) {
|
|
|
+ let temp = 'valueDateTime[' + j + ']'
|
|
|
+ that.setData({ [temp]: 0 })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ let arr = that.data.dateTimePicker.dispatch(i).map(m => m + that.data.unit[i])
|
|
|
+ let temp1 = 'dateTimeArray[' + i + ']';
|
|
|
+ that.setData({ [temp1]: arr })
|
|
|
+ }
|
|
|
+ that.setData({ dateTimeArray: dateTimeArray, valueDateTime: valueDateTime })
|
|
|
},
|
|
|
}
|
|
|
-})
|
|
|
-
|
|
|
+})
|