uni-datetime-picker.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <template>
  2. <view class="uni-datetime-picker">
  3. <view @click="tiggerTimePicker">
  4. <slot>
  5. <view class="uni-datetime-picker-timebox uni-datetime-picker-flex">
  6. {{time}}<view v-if="!time" class="uni-datetime-picker-time">选择日期时间</view>
  7. <view class="uni-datetime-picker-down-arrow"></view>
  8. </view>
  9. </slot>
  10. </view>
  11. <view v-if="visible" class="uni-datetime-picker-mask" @click="initTimePicker"></view>
  12. <view v-if="visible" class="uni-datetime-picker-popup">
  13. <view class="uni-title">
  14. 设置日期和时间
  15. </view>
  16. <picker-view class="uni-datetime-picker-view" :indicator-style="indicatorStyle" :value="ymd" @change="bindDateChange">
  17. <picker-view-column class="uni-datetime-picker-hyphen">
  18. <view class="uni-datetime-picker-item" v-for="(item,index) in years" :key="index">{{item}}</view>
  19. </picker-view-column>
  20. <picker-view-column class="uni-datetime-picker-hyphen">
  21. <view class="uni-datetime-picker-item" v-for="(item,index) in months" :key="index">{{item < 10 ? '0' + item : item}}</view>
  22. </picker-view-column>
  23. <picker-view-column>
  24. <view class="uni-datetime-picker-item" v-for="(item,index) in days" :key="index">{{item < 10 ? '0' + item : item}}</view>
  25. </picker-view-column>
  26. </picker-view>
  27. <picker-view class="uni-datetime-picker-view" :indicator-style="indicatorStyle" :value="hms" @change="bindTimeChange">
  28. <picker-view-column class="uni-datetime-picker-colon">
  29. <view class="uni-datetime-picker-item" v-for="(item,index) in hours" :key="index">{{item < 10 ? '0' + item : item}}</view>
  30. </picker-view-column>
  31. <picker-view-column class="uni-datetime-picker-colon">
  32. <view class="uni-datetime-picker-item" v-for="(item,index) in minutes" :key="index">{{item < 10 ? '0' + item : item}}</view>
  33. </picker-view-column>
  34. <picker-view-column>
  35. <view class="uni-datetime-picker-item" v-for="(item,index) in seconds" :key="index">{{item < 10 ? '0' + item : item}}</view>
  36. </picker-view-column>
  37. </picker-view>
  38. <view class="uni-datetime-picker-btn">
  39. <view class="" @click="clearTime">重置</view>
  40. <view class="uni-datetime-picker-btn-group">
  41. <view class="uni-datetime-picker-cancel" @click="tiggerTimePicker">取消</view>
  42. <view class="" @click="setTime">确定</view>
  43. </view>
  44. </view>
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. export default {
  50. data() {
  51. return {
  52. visible: false,
  53. time: '',
  54. years: [],
  55. months: [],
  56. days: [],
  57. hours: [],
  58. minutes: [],
  59. seconds: [],
  60. year: 1900,
  61. month: 0,
  62. day: 0,
  63. hour: 0,
  64. minute: 0,
  65. second: 0,
  66. indicatorStyle: `height: 50px;`,
  67. }
  68. },
  69. props: {
  70. type: {
  71. type: String,
  72. default: 'datetime-local'
  73. },
  74. timestamp: {
  75. type: Boolean,
  76. default: false
  77. },
  78. value: {
  79. type: [String, Number],
  80. default: ''
  81. },
  82. maxYear: {
  83. type: [Number, String],
  84. default: 2100
  85. },
  86. minYear: {
  87. type: [Number, String],
  88. default: 1900
  89. }
  90. },
  91. computed: {
  92. ymd() {
  93. return [this.year - this.minYear, this.month - 1, this.day - 1]
  94. },
  95. hms() {
  96. return [this.hour, this.minute, this.second]
  97. }
  98. },
  99. watch: {
  100. value(newValue) {
  101. this.parseValue(this.value)
  102. this.initTime()
  103. }
  104. },
  105. created() {
  106. this.form = this.getForm('uniForms')
  107. this.formItem = this.getForm('uniFormsItem')
  108. if (this.formItem) {
  109. if (this.formItem.name) {
  110. this.rename = this.formItem.name
  111. this.form.inputChildrens.push(this)
  112. }
  113. }
  114. },
  115. mounted() {
  116. const date = new Date()
  117. for (let i = this.minYear; i <= this.maxYear; i++) {
  118. this.years.push(i)
  119. }
  120. for (let i = 1; i <= 12; i++) {
  121. this.months.push(i)
  122. }
  123. for (let i = 1; i <= 31; i++) {
  124. this.days.push(i)
  125. }
  126. for (let i = 0; i <= 23; i++) {
  127. this.hours.push(i)
  128. }
  129. for (let i = 0; i <= 59; i++) {
  130. this.minutes.push(i)
  131. }
  132. for (let i = 0; i <= 59; i++) {
  133. this.seconds.push(i)
  134. }
  135. this.parseValue(this.value)
  136. if (this.value) {
  137. this.initTime()
  138. }
  139. },
  140. methods: {
  141. /**
  142. * 获取父元素实例
  143. */
  144. getForm(name = 'uniForms') {
  145. let parent = this.$parent;
  146. let parentName = parent.$options.name;
  147. while (parentName !== name) {
  148. parent = parent.$parent;
  149. if (!parent) return false
  150. parentName = parent.$options.name;
  151. }
  152. return parent;
  153. },
  154. parseDateTime(datetime) {
  155. let defaultDate = null
  156. if (!datetime) {
  157. defaultDate = new Date()
  158. } else {
  159. defaultDate = new Date(datetime)
  160. }
  161. this.year = defaultDate.getFullYear()
  162. if (this.year < this.minYear || this.year > this.maxYear) {
  163. const now = Date.now()
  164. this.parseDateTime(now)
  165. return
  166. }
  167. this.month = defaultDate.getMonth() + 1
  168. this.day = defaultDate.getDate()
  169. this.hour = defaultDate.getHours()
  170. this.minute = defaultDate.getMinutes()
  171. this.second = defaultDate.getSeconds()
  172. },
  173. parseValue(defaultTime) {
  174. if (Number(defaultTime)) {
  175. defaultTime = parseInt(defaultTime)
  176. }
  177. this.parseDateTime(defaultTime)
  178. },
  179. bindDateChange(e) {
  180. const val = e.detail.value
  181. this.year = this.years[val[0]]
  182. this.month = this.months[val[1]]
  183. this.day = this.days[val[2]]
  184. },
  185. bindTimeChange(e) {
  186. const val = e.detail.value
  187. this.hour = this.hours[val[0]]
  188. this.minute = this.minutes[val[1]]
  189. this.second = this.seconds[val[2]]
  190. },
  191. initTimePicker() {
  192. // if (!this.time) {
  193. // this.parseValue()
  194. // }
  195. this.parseValue(this.value)
  196. this.visible = !this.visible
  197. },
  198. tiggerTimePicker() {
  199. this.visible = !this.visible
  200. },
  201. clearTime() {
  202. this.time = ''
  203. this.tiggerTimePicker()
  204. },
  205. initTime() {
  206. this.time = this.createDomSting()
  207. if (!this.timestamp) {
  208. this.formItem && this.formItem.setValue(this.time)
  209. this.$emit('change', this.time)
  210. } else {
  211. this.formItem && this.formItem.setValue(this.createTimeStamp(this.time))
  212. this.$emit('change', this.createTimeStamp(this.time))
  213. }
  214. },
  215. setTime() {
  216. this.initTime()
  217. this.tiggerTimePicker()
  218. },
  219. createTimeStamp(time) {
  220. return Date.parse(new Date(time))
  221. },
  222. createDomSting() {
  223. const yymmdd = this.year +
  224. '-' +
  225. (this.month < 10 ? '0' + this.month : this.month) +
  226. '-' +
  227. (this.day < 10 ? '0' + this.day : this.day) +
  228. ' ' +
  229. (this.hour < 10 ? '0' + this.hour : this.hour) +
  230. ':' +
  231. (this.minute < 10 ? '0' + this.minute : this.minute) +
  232. ':' +
  233. (this.second < 10 ? '0' + this.second : this.second)
  234. return yymmdd
  235. }
  236. }
  237. }
  238. </script>
  239. <style>
  240. .uni-datetime-picker-view {
  241. width: 100%;
  242. height: 130px;
  243. margin-top: 30px;
  244. }
  245. .uni-datetime-picker-item {
  246. line-height: 50px;
  247. text-align: center;
  248. }
  249. .uni-datetime-picker-btn {
  250. margin-top: 60px;
  251. display: flex;
  252. justify-content: space-between;
  253. color: blue;
  254. cursor: pointer;
  255. }
  256. .uni-datetime-picker-btn-group {
  257. display: flex;
  258. }
  259. .uni-datetime-picker-cancel {
  260. margin-right: 30px;
  261. }
  262. .uni-datetime-picker-mask {
  263. position: fixed;
  264. bottom: 0px;
  265. top: 0px;
  266. left: 0px;
  267. right: 0px;
  268. background-color: rgba(0, 0, 0, 0.4);
  269. transition-duration: 0.3s;
  270. z-index: 998;
  271. }
  272. .uni-datetime-picker-popup {
  273. border-radius: 8px;
  274. padding: 30px;
  275. width: 270px;
  276. background-color: #fff;
  277. position: fixed;
  278. top: 50%;
  279. left: 50%;
  280. transform: translate(-50%, -50%);
  281. transition-duration: 0.3s;
  282. z-index: 999;
  283. }
  284. .uni-datetime-picker-time {
  285. color: grey;
  286. }
  287. .uni-datetime-picker-colon::after {
  288. content: ':';
  289. position: absolute;
  290. top: 53px;
  291. right: 0;
  292. }
  293. .uni-datetime-picker-hyphen::after {
  294. content: '-';
  295. position: absolute;
  296. top: 53px;
  297. right: -2px;
  298. }
  299. .uni-datetime-picker-timebox {
  300. border: 1px solid #E5E5E5;
  301. border-radius: 5px;
  302. padding: 7px 10px;
  303. box-sizing: border-box;
  304. cursor: pointer;
  305. }
  306. // 下箭头
  307. .uni-datetime-picker-down-arrow {
  308. display :inline-block;
  309. position: relative;
  310. width: 20px;
  311. height: 15px;
  312. }
  313. .uni-datetime-picker-down-arrow::after {
  314. display: inline-block;
  315. content: " ";
  316. height: 9px;
  317. width: 9px;
  318. border-width: 0 1px 1px 0;
  319. border-color: #E5E5E5;
  320. border-style: solid;
  321. transform: matrix(0.71, 0.71, -0.71, 0.71, 0, 0);
  322. transform-origin: center;
  323. transition: transform .3s;
  324. position: absolute;
  325. top: 50%;
  326. right: 5px;
  327. margin-top: -5px;
  328. }
  329. .uni-datetime-picker-flex {
  330. display: flex;
  331. justify-content: space-between;
  332. }
  333. </style>