Calendar.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <template>
  2. <div >
  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: 1,
  28. rows: 1,
  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.doDates,
  59. },
  60. {
  61. highlight: {
  62. fillMode:'solid',
  63. color: "red",
  64. // style: {
  65. // width: "16px",
  66. // height: "3px",
  67. // borderRadius: "0",
  68. // },
  69. contentClass: "italic",
  70. },
  71. dates: this.unDoDates,
  72. },
  73. {
  74. highlight: {
  75. fillMode:'solid',
  76. color: "orange",
  77. // style: {
  78. // width: "16px",
  79. // height: "3px",
  80. // borderRadius: "0",
  81. // },
  82. contentClass: "italic",
  83. },
  84. dates: this.halfDoDates,
  85. }
  86. ],
  87. };
  88. },
  89. props: {
  90. startDate: {
  91. isRequired: true,
  92. },
  93. endDate: {
  94. type:[Date,String],
  95. default: () => {
  96. return new Date();
  97. },
  98. },
  99. doDates: {
  100. isRequired: true,
  101. type: Array,
  102. },
  103. unDoDates: {
  104. isRequired: true,
  105. type: Array,
  106. },
  107. halfDoDates: {
  108. isRequired: true,
  109. type: Array,
  110. },
  111. defaultSelectedDate:{
  112. type:Object
  113. }
  114. },
  115. computed: {
  116. calendarWidth() {
  117. if (this.layout.columns === 0) {
  118. return "display:none";
  119. } else if (this.layout.columns === 1) {
  120. return "width:33.3%";
  121. } else if (this.layout.columns === 1) {
  122. return "width:66.6%";
  123. } else {
  124. return "";
  125. }
  126. },
  127. },
  128. watch: {
  129. startDate: {
  130. immediate: true,
  131. handler: function (v) {
  132. this.attrs[0].dates.start=v;
  133. this.setLayout(v);
  134. },
  135. },
  136. endDate: {
  137. immediate: true,
  138. handler: function (v) {
  139. console.log("456",v)
  140. this.attrs[0].dates.end=v;
  141. },
  142. },
  143. doDates: {
  144. immediate: true,
  145. handler: function (v) {
  146. this.attrs[1].dates=v;
  147. },
  148. },
  149. unDoDates: {
  150. immediate: true,
  151. handler: function (v) {
  152. this.attrs[2].dates=v;
  153. },
  154. },
  155. halfDoDates: {
  156. immediate: true,
  157. handler: function (v) {
  158. this.attrs[3].dates=v;
  159. },
  160. },
  161. },
  162. mounted() {
  163. this.init();
  164. },
  165. methods: {
  166. init() {
  167. // this.setLayout();
  168. // const calendar = this.$refs.calendar;
  169. // setTimeout(() => {
  170. // calendar.move({ month: 1, year: 1983 });
  171. // }, 3000);
  172. //calendar.move({ month: 1, year: 1983 });
  173. //this.setOps();
  174. },
  175. setLayout(startDate) {
  176. /*debugger
  177. console.log("qqqqq")
  178. if (startDate != null) {
  179. let diff = Math.ceil(
  180. dayjs(new Date()).diff(new Date(startDate), "months", true)
  181. );
  182. if (diff >= 2) {
  183. this.layout.columns = 2;
  184. } else {
  185. this.layout.columns = diff;
  186. }
  187. this.layout.rows = Math.ceil(diff / 2);
  188. }*/
  189. },
  190. onSelect(data){
  191. this.$emit("select",data.date)
  192. }
  193. },
  194. };
  195. </script>
  196. <style scoped lang="scss"></style>