picker.vue
6.92 KB
1
2
3
4
5
6
7
8
9
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<template>
<picker
mode="multiSelector"
:range-key="'name'"
:value="timeIndex"
:range="activityArray"
:disabled="disabled"
@change="bindMultiPickerChange"
@columnChange="bindMultiPickerColumnChange"
>
<slot />
</picker>
</template>
<script>
import { dateTimePicker, dateDate } from './dateTimePicker.js'
export default {
name: 'TimePickerDataPicker',
props: {
startTime: {
type: [Object, Date],
default: new Date()
},
endTime: {
type: [Object, Date],
default: new Date()
},
defaultTime: {
type: [Object, Date],
default: new Date()
},
disabled: {
type: Boolean,
default: false
}
},
data() {
return {
timeIndex: [0, 0, 0, 0, 0],
activityArray: [],
year: 0,
month: 1,
day: 1,
hour: 0,
minute: 0,
datePicker: '',
defaultIndex: [0, 0, 0, 0, 0],
startIndex: [0, 0, 0, 0, 0],
endIndex: [0, 0, 0, 0, 0]
}
},
computed: {
timeDate() {
const { startTime, endTime } = this
return { startTime, endTime }
}
},
watch: {
timeDate() {
this.initData()
},
defaultTime() {
this.initData()
}
},
created() {
this.initData()
},
methods: {
initData() {
const startTime = this.startTime
const endTime = this.endTime
this.datePicker = dateTimePicker(startTime.getFullYear(), endTime.getFullYear())
this.setDateData(this.defaultTime)
this.getKeyIndex(this.startTime, 'startIndex')
// 截止时间索引
this.getKeyIndex(this.endTime, 'endIndex')
// 默认索引
this.getKeyIndex(this.defaultTime, 'defaultIndex')
this.timeIndex = this.defaultIndex
// 初始时间
this.initTime()
},
getKeyIndex(time, key) {
const Arr = dateDate(time)
const _index = this.getIndex(Arr)
this[key] = _index
},
getIndex(arr) {
const timeIndex = []
const indexKey = ['year', 'month', 'day', 'hours', 'minutes']
this.activityArray.forEach((element, index) => {
const _index = element.findIndex(
item => parseInt(item.id) === parseInt(arr[indexKey[index]])
)
timeIndex[index] = _index >= 0 ? _index : 0
})
return timeIndex
},
initTime() {
const _index = this.timeIndex
this.year = this.activityArray[0][_index[0]].id
this.month = this.activityArray[1].length && this.activityArray[1][_index[1]].id
this.day = this.activityArray[2].length && this.activityArray[2][_index[2]].id
this.hour = this.activityArray[3].length && this.activityArray[3][_index[3]].id
this.minute = this.activityArray[4].length && this.activityArray[4][_index[4]].id
},
setDateData(_date) {
const _data = dateDate(_date)
this.activityArray = this.datePicker(_data.year, _data.month)
},
bindMultiPickerChange(e) {
console.log('picker发送选择改变,携带值为', e.detail.value)
const activityArray = JSON.parse(JSON.stringify(this.activityArray)),
{ value } = e.detail,
_result = []
for (let i = 0; i < value.length; i++) {
_result[i] = activityArray[i][value[i]].id
}
this.$emit('result', _result)
},
bindMultiPickerColumnChange(e) {
console.log('修改的列为', e.detail.column, ',值为', e.detail.value)
let _data = JSON.parse(JSON.stringify(this.activityArray)),
timeIndex = JSON.parse(JSON.stringify(this.timeIndex)),
{ startIndex, endIndex } = this,
{ column, value } = e.detail,
_value = _data[column][value].id,
_start = dateDate(this.startTime),
_end = dateDate(this.endTime)
switch (e.detail.column) {
case 0:
if (_value <= _start.year) {
timeIndex = startIndex
this.year = _start.year
this.setDateData(this.startTime)
} else if (_value >= _end.year) {
this.year = _end.year
timeIndex = [endIndex[0], 0, 0, 0, 0]
this.setDateData(this.endTime)
} else {
this.year = _value
timeIndex = [value, 0, 0, 0, 0]
this.activityArray = this.datePicker(_value, 1)
}
timeIndex = this.timeIndex = JSON.parse(JSON.stringify(timeIndex))
this.timeIndex = timeIndex
break
case 1:
if (this.year == _start.year && value <= startIndex[1]) {
timeIndex = startIndex
this.month = _start.month
this.setDateData(this.startTime)
} else if (this.year == _end.year && value >= endIndex[1]) {
timeIndex = endIndex
this.month = _end.month
this.setDateData(this.endTime)
} else {
this.month = _value
_data[2] = this.datePicker(this.year, this.month)[2]
timeIndex = [timeIndex[0], value, 0, 0, 0]
this.activityArray = _data
}
this.timeIndex = JSON.parse(JSON.stringify(timeIndex))
break
case 2:
if (this.year == _start.year && this.month == _start.month && value <= startIndex[2]) {
this.day = _start.day
timeIndex = startIndex
} else if (this.year == _end.year && this.month == _end.month && value >= endIndex[2]) {
this.day = _end.day
timeIndex = endIndex
} else {
this.day = _value
timeIndex = [timeIndex[0], timeIndex[1], value, 0, 0]
}
this.timeIndex = JSON.parse(JSON.stringify(timeIndex))
break
case 3:
if (
this.year == _start.year &&
this.month == _start.month &&
this.day == _start.day &&
value <= startIndex[3]
) {
this.hour = _start.hours
timeIndex = startIndex
} else if (
this.year == _end.year &&
this.month == _end.month &&
this.day == _end.day &&
value >= endIndex[3]
) {
this.hour = _end.hours
timeIndex = endIndex
} else {
this.hour = _value
timeIndex[3] = value
timeIndex[4] = 0
}
this.timeIndex = JSON.parse(JSON.stringify(timeIndex))
break
case 4:
timeIndex[4] = value
if (
this.year == _start.year &&
this.month == _start.month &&
this.day == _start.day &&
this.hour == _start.hours &&
value <= startIndex[4]
) {
timeIndex = startIndex
} else if (
this.year == _end.year &&
this.month == _end.month &&
this.day == _end.day &&
this.hour == _end.hours &&
value >= endIndex[4]
) {
timeIndex = endIndex
}
this.timeIndex = JSON.parse(JSON.stringify(timeIndex))
break
}
}
}
}
</script>