itmister-date-picker.nvue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <template>
  2. <view v-if="isShow">
  3. <view class="date-picker-mask" bubble='true' @click="hide" :class="[isOpen?'show-date-picker-mask':'hide-date-picekr-mask']"
  4. :style="{backgroundColor:maskColor}"></view>
  5. <view class="date-picker-container" @click.stop="handleClick" :class="[isOpen?'show-date-picker':'hide-date-picekr']">
  6. <!-- 操作 -->
  7. <view class="date-picker-title row between-center">
  8. <text class="date-picker-cancel" @click="hide">取消</text>
  9. <text class="date-picker-confirm" @click="dateConfirm">确定</text>
  10. </view>
  11. <!-- 内容 -->
  12. <picker-view class="date-picker-box" v-if="visible" :indicator-style="indicatorStyle" :value="value" @change="bindChange">
  13. <picker-view-column class="center">
  14. <view class="date-picker-item center" v-for="(item,index) in years" :key="index">
  15. <text v-if='item!="长期"'>{{item}}年</text>
  16. <text v-else>{{item}}</text>
  17. </view>
  18. </picker-view-column>
  19. <picker-view-column>
  20. <view class="date-picker-item center" v-for="(item,index) in months" :key="index">
  21. <text v-if="item!=''">{{item}}月</text>
  22. <text v-else>{{item}}</text>
  23. </view>
  24. </picker-view-column>
  25. <picker-view-column>
  26. <view class="date-picker-item center" v-for="(item,index) in days" :key="index">
  27. <text v-if="item!=''">{{item}}日</text>
  28. <text v-else>{{item}}</text>
  29. </view>
  30. </picker-view-column>
  31. </picker-view>
  32. </view>
  33. </view>
  34. </template>
  35. <script>
  36. export default {
  37. name: "datePicker",
  38. props: {
  39. maskColor: { // 模态框背景色
  40. type: String,
  41. default: 'rgba(0,0,0,0.3)'
  42. },
  43. checkYear:{
  44. type: [String,Number],
  45. default: new Date().getFullYear()
  46. },
  47. checkMonth:{
  48. type: [String,Number],
  49. default: new Date().getMonth() + 1
  50. },
  51. checkDay:{
  52. type: [String,Number],
  53. default: new Date().getDate()
  54. }
  55. },
  56. data() {
  57. const date = new Date();
  58. const years = ['长期'];
  59. const months = [''];
  60. // console.log(this.checkYear)
  61. // const month = date.getMonth() + 1
  62. // const day = date.getDate();
  63. const year = this.checkYear
  64. const month = this.checkMonth
  65. const day = this.checkDay
  66. console.log(year,month,day)
  67. // 可在此设置起始年份和终止年份
  68. for (let i = 1940; i <= date.getFullYear() + 10; i++) {
  69. years.push(i);
  70. }
  71. for (let i = 1; i <= 12; i++) {
  72. months.push(i);
  73. }
  74. console.log(months)
  75. return {
  76. isShow: false, // 是否弹出
  77. isOpen: false,
  78. years,
  79. months,
  80. days: [''],
  81. year,
  82. month,
  83. day,
  84. value: [year - 1940, month - 1, day - 1], // 默认选中当天
  85. visible: true,
  86. indicatorStyle: `height: ${Math.round(uni.getSystemInfoSync().screenWidth/(750/100))}px;`
  87. }
  88. },
  89. methods: {
  90. // 选中日期
  91. dateConfirm() {
  92. let dateobj={
  93. year:this.year,
  94. month:this.month?this.month > 9 ? this.month : '0' + this.month:'',
  95. day:this.day?this.day > 9 ? this.day :'0' + this.day:'',
  96. date:''
  97. }
  98. if(this.year!='长期'){
  99. var val=[]
  100. for (let i = 0; i < this.years.length; i++) {
  101. if(this.year==this.years[i]){
  102. val[0]=i
  103. }
  104. }
  105. for (let i = 0; i < this.months.length; i++) {
  106. if(this.month==this.months[i]){
  107. val[1]=i
  108. }
  109. }
  110. for (let i = 0; i < this.days.length; i++) {
  111. if(this.day==this.days[i]){
  112. val[2]=i
  113. }
  114. }
  115. this.value=val
  116. dateobj.date= this.year + '-' + (this.month > 9 ? this.month : '0' + this.month) + '-' + (this.day > 9 ? this.day :
  117. '0' + this.day);
  118. }else{
  119. this.value=[0,0,0]
  120. dateobj.date = this.year
  121. }
  122. // 发送一个点击事件,并把当前选中的日期发送出去
  123. this.$emit('dateConfirm', dateobj);
  124. this.hide();
  125. },
  126. bindChange(e) {
  127. // console.log(this.value,e)
  128. const val = e.detail.value;
  129. this.year = this.years[val[0]];
  130. this.month = this.months[val[1]];
  131. this.day = this.days[val[2]];
  132. // this.value=[this.year, this.month, this.day]
  133. },
  134. // 弹出
  135. show() {
  136. this.isShow = true;
  137. this.$nextTick(() => {
  138. setTimeout(() => {
  139. this.isOpen = true;
  140. }, 20);
  141. });
  142. },
  143. // 关闭
  144. hide() {
  145. this.isOpen = false;
  146. setTimeout(() => {
  147. this.isShow = false;
  148. }, 200);
  149. },
  150. // 阻止冒泡
  151. handleClick(event) {
  152. event.stopPropagation();
  153. }
  154. },
  155. watch: {
  156. "month": { // 监听月份变化,改变当前月份天数值
  157. handler(val) {
  158. if (val < 8&&this.year!='长期') {
  159. if (val % 2 !== 0) {
  160. this.days = [''];
  161. for (let i = 1; i <= 31; i++) {
  162. this.days.push(i);
  163. }
  164. } else {
  165. this.days = [''];
  166. for (let i = 1; i <= 30; i++) {
  167. this.days.push(i);
  168. }
  169. }
  170. }
  171. if (val > 7&&this.year!='长期') {
  172. if (val % 2 === 0) {
  173. this.days = [''];
  174. for (let i = 1; i <= 31; i++) {
  175. this.days.push(i);
  176. }
  177. } else {
  178. this.days = [''];
  179. for (let i = 1; i <= 30; i++) {
  180. this.days.push(i);
  181. }
  182. }
  183. }
  184. if (val === 2&&this.year!='长期') {
  185. if (this.year % 4 === 0) {
  186. this.days = [''];
  187. for (let i = 1; i <= 29; i++) {
  188. this.days.push(i);
  189. }
  190. } else {
  191. this.days = [''];
  192. for (let i = 1; i <= 28; i++) {
  193. this.days.push(i);
  194. }
  195. }
  196. }
  197. },
  198. deep: true,
  199. immediate: true
  200. },
  201. "year": { // 监听年份变化,处理2月份天数变化
  202. handler(val) {
  203. if(val=='长期'){
  204. this.months=[''];
  205. this.days = [''];
  206. }else{
  207. const months = [''];
  208. for (let i = 1; i <= 12; i++) {
  209. months.push(i);
  210. }
  211. this.months=months
  212. if (val % 4 === 0) {
  213. if (this.month === 2) {
  214. this.days = [''];
  215. for (let i = 1; i <= 29; i++) {
  216. this.days.push(i);
  217. }
  218. }
  219. } else {
  220. if (this.month === 2) {
  221. this.days = [''];
  222. for (let i = 1; i <= 28; i++) {
  223. this.days.push(i);
  224. }
  225. }
  226. }
  227. }
  228. }
  229. },
  230. deep: true,
  231. immediate: true
  232. }
  233. }
  234. </script>
  235. <style lang="scss">
  236. .date-picker-mask {
  237. position: fixed;
  238. left: 0;
  239. right: 0;
  240. top: 0;
  241. bottom: 0;
  242. z-index: 99988;
  243. }
  244. .date-picker-container {
  245. position: fixed;
  246. left: 0;
  247. right: 0;
  248. bottom: 0;
  249. z-index: 99999;
  250. background-color: #FFFFFF;
  251. }
  252. .show-date-picker-mask {
  253. transition-property: opacity;
  254. transition-duration: 0.2s;
  255. transition-timing-function: ease;
  256. opacity: 1;
  257. }
  258. .hide-date-picekr-mask {
  259. transition-property: opacity;
  260. transition-duration: 0.2s;
  261. transition-timing-function: ease;
  262. opacity: 0;
  263. }
  264. .show-date-picker {
  265. transition-property: transform, opacity;
  266. transition-duration: 0.2s;
  267. transition-timing-function: ease;
  268. transform: translateY(0);
  269. opacity: 1;
  270. /* #ifndef APP-PLUS-NVUE */
  271. -moz-transition-property: transform, opacity;
  272. -webkit-transition-property: transform, opacity;
  273. -o-transition-property: transform, opacity;
  274. -moz-transition-duration: 0.2s;
  275. -webkit-transition-duration: 0.2s;
  276. -webkit-transition-duration: 0.2s;
  277. -moz-transition-timing-function: ease;
  278. -webkit-transition-timing-function: ease;
  279. -o-transition-timing-function: ease;
  280. -moz-transform: translateY(0);
  281. -webkit-transform: translateY(0);
  282. -o-transform: translateY(0);
  283. /* #endif */
  284. }
  285. .hide-date-picekr {
  286. transition-property: transform, opacity;
  287. transition-duration: 0.2s;
  288. transition-timing-function: ease;
  289. transform: translateY(500px);
  290. opacity: 1;
  291. /* #ifndef APP-PLUS-NVUE */
  292. -moz-transition-property: transform, opacity;
  293. -webkit-transition-property: transform, opacity;
  294. -o-transition-property: transform, opacity;
  295. -moz-transition-duration: 0.2s;
  296. -webkit-transition-duration: 0.2s;
  297. -webkit-transition-duration: 0.2s;
  298. -moz-transition-timing-function: ease;
  299. -webkit-transition-timing-function: ease;
  300. -o-transition-timing-function: ease;
  301. -moz-transform: translateY(500px);
  302. -webkit-transform: translateY(500px);
  303. -o-transform: translateY(500px);
  304. /* #endif */
  305. }
  306. // 确定、取消
  307. .date-picker-title {
  308. height: 100rpx;
  309. padding: 0 20rpx;
  310. box-shadow: 0 1rpx 1rpx #e4e4e4;
  311. }
  312. .date-picker-confirm {
  313. padding: 10rpx 30rpx;
  314. font-size: 32rpx;
  315. color: #007AFF;
  316. }
  317. .date-picker-cancel {
  318. padding: 10rpx 30rpx;
  319. font-size: 32rpx;
  320. color: red;
  321. }
  322. // 内容
  323. .date-picker-box {
  324. width: 750rpx;
  325. height: 500rpx;
  326. padding: 0 20rpx;
  327. /* #ifndef APP-PLUS-NVUE */
  328. box-sizing: border-box;
  329. /* #endif */
  330. background-color: #FFF;
  331. }
  332. .date-picker-item {
  333. height: 100rpx;
  334. }
  335. // flex
  336. .row {
  337. /* #ifndef APP-PLUS-NVUE */
  338. display: flex;
  339. /* #endif */
  340. flex-direction: row;
  341. }
  342. .center {
  343. /* #ifndef APP-PLUS-NVUE */
  344. display: flex;
  345. /* #endif */
  346. justify-content: center;
  347. align-items: center;
  348. }
  349. .between-center {
  350. justify-content: space-between;
  351. align-items: center;
  352. }
  353. </style>