DateTimeV2.vue 3.6 KB

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