Calendar.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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="endDate"
  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: 4,
  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: this.endDate },
  46. },
  47. {
  48. highlight: {
  49. fillMode:'solid',
  50. color: "green",
  51. // style: {
  52. // width: "16px",
  53. // height: "3px",
  54. // borderRadius: "0",
  55. // },
  56. contentClass: "italic",
  57. },
  58. dates: this.full,
  59. },
  60. {
  61. highlight: {
  62. fillMode:'solid',
  63. color: "yellow",
  64. // style: {
  65. // backgroundColor: "orange",
  66. // width: "16px",
  67. // height: "3px",
  68. // borderRadius: "0",
  69. // },
  70. contentClass: "italic",
  71. },
  72. dates: this.paritialLoss,
  73. },
  74. {
  75. highlight: {
  76. fillMode:'solid',
  77. color: "red",
  78. // style: {
  79. // width: "16px",
  80. // height: "3px",
  81. // borderRadius: "0",
  82. // },
  83. contentClass: "italic",
  84. },
  85. dates: this.allLoss,
  86. },
  87. ],
  88. };
  89. },
  90. props: {
  91. startDate: {
  92. isRequired: true,
  93. },
  94. endDate: {
  95. type:[Date,String],
  96. default: () => {
  97. return new Date();
  98. },
  99. },
  100. full: {
  101. isRequired: true,
  102. type: Array,
  103. },
  104. paritialLoss: {
  105. isRequired: true,
  106. type: Array,
  107. },
  108. allLoss: {
  109. isRequired: true,
  110. type: Array,
  111. },
  112. defaultSelectedDate:{
  113. type:Object
  114. }
  115. },
  116. computed: {
  117. calendarWidth() {
  118. if (this.layout.columns === 0) {
  119. return "display:none";
  120. } else if (this.layout.columns === 1) {
  121. return "width:33.3%";
  122. } else if (this.layout.columns === 1) {
  123. return "width:66.6%";
  124. } else {
  125. return "";
  126. }
  127. },
  128. },
  129. watch: {
  130. startDate: {
  131. immediate: true,
  132. handler: function (v) {
  133. this.setLayout(v);
  134. },
  135. },
  136. endDate: {
  137. immediate: true,
  138. handler: function (v) {
  139. this.attrs[0].dates.end=v;
  140. },
  141. },
  142. },
  143. mounted() {
  144. this.init();
  145. },
  146. methods: {
  147. init() {
  148. // this.setLayout();
  149. // const calendar = this.$refs.calendar;
  150. // setTimeout(() => {
  151. // calendar.move({ month: 1, year: 1983 });
  152. // }, 3000);
  153. //calendar.move({ month: 1, year: 1983 });
  154. //this.setOps();
  155. },
  156. setLayout(startDate) {
  157. if (startDate != null) {
  158. let diff = Math.ceil(
  159. dayjs(new Date()).diff(new Date(startDate), "months", true)
  160. );
  161. if (diff >= 4) {
  162. this.layout.columns = 4;
  163. } else {
  164. this.layout.columns = diff;
  165. }
  166. this.layout.rows = Math.ceil(diff / 4);
  167. }
  168. },
  169. onSelect(data){
  170. this.$emit("select",data.date)
  171. }
  172. },
  173. };
  174. </script>
  175. <style scoped lang="scss"></style>