VueMonthlyPickerExample.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <template>
  2. <div>
  3. <h1>月份选择器</h1>
  4. <h2>绑定dayjs对象</h2>
  5. <div class="demo-component">
  6. <vue-monthly-picker
  7. v-model="selectedMonth"
  8. :input-class="{'input': isDisplayInput}"
  9. :disabled="isDisable"
  10. :month-labels="locale"
  11. :clear-option="clearOption"
  12. :min="min"
  13. :max="max"
  14. :alignment="alignment"
  15. :selected-background-color="selectedBackgroundColor"
  16. @selected="handleSelect"
  17. />
  18. </div>
  19. <div>选择的月份: {{ selectedMonthStr }}</div>
  20. <h2>绑定String</h2>
  21. <div class="demo-component">
  22. <vue-monthly-picker
  23. v-model="selectedMonth1"
  24. :input-class="{'input': isDisplayInput}"
  25. :disabled="isDisable"
  26. :month-labels="locale"
  27. :clear-option="clearOption"
  28. :min="min"
  29. :max="max"
  30. :alignment="alignment"
  31. :selected-background-color="selectedBackgroundColor"
  32. @selected="handleSelect"
  33. />
  34. </div>
  35. <div>选择的月份: {{ selectedMonth1 }}</div>
  36. <div>
  37. <div class="form-group">
  38. <label>只读</label>
  39. <div class="input-group">
  40. <Switches v-model="isDisable" />
  41. {{ isDisable ? 'Disabled': 'Enable' }}
  42. </div>
  43. </div>
  44. <div class="form-group">
  45. <label>限制范围</label>
  46. <div class="input-group">
  47. <Switches v-model="isLimitRange" />
  48. {{ rangeDisplay }}
  49. </div>
  50. </div>
  51. <div class="form-group">
  52. <label>显示</label>
  53. <div class="input-group">
  54. <Switches
  55. v-model="isDisplayInput"
  56. true-value="Input"
  57. false-value="Label"
  58. />
  59. {{ isDisplayInput? 'Input': 'Label' }}
  60. </div>
  61. </div>
  62. <div class="form-group">
  63. <label>清空</label>
  64. <div class="input-group">
  65. <Switches v-model="clearOption" />
  66. {{ clearOption ? 'Enable': 'Disabled' }}
  67. </div>
  68. </div>
  69. <div class="form-group">
  70. <label>国际化</label>
  71. <select
  72. v-model="locale"
  73. placeholder="Select a language"
  74. class="form-control"
  75. >
  76. <option
  77. v-for="option in options"
  78. :key="option.id"
  79. :value="option.monthLabels"
  80. >
  81. {{ option.title }}
  82. </option>
  83. </select>
  84. </div>
  85. <div class="form-group">
  86. <label>对齐</label>
  87. <select
  88. v-model="alignment"
  89. placeholder="Select an alignment"
  90. class="form-control"
  91. >
  92. <option
  93. v-for="alignment1 in alignments"
  94. :key="alignment1"
  95. :value="alignment1"
  96. >
  97. {{ alignment1 }}
  98. </option>
  99. </select>
  100. </div>
  101. <div class="form-group">
  102. <label>背景色</label>
  103. <select
  104. v-model="selectedBackgroundColor"
  105. placeholder="Select an color"
  106. class="form-control"
  107. >
  108. <option
  109. v-for="color in colorExamples"
  110. :key="color"
  111. :value="color"
  112. >
  113. {{ color }}
  114. </option>
  115. </select>
  116. </div>
  117. </div>
  118. </div>
  119. </template>
  120. <script>
  121. import dayjs from 'dayjs';
  122. import VueMonthlyPicker from '@/vue-monthly-picker/index.js';
  123. import Switches from '@/switches/index.js';
  124. export default {
  125. name: 'App',
  126. components: {
  127. VueMonthlyPicker, Switches,
  128. },
  129. data() {
  130. return {
  131. selectedMonth: dayjs(),
  132. selectedMonth1: '2024-05',
  133. isDisable: false,
  134. isDisplayInput: true,
  135. locale: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  136. alignment: 'left',
  137. selectedBackgroundColor: 'blue',
  138. options: [
  139. {
  140. id: 1,
  141. title: 'English',
  142. monthLabels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  143. },
  144. {
  145. id: 2,
  146. title: '中文',
  147. monthLabels: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
  148. },
  149. {
  150. id: 3,
  151. title: 'Korean',
  152. monthLabels: ['1월', '2월', '3월', '4월', '5월', '6월', '7월', '8월', '9월', '10월', '11월', '12월'],
  153. },
  154. ],
  155. alignments: ['left', 'center', 'right'],
  156. colorExamples: ['blue', 'red', 'black'],
  157. min: null,
  158. max: null,
  159. isLimitRange: false,
  160. clearOption: true,
  161. };
  162. },
  163. computed: {
  164. rangeDisplay() {
  165. if (!this.min || !this.max) {
  166. return 'Disabled';
  167. }
  168. return this.min.format('YYYY/MM') + '-' + this.max.format('YYYY/MM');
  169. },
  170. selectedMonthStr(){
  171. if(typeof this.selectedMonth === 'string'){
  172. return this.selectedMonth;
  173. }else{
  174. return this.selectedMonth.format('YYYY-MM');
  175. }
  176. },
  177. },
  178. watch: {
  179. isLimitRange(newValue) {
  180. if (newValue) {
  181. this.setSelectRange(dayjs().subtract(6, 'months'), dayjs().add(6, 'months'));
  182. } else {
  183. this.setSelectRange(null, null);
  184. }
  185. },
  186. },
  187. mounted(){
  188. this.testDayJs();
  189. },
  190. methods: {
  191. handleSelect(value) {
  192. console.log('Select', value);
  193. },
  194. setSelectRange(min, max) {
  195. this.min = min;
  196. this.max = max;
  197. },
  198. // 测试DayJs
  199. testDayJs(){
  200. // exist bug
  201. console.log(dayjs('00:00:00', 'HH:mm:ss'));
  202. console.log(dayjs('2024-05-09').format('YYYY-MM-DD'));
  203. console.log(dayjs('2024-05-09 10:45').format('YYYY-MM-DD'));
  204. console.log(dayjs('2024-05-09 10:45:01').format('YYYY-MM-DD'));
  205. console.log(dayjs().format('YYYYMMDD_hhmmss'));
  206. const nowDate = new Date();
  207. const startDate = dayjs(nowDate).subtract(1, 'M').format('YYYY-MM-DD HH:mm:ss');
  208. const endDate = dayjs(nowDate).add(1, 'd').format('YYYY-MM-DD HH:mm:ss');
  209. console.log(startDate, endDate);
  210. const startDate1 = '2024-01-02';
  211. const endDate1 = '2024-01-10';
  212. let currentYearMonth = dayjs(startDate1).add(1, 'M').format('YYYY-MM');
  213. console.log(endDate1, 'isBefore', currentYearMonth, dayjs(endDate1).isBefore(currentYearMonth));
  214. console.log(dayjs('2024-01-25').format('YYYY-MM-DD HH:mm'));
  215. console.log(dayjs('2024-01-25').format('YYYY-MM-DD'));
  216. console.log(dayjs('2024-01-25').format('YYYY-MM'));
  217. console.log(dayjs().format('_YYYYMMDD_hhmmss'));
  218. },
  219. },
  220. };
  221. </script>
  222. <style scoped>
  223. #app {
  224. font-family: "Avenir", Helvetica, Arial, sans-serif;
  225. -webkit-font-smoothing: antialiased;
  226. -moz-osx-font-smoothing: grayscale;
  227. text-align: center;
  228. color: #2c3e50;
  229. margin-top: 60px;
  230. }
  231. .demo-component {
  232. width: 250px;
  233. margin: auto;
  234. }
  235. .control-group {
  236. margin-top: 100px;
  237. }
  238. .control {
  239. text-align: center;
  240. }
  241. .option-list {
  242. margin-top: 20px;
  243. }
  244. </style>