DateTimeV2.vue 3.3 KB

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