uni-datetime-picker.vue 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. <template>
  2. <view class="uni-datetime-picker">
  3. <view @click="initTimePicker">
  4. <slot>
  5. <view class="uni-datetime-picker-timebox-pointer" :class="{'uni-datetime-picker-disabled': disabled, 'uni-datetime-picker-timebox': border}" style="text-align: right">
  6. <text class="uni-datetime-picker-text" >{{time}}</text>
  7. <view v-if="!time" class="uni-datetime-picker-time">
  8. <text class="uni-datetime-picker-text">请选择{{labelText}}时间</text>
  9. </view>
  10. <view style="margin-left: 5px" class="flex">
  11. <u-icon name="arrow-right" color="#909399" size="32"></u-icon>
  12. </view>
  13. </view>
  14. </slot>
  15. </view>
  16. <view v-if="visible" id="mask" class="uni-datetime-picker-mask" @click="tiggerTimePicker"></view>
  17. <view v-if="visible" class="uni-datetime-picker-popup" :class="[dateShow && timeShow ? '' : 'fix-nvue-height']" :style="fixNvueBug">
  18. <view class="uni-title" v-if="dateShow">
  19. <text class="uni-datetime-picker-text">选择{{labelText}}日期</text>
  20. </view>
  21. <view v-if="dateShow" class="uni-datetime-picker__container-box">
  22. <picker-view class="uni-datetime-picker-view" :indicator-style="indicatorStyle" :value="ymd" @change="bindDateChange">
  23. <picker-view-column>
  24. <view class="uni-datetime-picker-item" v-for="(item,index) in years" :key="index">
  25. <text class="uni-datetime-picker-item">{{lessThanTen(item)}}</text>
  26. </view>
  27. </picker-view-column>
  28. <picker-view-column>
  29. <view class="uni-datetime-picker-item" v-for="(item,index) in months" :key="index">
  30. <text class="uni-datetime-picker-item">{{lessThanTen(item)}}</text>
  31. </view>
  32. </picker-view-column>
  33. <picker-view-column>
  34. <view class="uni-datetime-picker-item" v-for="(item,index) in days" :key="index">
  35. <text class="uni-datetime-picker-item">{{lessThanTen(item)}}</text>
  36. </view>
  37. </picker-view-column>
  38. </picker-view>
  39. <!-- 兼容 nvue 不支持伪类 -->
  40. <text class="uni-datetime-picker-sign sign-left">-</text>
  41. <text class="uni-datetime-picker-sign sign-right">-</text>
  42. </view>
  43. <view class="uni-title" v-if="timeShow">
  44. <text class="uni-datetime-picker-text">设置{{labelText}}时间</text>
  45. </view>
  46. <view v-if="timeShow" class="uni-datetime-picker__container-box">
  47. <picker-view class="uni-datetime-picker-view" :class="[hideSecond ? 'time-hide-second' : '']" :indicator-style="indicatorStyle" :value="hms" @change="bindTimeChange">
  48. <picker-view-column>
  49. <view class="uni-datetime-picker-item" v-for="(item,index) in hours" :key="index">
  50. <text class="uni-datetime-picker-item">{{lessThanTen(item)}}</text>
  51. </view>
  52. </picker-view-column>
  53. <picker-view-column>
  54. <view class="uni-datetime-picker-item" v-for="(item,index) in minutes" :key="index">
  55. <text class="uni-datetime-picker-item">{{lessThanTen(item)}}</text>
  56. </view>
  57. </picker-view-column>
  58. <picker-view-column v-if="!hideSecond">
  59. <view class="uni-datetime-picker-item" v-for="(item,index) in seconds" :key="index">
  60. <text class="uni-datetime-picker-item">{{lessThanTen(item)}}</text>
  61. </view>
  62. </picker-view-column>
  63. </picker-view>
  64. <!-- 兼容 nvue 不支持伪类 -->
  65. <text class="uni-datetime-picker-sign" :class="[hideSecond ? 'sign-center' : 'sign-left']">:</text>
  66. <text v-if="!hideSecond" class="uni-datetime-picker-sign sign-right">:</text>
  67. </view>
  68. <view class="uni-datetime-picker-btn">
  69. <view class="uni-datetime-picker-btn-group" v-show="showClear" @click="clearTime">
  70. 清空
  71. </view>
  72. <view class="uni-datetime-picker-btn-group" @click="tiggerTimePicker">
  73. 取消
  74. </view>
  75. <view class="uni-datetime-picker-btn-group" @click="setTime">
  76. 确定
  77. </view>
  78. </view>
  79. </view>
  80. <!-- #ifdef H5 -->
  81. <keypress v-if="visible" @esc="tiggerTimePicker" @enter="setTime" />
  82. <!-- #endif -->
  83. </view>
  84. </template>
  85. <script>
  86. // #ifdef H5
  87. import keypress from './keypress'
  88. // #endif
  89. /**
  90. * DatetimePicker 时间选择器
  91. * @description 可以同时选择日期和时间的选择器
  92. * @tutorial https://ext.dcloud.net.cn/plugin?id=xxx
  93. * @property {String} type = [datetime | date | time] 显示模式
  94. * @property {Boolean} multiple = [true|false] 是否多选
  95. * @property {String|Number} value 默认值
  96. * @property {String|Number} start 起始日期或时间
  97. * @property {String|Number} end 起始日期或时间
  98. * @property {String} return-type = [timestamp | string]
  99. * @event {Function} change 选中发生变化触发
  100. */
  101. export default {
  102. name: 'UniDatetimePicker',
  103. components: {
  104. // #ifdef H5
  105. keypress
  106. // #endif
  107. },
  108. data() {
  109. return {
  110. indicatorStyle: `height: 50px;`,
  111. visible: false,
  112. fixNvueBug: {},
  113. dateShow: true,
  114. timeShow: true,
  115. title: '日期和时间',
  116. // 输入框当前时间
  117. time: '',
  118. // 当前的年月日时分秒
  119. year: 1900,
  120. month: 0,
  121. day: 0,
  122. hour: 0,
  123. minute: 0,
  124. second: 0,
  125. // 起始时间
  126. startYear: 1920,
  127. startMonth: 1,
  128. startDay: 1,
  129. startHour: 0,
  130. startMinute: 0,
  131. startSecond: 0,
  132. // 结束时间
  133. endYear: 2120,
  134. endMonth: 12,
  135. endDay: 31,
  136. endHour: 23,
  137. endMinute: 59,
  138. endSecond: 59,
  139. }
  140. },
  141. props: {
  142. labelText: {
  143. type: String,
  144. default: ''
  145. },
  146. type: {
  147. type: String,
  148. default: 'datetime'
  149. },
  150. value: {
  151. type: [String, Number],
  152. default: ''
  153. },
  154. start: {
  155. type: [Number, String],
  156. default: ''
  157. },
  158. end: {
  159. type: [Number, String],
  160. default: ''
  161. },
  162. returnType: {
  163. type: String,
  164. default: 'string'
  165. },
  166. disabled: {
  167. type: [Boolean, String],
  168. default: false
  169. },
  170. border: {
  171. type: [Boolean, String],
  172. default: true
  173. },
  174. showClear: {
  175. type: [Boolean, String],
  176. default: true
  177. },
  178. hideSecond: {
  179. type: [Boolean, String],
  180. default: true
  181. }
  182. },
  183. watch: {
  184. value: {
  185. handler(newVal, oldVal) {
  186. if (newVal) {
  187. this.parseValue(this.fixIosDateFormat(newVal)) //兼容 iOS、safari 日期格式
  188. this.initTime(false)
  189. } else {
  190. this.parseValue(Date.now())
  191. }
  192. },
  193. immediate: true
  194. },
  195. type: {
  196. handler(newValue) {
  197. if (newValue === 'date') {
  198. this.dateShow = true
  199. this.timeShow = false
  200. this.title = '日期'
  201. } else if (newValue === 'time') {
  202. this.dateShow = false
  203. this.timeShow = true
  204. this.title = '时间'
  205. } else {
  206. this.dateShow = true
  207. this.timeShow = true
  208. this.title = '日期和时间'
  209. }
  210. },
  211. immediate: true
  212. },
  213. start: {
  214. handler(newVal) {
  215. this.parseDatetimeRange(this.fixIosDateFormat(newVal), 'start') //兼容 iOS、safari 日期格式
  216. },
  217. immediate: true
  218. },
  219. end: {
  220. handler(newVal) {
  221. this.parseDatetimeRange(this.fixIosDateFormat(newVal), 'end') //兼容 iOS、safari 日期格式
  222. },
  223. immediate: true
  224. },
  225. // 月、日、时、分、秒可选范围变化后,检查当前值是否在范围内,不在则当前值重置为可选范围第一项
  226. months(newVal) {
  227. this.checkValue('month', this.month, newVal)
  228. },
  229. days(newVal) {
  230. this.checkValue('day', this.day, newVal)
  231. },
  232. hours(newVal) {
  233. this.checkValue('hour', this.hour, newVal)
  234. },
  235. minutes(newVal) {
  236. this.checkValue('minute', this.minute, newVal)
  237. },
  238. seconds(newVal) {
  239. this.checkValue('second', this.second, newVal)
  240. }
  241. },
  242. created() {
  243. this.form = this.getForm('uniForms')
  244. this.formItem = this.getForm('uniFormsItem')
  245. if (this.formItem) {
  246. if (this.formItem.name) {
  247. this.rename = this.formItem.name
  248. this.form.inputChildrens.push(this)
  249. }
  250. }
  251. },
  252. computed: {
  253. // 当前年、月、日、时、分、秒选择范围
  254. years() {
  255. return this.getCurrentRange('year')
  256. },
  257. months() {
  258. return this.getCurrentRange('month')
  259. },
  260. days() {
  261. return this.getCurrentRange('day')
  262. },
  263. hours() {
  264. return this.getCurrentRange('hour')
  265. },
  266. minutes() {
  267. return this.getCurrentRange('minute')
  268. },
  269. seconds() {
  270. return this.getCurrentRange('second')
  271. },
  272. // picker 当前值数组
  273. ymd() {
  274. return [this.year - this.minYear, this.month - this.minMonth, this.day - this.minDay]
  275. },
  276. hms() {
  277. return [this.hour - this.minHour, this.minute - this.minMinute, this.second - this.minSecond]
  278. },
  279. // 当前 date 是 start
  280. currentDateIsStart() {
  281. return this.year === this.startYear && this.month === this.startMonth && this.day === this.startDay
  282. },
  283. // 当前 date 是 end
  284. currentDateIsEnd() {
  285. return this.year === this.endYear && this.month === this.endMonth && this.day === this.endDay
  286. },
  287. // 当前年、月、日、时、分、秒的最小值和最大值
  288. minYear() {
  289. return this.startYear
  290. },
  291. maxYear() {
  292. return this.endYear
  293. },
  294. minMonth() {
  295. if (this.year === this.startYear) {
  296. return this.startMonth
  297. } else {
  298. return 1
  299. }
  300. },
  301. maxMonth() {
  302. if (this.year === this.endYear) {
  303. return this.endMonth
  304. } else {
  305. return 12
  306. }
  307. },
  308. minDay() {
  309. if (this.year === this.startYear && this.month === this.startMonth) {
  310. return this.startDay
  311. } else {
  312. return 1
  313. }
  314. },
  315. maxDay() {
  316. if (this.year === this.endYear && this.month === this.endMonth) {
  317. return this.endDay
  318. } else {
  319. return this.daysInMonth(this.year, this.month)
  320. }
  321. },
  322. minHour() {
  323. if (this.type === 'datetime') {
  324. if (this.currentDateIsStart) {
  325. return this.startHour
  326. } else {
  327. return 0
  328. }
  329. }
  330. if (this.type === 'time') {
  331. return this.startHour
  332. }
  333. },
  334. maxHour() {
  335. if (this.type === 'datetime') {
  336. if (this.currentDateIsEnd) {
  337. return this.endHour
  338. } else {
  339. return 23
  340. }
  341. }
  342. if (this.type === 'time') {
  343. return this.endHour
  344. }
  345. },
  346. minMinute() {
  347. if (this.type === 'datetime') {
  348. if (this.currentDateIsStart && this.hour === this.startHour) {
  349. return this.startMinute
  350. } else {
  351. return 0
  352. }
  353. }
  354. if (this.type === 'time') {
  355. if (this.hour === this.startHour) {
  356. return this.startMinute
  357. } else {
  358. return 0
  359. }
  360. }
  361. },
  362. maxMinute() {
  363. if (this.type === 'datetime') {
  364. if (this.currentDateIsEnd && this.hour === this.endHour) {
  365. return this.endMinute
  366. } else {
  367. return 59
  368. }
  369. }
  370. if (this.type === 'time') {
  371. if (this.hour === this.endHour) {
  372. return this.endMinute
  373. } else {
  374. return 59
  375. }
  376. }
  377. },
  378. minSecond() {
  379. if (this.type === 'datetime') {
  380. if (this.currentDateIsStart && this.hour === this.startHour && this.minute === this.startMinute) {
  381. return this.startSecond
  382. } else {
  383. return 0
  384. }
  385. }
  386. if (this.type === 'time') {
  387. if (this.hour === this.startHour && this.minute === this.startMinute) {
  388. return this.startSecond
  389. } else {
  390. return 0
  391. }
  392. }
  393. },
  394. maxSecond() {
  395. if (this.type === 'datetime') {
  396. if (this.currentDateIsEnd && this.hour === this.endHour && this.minute === this.endMinute) {
  397. return this.endSecond
  398. } else {
  399. return 59
  400. }
  401. }
  402. if (this.type === 'time') {
  403. if (this.hour === this.endHour && this.minute === this.endMinute) {
  404. return this.endSecond
  405. } else {
  406. return 59
  407. }
  408. }
  409. }
  410. },
  411. mounted() {
  412. // #ifdef APP-NVUE
  413. const res = uni.getSystemInfoSync();
  414. this.fixNvueBug = {
  415. top: res.windowHeight / 2,
  416. left: res.windowWidth / 2
  417. }
  418. // #endif
  419. },
  420. methods: {
  421. /**
  422. * @param {Object} item
  423. * 小于 10 在前面加个 0
  424. */
  425. lessThanTen(item) {
  426. return item < 10 ? '0' + item : item
  427. },
  428. /**
  429. * 获取父元素实例
  430. */
  431. getForm(name = 'uniForms') {
  432. let parent = this.$parent;
  433. let parentName = parent.$options.name;
  434. while (parentName !== name) {
  435. parent = parent.$parent;
  436. if (!parent) return false
  437. parentName = parent.$options.name;
  438. }
  439. return parent;
  440. },
  441. /**
  442. * 解析时分秒字符串,例如:00:00:00
  443. * @param {String} timeString
  444. */
  445. parseTimeType(timeString) {
  446. if (timeString) {
  447. let timeArr = timeString.split(':')
  448. this.hour = Number(timeArr[0])
  449. this.minute = Number(timeArr[1])
  450. this.second = Number(timeArr[2])
  451. }
  452. },
  453. /**
  454. * 解析选择器初始值,类型可以是字符串、时间戳,例如:2000-10-02、'08:30:00'、 1610695109000
  455. * @param {String | Number} datetime
  456. */
  457. initPickerValue(datetime) {
  458. let defaultValue = null
  459. if (datetime) {
  460. defaultValue = this.compareValueWithStartAndEnd(datetime, this.start, this.end)
  461. } else {
  462. defaultValue = Date.now()
  463. defaultValue = this.compareValueWithStartAndEnd(defaultValue, this.start, this.end)
  464. }
  465. this.parseValue(defaultValue)
  466. },
  467. /**
  468. * 初始值规则:
  469. * - 用户设置初始值 value
  470. * - 设置了起始时间 start、终止时间 end,并 start < value < end,初始值为 value, 否则初始值为 start
  471. * - 只设置了起始时间 start,并 start < value,初始值为 value,否则初始值为 start
  472. * - 只设置了终止时间 end,并 value < end,初始值为 value,否则初始值为 end
  473. * - 无起始终止时间,则初始值为 value
  474. * - 无初始值 value,则初始值为当前本地时间 Date.now()
  475. * @param {Object} value
  476. * @param {Object} dateBase
  477. */
  478. compareValueWithStartAndEnd(value, start, end) {
  479. let winner = null
  480. value = this.superTimeStamp(value)
  481. start = this.superTimeStamp(start)
  482. end = this.superTimeStamp(end)
  483. if (start && end) {
  484. if (value < start) {
  485. winner = new Date(start)
  486. } else if (value > end) {
  487. winner = new Date(end)
  488. } else {
  489. winner = new Date(value)
  490. }
  491. } else if (start && !end) {
  492. winner = start <= value ? new Date(value) : new Date(start)
  493. } else if (!start && end) {
  494. winner = value <= end ? new Date(value) : new Date(end)
  495. } else {
  496. winner = new Date(value)
  497. }
  498. return winner
  499. },
  500. /**
  501. * 转换为可比较的时间戳,接受日期、时分秒、时间戳
  502. * @param {Object} value
  503. */
  504. superTimeStamp(value) {
  505. let dateBase = ''
  506. if (this.type === 'time' && value && typeof value === 'string') {
  507. const now = new Date()
  508. const year = now.getFullYear()
  509. const month = now.getMonth() + 1
  510. const day = now.getDate()
  511. dateBase = year + '/' + month + '/' + day + ' '
  512. }
  513. if (Number(value) && typeof value !== NaN) {
  514. value = parseInt(value)
  515. dateBase = 0
  516. }
  517. return this.createTimeStamp(dateBase + value)
  518. },
  519. /**
  520. * 解析默认值 value,字符串、时间戳
  521. * @param {Object} defaultTime
  522. */
  523. parseValue(value) {
  524. if (!value) return
  525. if (this.type === 'time' && typeof value === "string") {
  526. this.parseTimeType(value)
  527. } else {
  528. let defaultDate = null
  529. defaultDate = new Date(value)
  530. if (this.type !== 'time') {
  531. this.year = defaultDate.getFullYear()
  532. this.month = defaultDate.getMonth() + 1
  533. this.day = defaultDate.getDate()
  534. }
  535. if (this.type !== 'date') {
  536. this.hour = defaultDate.getHours()
  537. this.minute = defaultDate.getMinutes()
  538. this.second = defaultDate.getSeconds()
  539. }
  540. }
  541. if (this.hideSecond) {
  542. this.second = 0
  543. }
  544. },
  545. /**
  546. * 解析可选择时间范围 start、end,年月日字符串、时间戳
  547. * @param {Object} defaultTime
  548. */
  549. parseDatetimeRange(point, pointType) {
  550. if (point && this.type === 'time') {
  551. const pointArr = point.split(':')
  552. this[pointType + 'Hour'] = Number(pointArr[0])
  553. this[pointType + 'Minute'] = Number(pointArr[1])
  554. this[pointType + 'Second'] = Number(pointArr[2])
  555. } else {
  556. if (!point) {
  557. pointType === 'start' ? this.startYear = this.year - 60 : this.endYear = this.year + 60
  558. return
  559. }
  560. if (Number(point) && Number(point) !== NaN) {
  561. point = parseInt(point)
  562. }
  563. // datetime 的 end 没有时分秒, 则不限制
  564. const hasTime = /[0-9]:[0-9]/
  565. if (this.type === 'datetime' && pointType === 'end' && typeof point === 'string' && !hasTime.test(
  566. point)) {
  567. point = point + ' 23:59:59'
  568. }
  569. const pointDate = new Date(point)
  570. this[pointType + 'Year'] = pointDate.getFullYear()
  571. this[pointType + 'Month'] = pointDate.getMonth() + 1
  572. this[pointType + 'Day'] = pointDate.getDate()
  573. if (this.type === 'datetime') {
  574. this[pointType + 'Hour'] = pointDate.getHours()
  575. this[pointType + 'Minute'] = pointDate.getMinutes()
  576. this[pointType + 'Second'] = pointDate.getSeconds()
  577. }
  578. }
  579. },
  580. // 获取 年、月、日、时、分、秒 当前可选范围
  581. getCurrentRange(value) {
  582. const range = []
  583. for (let i = this['min' + this.capitalize(value)]; i <= this['max' + this.capitalize(value)]; i++) {
  584. range.push(i)
  585. }
  586. return range
  587. },
  588. // 字符串首字母大写
  589. capitalize(str) {
  590. return str.charAt(0).toUpperCase() + str.slice(1)
  591. },
  592. // 检查当前值是否在范围内,不在则当前值重置为可选范围第一项
  593. checkValue(name, value, values) {
  594. if (values.indexOf(value) === -1) {
  595. this[name] = values[0]
  596. }
  597. },
  598. // 每个月的实际天数
  599. daysInMonth(year, month) { // Use 1 for January, 2 for February, etc.
  600. return new Date(year, month, 0).getDate();
  601. },
  602. //兼容 iOS、safari 日期格式
  603. fixIosDateFormat(value) {
  604. if (typeof value === 'string') {
  605. value = value.replace(/-/g, '/')
  606. }
  607. return value
  608. },
  609. /**
  610. * 生成时间戳
  611. * @param {Object} time
  612. */
  613. createTimeStamp(time) {
  614. if (!time) return
  615. if (typeof time === "number") {
  616. return time
  617. } else {
  618. time = time.replace(/-/g, '/')
  619. if (this.type === 'date') {
  620. time = time + ' ' + '00:00:00'
  621. }
  622. return Date.parse(time)
  623. }
  624. },
  625. /**
  626. * 生成日期或时间的字符串
  627. */
  628. createDomSting() {
  629. const yymmdd = this.year +
  630. '-' +
  631. this.lessThanTen(this.month) +
  632. '-' +
  633. this.lessThanTen(this.day)
  634. let hhmmss = this.lessThanTen(this.hour) +
  635. ':' +
  636. this.lessThanTen(this.minute)
  637. if (!this.hideSecond) {
  638. hhmmss = hhmmss + ':' + this.lessThanTen(this.second)
  639. }
  640. if (this.type === 'date') {
  641. return yymmdd
  642. } else if (this.type === 'time') {
  643. return hhmmss
  644. } else {
  645. return yymmdd + ' ' + hhmmss
  646. }
  647. },
  648. /**
  649. * 初始化返回值,并抛出 change 事件
  650. */
  651. initTime(emit = true) {
  652. this.time = this.createDomSting()
  653. if (!emit) return
  654. if (this.returnType === 'timestamp' && this.type !== 'time') {
  655. this.formItem && this.formItem.setValue(this.createTimeStamp(this.time))
  656. this.$emit('change', this.createTimeStamp(this.time))
  657. this.$emit('input', this.createTimeStamp(this.time))
  658. } else {
  659. this.formItem && this.formItem.setValue(this.time)
  660. this.$emit('change', this.time)
  661. this.$emit('input', this.time)
  662. }
  663. },
  664. /**
  665. * 用户选择日期或时间更新 data
  666. * @param {Object} e
  667. */
  668. bindDateChange(e) {
  669. const val = e.detail.value
  670. this.year = this.years[val[0]]
  671. this.month = this.months[val[1]]
  672. this.day = this.days[val[2]]
  673. },
  674. bindTimeChange(e) {
  675. const val = e.detail.value
  676. this.hour = this.hours[val[0]]
  677. this.minute = this.minutes[val[1]]
  678. this.second = this.seconds[val[2]]
  679. },
  680. /**
  681. * 初始化弹出层
  682. */
  683. initTimePicker() {
  684. if (this.disabled) return
  685. const value = this.fixIosDateFormat(this.value)
  686. this.initPickerValue(value)
  687. this.visible = !this.visible
  688. },
  689. /**
  690. * 触发或关闭弹框
  691. */
  692. tiggerTimePicker(e) {
  693. this.visible = !this.visible
  694. },
  695. /**
  696. * 用户点击“清空”按钮,清空当前值
  697. */
  698. clearTime() {
  699. this.time = ''
  700. this.formItem && this.formItem.setValue(this.time)
  701. this.$emit('change', this.time)
  702. this.$emit('input', this.time)
  703. this.tiggerTimePicker()
  704. },
  705. /**
  706. * 用户点击“确定”按钮
  707. */
  708. setTime() {
  709. this.initTime()
  710. this.tiggerTimePicker()
  711. }
  712. }
  713. }
  714. </script>
  715. <style lang="scss" scoped>
  716. @charset "UTF-8";
  717. .uni-datetime-picker {
  718. /* #ifndef APP-NVUE */
  719. width: 100%;
  720. /* #endif */
  721. }
  722. .uni-datetime-picker-view {
  723. height: 130px;
  724. width: 270px;
  725. /* #ifndef APP-NVUE */
  726. cursor: pointer;
  727. /* #endif */
  728. }
  729. .uni-datetime-picker-item {
  730. height: 50px;
  731. line-height: 50px;
  732. text-align: center;
  733. font-size: 14px;
  734. }
  735. .uni-datetime-picker-btn {
  736. margin-top: 20px;
  737. /* #ifndef APP-NVUE */
  738. display: flex;
  739. cursor: pointer;
  740. /* #endif */
  741. flex-direction: row;
  742. justify-content: space-between;
  743. &-group {
  744. font-size: 14px;
  745. //color: #2EDAF1;
  746. //background: red;
  747. width: 60px;
  748. height: 40px;
  749. line-height: 40px;
  750. border-radius: 8px;
  751. text-align: center;
  752. &:first-child{
  753. //text-align: left;
  754. background: rgba(255, 124, 110, 1);
  755. color: white;
  756. //flex: 1;
  757. }
  758. &:not(:last-child,:first-child){
  759. background: rgba(221, 226, 236, 1);
  760. color: white;
  761. }
  762. &:last-child{
  763. //text-align: right;
  764. background: #2EDAF1;
  765. color: white;
  766. }
  767. }
  768. }
  769. .uni-datetime-picker-mask {
  770. position: fixed;
  771. bottom: 0px;
  772. top: 0px;
  773. left: 0px;
  774. right: 0px;
  775. background-color: rgba(0, 0, 0, 0.4);
  776. transition-duration: 0.3s;
  777. z-index: 998;
  778. }
  779. .uni-datetime-picker-popup {
  780. border-radius: 8px;
  781. padding: 20px;
  782. width: 270px;
  783. /* #ifdef APP-NVUE */
  784. height: 500px;
  785. /* #endif */
  786. /* #ifdef APP-NVUE */
  787. width: 330px;
  788. /* #endif */
  789. background-color: #fff;
  790. position: fixed;
  791. top: 50%;
  792. left: 50%;
  793. transform: translate(-50%, -50%);
  794. transition-duration: 0.3s;
  795. z-index: 999;
  796. }
  797. .fix-nvue-height {
  798. /* #ifdef APP-NVUE */
  799. height: 330px;
  800. /* #endif */
  801. }
  802. .uni-datetime-picker-time {
  803. color: grey;
  804. }
  805. .uni-datetime-picker-column {
  806. height: 50px;
  807. }
  808. .uni-datetime-picker-timebox {
  809. border: 1px solid #E5E5E5;
  810. border-radius: 5px;
  811. padding: 7px 10px;
  812. /* #ifndef APP-NVUE */
  813. box-sizing: border-box;
  814. cursor: pointer;
  815. /* #endif */
  816. }
  817. .uni-datetime-picker-timebox-pointer {
  818. display: flex;
  819. justify-content: flex-end;
  820. /* #ifndef APP-NVUE */
  821. cursor: pointer;
  822. /* #endif */
  823. }
  824. .uni-datetime-picker-disabled {
  825. opacity: 0.4;
  826. /* #ifdef H5 */
  827. cursor: not-allowed !important;
  828. /* #endif */
  829. }
  830. .uni-datetime-picker-text {
  831. font-size: 14px;
  832. }
  833. .uni-datetime-picker-sign {
  834. position: absolute;
  835. top: 53px;
  836. /* 减掉 10px 的元素高度,兼容nvue */
  837. color: #999;
  838. /* #ifdef APP-NVUE */
  839. font-size: 16px;
  840. /* #endif */
  841. }
  842. .sign-left {
  843. left: 86px;
  844. }
  845. .sign-right {
  846. right: 86px;
  847. }
  848. .sign-center {
  849. left: 135px;
  850. }
  851. .uni-datetime-picker__container-box {
  852. position: relative;
  853. display: flex;
  854. align-items: center;
  855. justify-content: center;
  856. //border:1px solid #ccc;
  857. //border-radius: 8px;
  858. overflow: hidden;
  859. }
  860. .time-hide-second {
  861. width: 180px;
  862. }
  863. .uni-title{
  864. margin: 10px 0;
  865. }
  866. </style>