Calendar.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <template>
  2. <div :style="calendarWidth">
  3. <VCalender
  4. ref="calendar"
  5. :columns="layout.columns"
  6. :rows="layout.rows"
  7. :is-expanded="layout.isExpanded"
  8. :attributes="attrs"
  9. :min-date="startDate ? new Date(startDate) : new Date()"
  10. :max-date="new Date()"
  11. v-model="selectedDate"
  12. @dayclick="onSelect"
  13. />
  14. </div>
  15. </template>
  16. <script>
  17. import dayjs from "dayjs";
  18. //日历组件
  19. import {Calendar as VCalender} from 'v-calendar';
  20. export default {
  21. name: "Calendar",
  22. components:{VCalender},
  23. data() {
  24. return {
  25. selectedDate:this.defaultSelectedDate,
  26. layout: {
  27. columns: 3,
  28. rows: 2,
  29. isExpanded: true,
  30. },
  31. attrs: [
  32. {
  33. highlight: {
  34. start: {
  35. fillMode: "outline",
  36. },
  37. base: {
  38. color: "gray",
  39. fillMode: "light",
  40. },
  41. end: {
  42. fillMode: "outline",
  43. },
  44. },
  45. dates: { start: this.startDate?new Date(this.startDate):new Date(), end: new Date() },
  46. },
  47. {
  48. dot: {
  49. color: "green",
  50. style: {
  51. width: "16px",
  52. height: "3px",
  53. borderRadius: "0",
  54. },
  55. contentClass: "italic",
  56. },
  57. dates: this.full,
  58. },
  59. {
  60. dot: {
  61. color: "yellow",
  62. style: {
  63. backgroundColor: "orange",
  64. width: "16px",
  65. height: "3px",
  66. borderRadius: "0",
  67. },
  68. contentClass: "italic",
  69. },
  70. dates: this.paritialLoss,
  71. },
  72. {
  73. dot: {
  74. color: "red",
  75. style: {
  76. width: "16px",
  77. height: "3px",
  78. borderRadius: "0",
  79. },
  80. contentClass: "italic",
  81. },
  82. dates: this.allLoss,
  83. },
  84. ],
  85. };
  86. },
  87. props: {
  88. startDate: {
  89. isRequired: true,
  90. },
  91. endDate: {
  92. default: () => {
  93. return new Date();
  94. },
  95. },
  96. full: {
  97. isRequired: true,
  98. type: Array,
  99. },
  100. paritialLoss: {
  101. isRequired: true,
  102. type: Array,
  103. },
  104. allLoss: {
  105. isRequired: true,
  106. type: Array,
  107. },
  108. defaultSelectedDate:{
  109. type:Object
  110. }
  111. },
  112. computed: {
  113. calendarWidth() {
  114. if (this.layout.columns === 0) {
  115. return "display:none";
  116. } else if (this.layout.columns === 1) {
  117. return "width:33.3%";
  118. } else if (this.layout.columns === 1) {
  119. return "width:66.6%";
  120. } else {
  121. return "";
  122. }
  123. },
  124. },
  125. watch: {
  126. startDate: {
  127. immediate: true,
  128. handler: function (v) {
  129. this.setLayout(v);
  130. },
  131. },
  132. },
  133. mounted() {
  134. this.init();
  135. },
  136. methods: {
  137. init() {
  138. // this.setLayout();
  139. // const calendar = this.$refs.calendar;
  140. // setTimeout(() => {
  141. // calendar.move({ month: 1, year: 1983 });
  142. // }, 3000);
  143. //calendar.move({ month: 1, year: 1983 });
  144. //this.setOps();
  145. },
  146. setLayout(startDate) {
  147. if (startDate != null) {
  148. let diff = Math.ceil(
  149. dayjs(new Date()).diff(new Date(startDate), "months", true)
  150. );
  151. if (diff >= 3) {
  152. this.layout.columns = 3;
  153. } else {
  154. this.layout.columns = diff;
  155. }
  156. this.layout.rows = Math.ceil(diff / 3);
  157. }
  158. },
  159. onSelect(data){
  160. this.$emit("select",data.date)
  161. }
  162. },
  163. };
  164. </script>
  165. <style scoped lang="scss"></style>