Date.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <template>
  2. <div
  3. class="datetime-container input-group"
  4. :class="{'has-error' : isValid === false}"
  5. >
  6. <input
  7. id="datetime"
  8. autocomplete="off"
  9. type="date"
  10. :readonly="readonly"
  11. :value="dateTime"
  12. class="datetime-item-1 form-control"
  13. @change="dateValueChanged($event)"
  14. />
  15. <input
  16. autocomplete="off"
  17. type="text"
  18. :readonly="readonly"
  19. :value="dateTime"
  20. class="datetime-item-2 form-control"
  21. @change="textValueChanged($event)"
  22. />
  23. <span
  24. v-if="isValid === false"
  25. class="glyphicon glyphicon-remove datetime-item-3 form-control-feedback"
  26. aria-hidden="true"
  27. />
  28. </div>
  29. </template>
  30. <script>
  31. import moment from 'moment';
  32. export default {
  33. // eslint-disable-next-line vue/multi-word-component-names
  34. name: 'Date',
  35. components: {
  36. },
  37. props: {
  38. 'modelValue': {
  39. type: String,
  40. default: null,
  41. },
  42. 'readonly': {
  43. type: Boolean,
  44. default: false,
  45. },
  46. },
  47. emits: ['update:modelValue'],
  48. data: function () {
  49. return {
  50. 'isValid': true,
  51. };
  52. },
  53. computed: {
  54. /**
  55. * 把(YYYY-MM-DD)格式的日期字符串转换成datetime-local(YYYY-MM-DD)
  56. */
  57. dateTime: function () {
  58. if (this.modelValue == null || this.modelValue.length == 0) {
  59. return null;
  60. } else {
  61. if(this.modelValue.length > 10){
  62. return moment(this.modelValue, 'YYYY-MM-DD HH:mm:ss').format('YYYY-MM-DD');
  63. }else{
  64. return moment(this.modelValue, 'YYYY-MM-DD').format('YYYY-MM-DD');
  65. }
  66. }
  67. },
  68. },
  69. mounted: function () {
  70. },
  71. methods: {
  72. /**
  73. * 是否是有效的日期时间格式,YYYY-MM-DD
  74. */
  75. isValidDateTime: function (text) {
  76. if (text == null || text.length == 0) {
  77. return true;
  78. }
  79. return moment(text, 'YYYY-MM-DD', true).isValid();
  80. },
  81. textValueChanged: function (event) {
  82. let newValue = event.target.value;
  83. // 日期时间校验
  84. if (this.isValidDateTime(newValue)) {
  85. if (this.readonly == true && (newValue == null || newValue.length == 0)) {
  86. this.isValid = false;
  87. } else {
  88. this.isValid = true;
  89. }
  90. console.log('Date input value changed: orginal value: %s, current value: %s', this.modelValue, newValue);
  91. this.$emit('update:modelValue', newValue);
  92. } else {
  93. this.isValid = false;
  94. }
  95. },
  96. /**
  97. * 值改变事件
  98. */
  99. dateValueChanged: function (event) {
  100. let newValue = event.target.value;
  101. let parsedDateTime = '';
  102. if (newValue != null && newValue.length > 0) {
  103. parsedDateTime = moment(newValue).format('YYYY-MM-DD');
  104. }
  105. // 日期时间校验
  106. if (this.isValidDateTime(parsedDateTime)) {
  107. if (this.readonly == true && (parsedDateTime == null || parsedDateTime.length == 0)) {
  108. this.isValid = false;
  109. } else {
  110. this.isValid = true;
  111. }
  112. console.log('Date value changed: orginal value: %s, current value: %s', newValue, parsedDateTime);
  113. this.$emit('update:modelValue', parsedDateTime);
  114. } else {
  115. this.isValid = false;
  116. }
  117. },
  118. },
  119. };
  120. </script>
  121. <style>
  122. /* .form-inline .datetime-container {
  123. display: inline-flex !important;
  124. vertical-align: middle;
  125. } */
  126. </style>
  127. <style scoped>
  128. .datetime-container {
  129. position: relative;
  130. height: 34px;
  131. width: auto;
  132. vertical-align: middle;
  133. min-width: 200px;
  134. /* display : inline-flex; */
  135. display: inline-flex;
  136. }
  137. .datetime-item-1 {
  138. position: absolute;
  139. display: inline;
  140. width: 100%;
  141. cursor: pointer;
  142. border-radius: 4px !important;
  143. }
  144. .datetime-item-2 {
  145. position: absolute !important;
  146. border-radius: 4px 0px 0px 4px !important;
  147. display: inline;
  148. width: calc(100% - 39px) !important;
  149. }
  150. .datetime-item-3 {
  151. right: 39px;
  152. }
  153. /*控制下拉小箭头的*/
  154. input[type="datetime-local"]::-webkit-calendar-picker-indicator {
  155. padding-right: 0px;
  156. }
  157. </style>