timeRange.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <template>
  2. <a-range-picker v-if="show" :default-value="[defaultStartTime,defaultEndTime]"
  3. :format="timeFormat"
  4. @change="change"
  5. />
  6. </template>
  7. <script>
  8. export default {
  9. data() {
  10. return {
  11. show: false,
  12. defaultStartTime: null,
  13. defaultEndTime: null,
  14. }
  15. },
  16. props: {
  17. timeFormat: {
  18. type: String,
  19. default: "YYYY/MM/DD"
  20. },
  21. timeRange: Object,
  22. },
  23. created() {
  24. },
  25. mounted() {
  26. this.$nextTick(()=>{
  27. if (this.timeRange) {
  28. if (this.timeRange.startTime) {
  29. this.defaultStartTime = this.$moment(this.timeRange.startTime, this.timeFormat)
  30. }
  31. if (this.timeRange.endTime) {
  32. this.defaultEndTime = this.$moment(this.timeRange.endTime, this.timeFormat)
  33. }
  34. }
  35. this.show = true
  36. })
  37. },
  38. emits: ['update:timeRange'],
  39. setup(props, context) {
  40. const methods = {
  41. updateTimeRange(obj) {
  42. context.emit('update:timeRange', obj)
  43. },
  44. }
  45. return methods
  46. },
  47. methods: {
  48. change(date, dateStr) {
  49. let obj = {
  50. startTime: dateStr[0],
  51. endTime: dateStr[1]
  52. }
  53. this.updateTimeRange(obj);
  54. },
  55. range(start, end) {
  56. const result = [];
  57. for (let i = start; i < end; i++) {
  58. result.push(i);
  59. }
  60. return result;
  61. },
  62. disabledDate(current) {
  63. // Can not select days before today and today
  64. return current && current < this.$moment().endOf("day");
  65. },
  66. disabledDateTime() {
  67. return {
  68. disabledHours: () => this.range(0, 24).splice(4, 20),
  69. disabledMinutes: () => this.range(30, 60),
  70. disabledSeconds: () => [55, 56],
  71. };
  72. },
  73. disabledRangeTime(_, type) {
  74. if (type === "start") {
  75. return {
  76. disabledHours: () => this.range(0, 60).splice(24, 4),
  77. disabledMinutes: () => this.range(30, 60),
  78. disabledSeconds: () => [55, 56],
  79. };
  80. }
  81. return {
  82. disabledHours: () => this.range(0, 60).splice(24, 4),
  83. disabledMinutes: () => this.range(0, 31),
  84. disabledSeconds: () => [55, 56],
  85. };
  86. },
  87. },
  88. };
  89. </script>
  90. <style></style>