range-datetime.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <template>
  2. <view class="datatime">
  3. <view class="content" @click="onShowDatePicker(type)">
  4. <text v-if="date">{{date}}</text>
  5. <text v-else-if="time">{{time}}</text>
  6. <text v-else-if="datetime">{{datetime}}</text>
  7. <text v-else-if="range.length > 0&&range[0]">{{range[0] == range[1] ? range[0] : range[0] +' 至 ' + range[1]}}</text>
  8. <text v-else-if="rangetime.length > 0&&rangetime[0]">{{rangetime[0] == rangetime[1] ? rangetime[0] : rangetime[0] +' 至 ' + rangetime[1]}}</text>
  9. <text v-else>请选择</text>
  10. </view>
  11. <view class="fix-bg">
  12. </view>
  13. <mx-date-picker style="color: #000;" :format="format" :show="showPicker" :type="type" :value="value" :show-tips="true" :begin-text="'开始'" :end-text="'结束'" :show-seconds="true" @confirm="onSelected" @cancel="onSelected" />
  14. </view>
  15. </template>
  16. <script>
  17. import MxDatePicker from "@/components/mx-datepicker/mx-datepicker.vue";
  18. export default {
  19. components: {
  20. MxDatePicker
  21. },
  22. /**
  23. * 数据
  24. */
  25. props: {
  26. // 是否禁用
  27. disabled: {
  28. type: Boolean,
  29. default: false
  30. },
  31. //类型,可选值:date(日期)、time(时间)、datetime(日期时间)、range(日期范围)、rangetime(日期时间范围)
  32. type: {
  33. type: String,
  34. default: 'range'
  35. },
  36. format:{
  37. type: String,
  38. default: 'yyyy.mm.dd hh:ii:ss'
  39. },
  40. defaultValue:{
  41. default: ''
  42. }
  43. },
  44. /**
  45. * 数据
  46. */
  47. data() {
  48. return {
  49. showPicker: false,
  50. date: '',
  51. time: '',
  52. datetime: '',
  53. range: [],
  54. rangetime: [],
  55. // type: 'rangetime',
  56. value: ''
  57. };
  58. },
  59. /**
  60. * 监听数据
  61. */
  62. // watch: {
  63. // // 默认值
  64. // defaultValue() {
  65. // // 设置默认值
  66. // this.setDefaultValue();
  67. // }
  68. // },
  69. /**
  70. * 组件初次加载完成
  71. */
  72. mounted() {
  73. this[this.type] = this.defaultValue;
  74. },
  75. /**
  76. * 方法
  77. */
  78. methods: {
  79. onShowDatePicker(type){//显示
  80. this.type = type;
  81. this.showPicker = true;
  82. this.value = this[type];
  83. },
  84. onSelected(e) {//选择
  85. this.showPicker = false;
  86. if(e) {
  87. this[this.type] = e.value;
  88. this.$emit('change',e)
  89. //选择的值
  90. //console.log('value => '+ e.value);
  91. //原始的Date对象
  92. //console.log('date => ' + e.date);
  93. }
  94. }
  95. }
  96. };
  97. </script>
  98. <style lang="scss" scoped>
  99. /* .content {
  100. text-align: right;
  101. margin-right: 60px!important;
  102. color: white;
  103. } */
  104. .picker{
  105. background-color: rgba(0, 0, 0, 0.5);
  106. }
  107. </style>