mpvue-datepicker.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <template>
  2. <div class="mpvue-picker">
  3. <div :class="{'pickerMask':showPicker}" @click="maskClick" catchtouchmove="true"></div>
  4. <div class="mpvue-picker-content " :class="{'mpvue-picker-view-show':showPicker}">
  5. <div class="mpvue-picker__hd" catchtouchmove="true">
  6. <div class="mpvue-picker__action" @click="pickerCancel">取消</div>
  7. <div class="mpvue-picker__action" :style="{color:themeColor}" @click="pickerConfirm">确定</div>
  8. </div>
  9. <picker-view indicator-style="height: 40px;" class="mpvue-picker-view" :value="pickerValue" @change="pickerChange">
  10. <block>
  11. <picker-view-column>
  12. <view class="picker-item" v-for="(item,index) in years" :key="index">{{item.label}}年</view>
  13. </picker-view-column>
  14. <picker-view-column>
  15. <view class="picker-item" v-for="(item,index) in months" :key="index">{{item.label}}月</view>
  16. </picker-view-column>
  17. <picker-view-column>
  18. <view class="picker-item" v-for="(item,index) in days" :key="index">{{item.label}}日</view>
  19. </picker-view-column>
  20. </block>
  21. </picker-view>
  22. </div>
  23. </div>
  24. </template>
  25. <script>
  26. //import provinceData from './city-data/province.js';
  27. //import cityData from './city-data/city.js';
  28. //import areaData from './city-data/area.js';
  29. var addMonth=function(date, months) {
  30. let y = date.getFullYear();
  31. let m = date.getMonth();
  32. var d = date.getDate();
  33. // console.log('add month y' + y + ' m:' + m + ' d: ' + d + ' month: ' + months);
  34. y += Math.floor((m + 1 + months) / 12); //计算年
  35. m = Math.floor((m + 1 + months) % 12) - 1; //计算月
  36. let _date = new Date(y,m,d);
  37. // console.log('result y:' + y + ' m: ' + m + '_date:' + _date);
  38. return _date;
  39. };
  40. var addDay=function(date, days) {
  41. var _date = new Date(date.getTime() + 24*60*60*1000*days)
  42. return _date;
  43. };
  44. export default {
  45. data() {
  46. const date = new Date()
  47. const years = []
  48. const year = date.getFullYear()
  49. const months = []
  50. const month = date.getMonth() + 1
  51. const days = []
  52. const day = date.getDate()
  53. let index = 0;
  54. for (let i = 1990; i <= date.getFullYear(); i++) {
  55. let data = { value: index++,label:i};
  56. years.push(data)
  57. }
  58. index = 0;
  59. for (let i = 1; i <= 12; i++) {
  60. let data = { value: index++,label:i};
  61. months.push(data)
  62. }
  63. index = 0;
  64. for (let i = 1; i <= 31; i++) {
  65. let data = { value: index++,label:i};
  66. days.push(data)
  67. }
  68. return {
  69. pickerValue: [0, 0, 0],
  70. years,
  71. year,
  72. months,
  73. month,
  74. days,
  75. day,
  76. /* 是否显示控件 */
  77. showPicker: false,
  78. };
  79. },
  80. created() {
  81. // console.log('mpvueCityPicker created')
  82. this.init()
  83. },
  84. props: {
  85. /* 默认值 */
  86. pickerValueDefault: {
  87. type: Array,
  88. default(){
  89. return [0, 0 , 0]
  90. }
  91. },
  92. /* 主题色 */
  93. themeColor: String
  94. },
  95. watch:{
  96. pickerValueDefault(){
  97. this.init();
  98. }
  99. },
  100. methods: {
  101. init() {
  102. // console.log('mpvueCityPicker init')
  103. if(this.pickerValueDefault.length == 0)
  104. this.pickerValueDefault = [0,0, 0];
  105. //console.log('ageDownLimit'+JSON.stringify(this.ageDownLimitDataList))
  106. //console.log('ageUpLimit'+JSON.stringify(this.ageUpLimitDataList))
  107. //this.handPickValueDefault(); // 对 pickerValueDefault 做兼容处理
  108. this.pickerValue = this.pickerValueDefault;
  109. },
  110. show() {
  111. setTimeout(() => {
  112. this.showPicker = true;
  113. }, 0);
  114. },
  115. maskClick() {
  116. this.pickerCancel();
  117. },
  118. pickerCancel() {
  119. this.showPicker = false;
  120. this._$emit('onCancel');
  121. },
  122. pickerConfirm(e) {
  123. this.showPicker = false;
  124. this._$emit('onConfirm');
  125. },
  126. showPickerView() {
  127. this.showPicker = true;
  128. },
  129. handPickValueDefault() {
  130. //console.log('mpvueCityPicker handPickValueDefault')
  131. console.log('默认树'+JSON.stringify(this.pickerValueDefault))
  132. //console.log('省'+JSON.stringify(this.pickerValueDefault))
  133. if (this.pickerValueDefault[0] > this.years.length - 1) {
  134. this.pickerValueDefault[0] = this.years.length - 1;
  135. }
  136. if (this.pickerValueDefault[1] > this.months.length - 1) {
  137. this.pickerValueDefault[1] = this.months.length - 1;
  138. }
  139. if (this.pickerValueDefault[2] > this.days.length - 1) {
  140. this.pickerValueDefault[2] = this.days.length - 1;
  141. }
  142. },
  143. pickerChange(e) {
  144. let changePickerValue = e.mp.detail.value;
  145. if (this.pickerValue[0] !== changePickerValue[0]) {
  146. // 第一级发生滚动
  147. //this.cityDataList = cityData[changePickerValue[0]];
  148. //this.areaDataList = areaData[changePickerValue[0]][0];
  149. changePickerValue[1] = 0;
  150. console.log('第一级滚动')
  151. console.log('date'+JSON.stringify(this.years[changePickerValue[0]]) +'-'+JSON.stringify(this.months[changePickerValue[1]]))
  152. }
  153. else if (this.pickerValue[1] !== changePickerValue[1]) {
  154. // 第二级滚动
  155. console.log('第二级滚动')
  156. let year = this.years[changePickerValue[0]].label;
  157. let month = this.months[changePickerValue[1]].label;
  158. console.log('date'+JSON.stringify(this.years[changePickerValue[0]]) +'-'+JSON.stringify(this.months[changePickerValue[1]]))
  159. let date = new Date(year,month-1,1);
  160. let nextMonth = addMonth(date,1);
  161. let currentMonth = addDay(nextMonth,-1);
  162. console.log('下月日期'+JSON.stringify(nextMonth))
  163. console.log('当前日期'+JSON.stringify(currentMonth))
  164. const day = currentMonth.getDate()
  165. console.log('day'+JSON.stringify(day))
  166. let index = 0;
  167. this.days =[];
  168. for (let i = 1; i <= day; i++) {
  169. let data = { value: index++,label:i};
  170. this.days.push(data)
  171. }
  172. changePickerValue[2] = 0;
  173. }
  174. this.pickerValue = changePickerValue;
  175. this._$emit('onChange');
  176. },
  177. _$emit(emitName) {
  178. let pickObj = {
  179. label: this._getLabel(),
  180. value: this.pickerValue
  181. };
  182. this.$emit(emitName, pickObj);
  183. },
  184. _getLabel() {
  185. let pcikerLabel =
  186. this.years[this.pickerValue[0]].label +
  187. '-' +
  188. this.months[this.pickerValue[1]].label+
  189. '-'+
  190. this.days[this.pickerValue[2]].label;
  191. return pcikerLabel;
  192. }
  193. }
  194. };
  195. </script>
  196. <style>
  197. .pickerMask {
  198. position: fixed;
  199. z-index: 1000;
  200. top: 0;
  201. right: 0;
  202. left: 0;
  203. bottom: 0;
  204. background: rgba(0, 0, 0, 0.6);
  205. }
  206. .mpvue-picker-content {
  207. position: fixed;
  208. bottom: 0;
  209. left: 0;
  210. width: 100%;
  211. transition: all 0.3s ease;
  212. transform: translateY(100%);
  213. z-index: 3000;
  214. }
  215. .mpvue-picker-view-show {
  216. transform: translateY(0);
  217. }
  218. .mpvue-picker__hd {
  219. display: flex;
  220. padding: 9px 15px;
  221. background-color: #fff;
  222. position: relative;
  223. text-align: center;
  224. font-size: 17px;
  225. }
  226. .mpvue-picker__hd:after {
  227. content: ' ';
  228. position: absolute;
  229. left: 0;
  230. bottom: 0;
  231. right: 0;
  232. height: 1px;
  233. border-bottom: 1px solid #e5e5e5;
  234. color: #e5e5e5;
  235. transform-origin: 0 100%;
  236. transform: scaleY(0.5);
  237. }
  238. .mpvue-picker__action {
  239. display: block;
  240. flex: 1;
  241. color: #1aad19;
  242. }
  243. .mpvue-picker__action:first-child {
  244. text-align: left;
  245. color: #888;
  246. }
  247. .mpvue-picker__action:last-child {
  248. text-align: right;
  249. }
  250. .picker-item {
  251. text-align: center;
  252. line-height: 40px;
  253. text-overflow: ellipsis;
  254. white-space: nowrap;
  255. font-size: 16px;
  256. }
  257. .mpvue-picker-view {
  258. position: relative;
  259. bottom: 0;
  260. left: 0;
  261. width: 100%;
  262. height: 238px;
  263. background-color: rgba(255, 255, 255, 1);
  264. }
  265. </style>