calendar.nvue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <template>
  2. <view class="u-page">
  3. <u-navbar
  4. title="日历"
  5. @leftClick="navigateBack"
  6. safeAreaInsetTop
  7. fixed
  8. placeholder
  9. ></u-navbar>
  10. <u-cell-group>
  11. <u-cell
  12. @click="showCalendar(index)"
  13. :title="item.title"
  14. v-for="(item, index) in list"
  15. :key="index"
  16. :label="values[index]"
  17. isLink
  18. >
  19. <image
  20. slot="icon"
  21. class="u-cell-icon"
  22. :src="item.iconUrl"
  23. mode="widthFix"
  24. ></image>
  25. </u-cell>
  26. </u-cell-group>
  27. <u-calendar
  28. :show="show1"
  29. defaultDate="2022-02-15"
  30. @confirm="confirm"
  31. @close="close"
  32. ></u-calendar>
  33. <u-calendar
  34. :show="show2"
  35. mode="multiple"
  36. :defaultDate="['2022-03-01']"
  37. @confirm="confirm"
  38. @close="close"
  39. ></u-calendar>
  40. <u-calendar
  41. :show="show3"
  42. mode="range"
  43. @confirm="confirm"
  44. @close="close"
  45. ></u-calendar>
  46. <u-calendar
  47. :show="show4"
  48. mode="range"
  49. @confirm="confirm"
  50. @close="close"
  51. color="#f56c6c"
  52. :defaultDate="customThemeDefaultDate"
  53. ></u-calendar>
  54. <u-calendar
  55. :show="show5"
  56. mode="range"
  57. @confirm="confirm"
  58. @close="close"
  59. :defaultDate="customTextDefaultDate"
  60. startText="住店"
  61. endText="离店"
  62. confirmDisabledText="请选择离店日期"
  63. :formatter="formatter"
  64. ></u-calendar>
  65. <u-calendar
  66. :show="show6"
  67. @confirm="confirm"
  68. @close="close"
  69. :maxDate="maxDate"
  70. ></u-calendar>
  71. <u-calendar
  72. :show="show7"
  73. @confirm="confirm"
  74. @close="close"
  75. showLunar
  76. ></u-calendar>
  77. <u-calendar
  78. :show="show8"
  79. @confirm="confirm"
  80. @close="close"
  81. mode="multiple"
  82. :defaultDate="defaultDateMultiple"
  83. ></u-calendar>
  84. </view>
  85. </template>
  86. <script>
  87. export default {
  88. data() {
  89. const d = new Date()
  90. const year = d.getFullYear()
  91. let month = d.getMonth() + 1
  92. month = month < 10 ? `0${month}` : month
  93. const date = d.getDate()
  94. return {
  95. index: 0,
  96. show1: false,
  97. show2: false,
  98. show3: false,
  99. show4: false,
  100. show5: false,
  101. show6: false,
  102. show7: false,
  103. show8: false,
  104. values: ['','','','','','','',''],
  105. customThemeDefaultDate: [`${year}-${month}-${date}`, `${year}-${month}-${date + 5}`],
  106. customTextDefaultDate: [`${year}-${month}-${date}`],
  107. maxDate: `${year}-${month}-${date + 10}`,
  108. defaultDateMultiple: [`${year}-${month}-${date}`, `${year}-${month}-${date + 1}`, `${year}-${month}-${date + 2}`],
  109. list: [{
  110. title: '单个日期',
  111. iconUrl: 'https://cdn.uviewui.com/uview/demo/calendar/7.png'
  112. },
  113. {
  114. title: '多个日期',
  115. iconUrl: 'https://cdn.uviewui.com/uview/demo/calendar/8.png'
  116. },
  117. {
  118. title: '日期范围',
  119. iconUrl: 'https://cdn.uviewui.com/uview/demo/calendar/9.png'
  120. },
  121. {
  122. title: '自定义主题颜色',
  123. iconUrl: 'https://cdn.uviewui.com/uview/demo/calendar/15.png'
  124. },{
  125. title: '自定义文案',
  126. iconUrl: 'https://cdn.uviewui.com/uview/demo/calendar/14.png'
  127. },{
  128. title: '日期最大范围',
  129. iconUrl: 'https://cdn.uviewui.com/uview/demo/calendar/13.png'
  130. },{
  131. title: '显示农历',
  132. iconUrl: 'https://cdn.uviewui.com/uview/demo/calendar/5.png'
  133. },{
  134. title: '默认日期',
  135. iconUrl: 'https://cdn.uviewui.com/uview/demo/calendar/10.png'
  136. }
  137. ]
  138. }
  139. },
  140. methods: {
  141. showCalendar(index) {
  142. this.index = index + 1
  143. this[`show${index + 1}`] = true
  144. },
  145. navigateBack() {
  146. uni.navigateBack();
  147. },
  148. confirm(e) {
  149. this[`show${this.index}`] = false
  150. console.log(e);
  151. switch(this.index - 1) {
  152. case 0:
  153. this.values[this.index - 1] = e[0];
  154. break;
  155. case 1:
  156. e.forEach((value, index) => {
  157. index === 0 ? this.values[this.index - 1] = value : this.values[this.index - 1] += ';' + value
  158. })
  159. break;
  160. case 2:
  161. this.values[this.index - 1] = e[0] + '~' + e[e.length - 1];
  162. break;
  163. case 3:
  164. this.values[this.index - 1] = e[0] + '~' + e[e.length - 1];
  165. break;
  166. case 4:
  167. this.values[this.index - 1] = e[0] + '~' + e[e.length - 1];
  168. break;
  169. case 5:
  170. this.values[this.index - 1] = e[0];
  171. break;
  172. case 6:
  173. this.values[this.index - 1] = e[0];
  174. break;
  175. case 7:
  176. e.forEach((value, index) => {
  177. index === 0 ? this.values[this.index - 1] = value : this.values[this.index - 1] += ';' + value
  178. })
  179. break;
  180. }
  181. },
  182. close() {
  183. this[`show${this.index}`] = false
  184. },
  185. formatter(day) {
  186. const d = new Date()
  187. let month = d.getMonth() + 1
  188. const date = d.getDate()
  189. if(day.month == month && day.day == date + 3) {
  190. day.bottomInfo = '有优惠'
  191. day.dot = true
  192. }
  193. return day
  194. }
  195. },
  196. }
  197. </script>
  198. <style lang="scss">
  199. .u-page {
  200. padding: 0;
  201. }
  202. </style>