selectTime.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <div class="select_time">
  3. <span
  4. v-for="(item,index) in timeArr"
  5. @click="checkTime(item)"
  6. class="bar"
  7. :class="item == activeClass ? 'active' : ''"
  8. :key="index">{{item}}</span>
  9. <span @click="doCheck" :class="'自定义' == activeClass ? 'active' : ''">
  10. 自定义
  11. <i v-show="valDate.length">{{valDate[0]}} ~ {{valDate[1]}}</i>
  12. <div v-show="isShow" class="picker_view">
  13. <i>扫楼时间111:</i>
  14. <el-date-picker
  15. v-model="value"
  16. type="daterange"
  17. range-separator="至"
  18. start-placeholder="开始日期"
  19. end-placeholder="结束日期"
  20. @change="getDate"
  21. ></el-date-picker>
  22. </div>
  23. </span>
  24. <div class="masked" v-if="isShow" @click="isShow = !isShow"></div>
  25. </div>
  26. </template>
  27. <script>
  28. export default {
  29. props: ['timeArr'],
  30. data() {
  31. return {
  32. activeClass: '今天',
  33. value: '',
  34. isShow: false,
  35. valDate: []
  36. }
  37. },
  38. methods: {
  39. getNowFormatDate(str) {
  40. var date = new Date(str);
  41. var seperator1 = "-";
  42. var seperator2 = ":";
  43. var month = date.getMonth() + 1;
  44. var strDate = date.getDate();
  45. if (month >= 1 && month <= 9) {
  46. month = "0" + month;
  47. }
  48. if (strDate >= 0 && strDate <= 9) {
  49. strDate = "0" + strDate;
  50. }
  51. let minutes = date.getMinutes() > 9 ? date.getMinutes() : "0" + date.getMinutes()
  52. let hour = date.getHours() > 9 ? date.getHours() : "0" + date.getHours()
  53. let seconds = date.getSeconds() > 9 ? date.getSeconds() : "0" + date.getSeconds()
  54. var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
  55. + " " + hour + seperator2 + minutes
  56. + seperator2 + seconds;
  57. return currentdate;
  58. },
  59. checkTime(val) {
  60. // 控制active显示
  61. this.activeClass = val
  62. let nowdate = new Date().setHours(0, 0, 0, 0)
  63. let oldDate = ''
  64. let oneDay = new Date()
  65. this.valDate = []
  66. switch (val) {
  67. case '一个月内':
  68. oldDate = new Date(new Date().setMonth(new Date().getMonth() - 1)).setHours(0, 0, 0, 0)
  69. this.$emit('checkTime', [this.getNowFormatDate(oldDate), this.getNowFormatDate(new Date(oneDay))])
  70. break;
  71. case '一周内':
  72. oldDate = new Date(nowdate - 7 * 24 * 3600 * 1000)
  73. this.$emit('checkTime', [this.getNowFormatDate(oldDate), this.getNowFormatDate(new Date(oneDay))])
  74. break;
  75. case '近三天':
  76. oldDate = new Date(nowdate - 3 * 24 * 3600 * 1000)
  77. this.$emit('checkTime', [this.getNowFormatDate(oldDate), this.getNowFormatDate(new Date(oneDay))])
  78. break;
  79. case '昨天':
  80. oldDate = new Date(nowdate - 24 * 3600 * 1000)
  81. this.$emit('checkTime', [this.getNowFormatDate(oldDate), this.getNowFormatDate(new Date(nowdate))])
  82. break;
  83. case '今天':
  84. oldDate = new Date(nowdate);
  85. this.$emit('checkTime', [this.getNowFormatDate(oldDate), this.getNowFormatDate(new Date())])
  86. break;
  87. default:
  88. }
  89. },
  90. doCheck() {
  91. this.isShow = true
  92. },
  93. getDate() {
  94. if (this.value == '' || this.value) {
  95. this.$emit('checkTime', [this.getNowFormatDate(this.value[0]), this.getNowFormatDate(this.value[1])])
  96. this.valDate[0] = this.getNowFormatDate(this.value[0]).split(' ')[0]
  97. this.valDate[1] = this.getNowFormatDate(this.value[1]).split(' ')[0]
  98. this.isShow = false
  99. this.activeClass = '自定义'
  100. } else {
  101. this.isShow = false
  102. }
  103. }
  104. }
  105. }
  106. </script>
  107. <style lang="less" scoped>
  108. .select_time {
  109. font-size: 0.8rem;
  110. display: inline-block;
  111. margin-left: 2rem;
  112. span {
  113. cursor: pointer;
  114. position: relative;
  115. &.active {
  116. color: #409eff;
  117. }
  118. .picker_view {
  119. position: absolute;
  120. width: 33rem;
  121. bottom: -4rem;
  122. left: -16rem;
  123. height: 3rem;
  124. padding-top: -0.5rem;
  125. box-sizing: border-box;
  126. z-index: 999;
  127. background-color: rgba(255, 255, 255, 1);
  128. padding-left: 1rem;
  129. padding-right: 2rem;
  130. border: 0.01rem solid #eee;
  131. z-index: 2001;
  132. i {
  133. color: #000;
  134. }
  135. }
  136. }
  137. .bar::after {
  138. content: "|";
  139. margin-right: 0.2rem;
  140. margin-left: 0.2rem;
  141. color: #000;
  142. }
  143. .masked {
  144. position: fixed;
  145. left: 0;
  146. top: 0;
  147. right: 0;
  148. bottom: 0;
  149. opacity: 0;
  150. z-index: 2;
  151. }
  152. }
  153. </style>