Picker.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <!-- 时间选择弹窗 -->
  2. <template>
  3. <portal to="notification-outlet">
  4. <div class="toast-wrap" v-if="show">
  5. <div class="content">
  6. <van-picker
  7. :columns="columns"
  8. :visible-item-count='3'
  9. @change="handlechange"
  10. :swipe-duration="time"
  11. />
  12. <div class="confBtn">
  13. <p class="tip" v-show="showTip">请选择正确的时间</p>
  14. <div @click="getVal">确认</div>
  15. </div>
  16. </div>
  17. <div class="mask" @click="onCancel"></div>
  18. </div>
  19. </portal>
  20. </template>
  21. <script type="text/javascript">
  22. import $ from 'jquery';
  23. const num = [0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9];
  24. const unit = ['分钟', '小时', '天', '周', '月','年','分钟', '小时', '天', '周', '月','年'];
  25. export default {
  26. name:'Picker',
  27. data() {
  28. return {
  29. columns: [
  30. {
  31. values:num,
  32. className:'column1',
  33. defaultIndex: 10
  34. },
  35. {
  36. values:num,
  37. className:'column2',
  38. defaultIndex: 11
  39. },
  40. {
  41. values:unit,
  42. className:'column3',
  43. defaultIndex: 6
  44. },
  45. ],
  46. showTip:false,
  47. columIndx:[],
  48. time:100
  49. }
  50. },
  51. props:['show','picIndex'],
  52. created(){//记录位置
  53. this.columns[0].defaultIndex = this.columIndx[0] = this.picIndex[0] || 10;
  54. this.columns[1].defaultIndex = this.columIndx[1] = this.picIndex[1] || 11;
  55. this.columns[2].defaultIndex = this.columIndx[2] = this.picIndex[2] || 6;
  56. },
  57. methods: {
  58. getVal() {
  59. const val1 = $(".column1 ul .van-picker-column__item--selected")[0].innerText;
  60. const val2 = $(".column2 ul .van-picker-column__item--selected")[0].innerText;
  61. const val3 = $(".column3 ul .van-picker-column__item--selected")[0].innerText;
  62. // 如果选两个0,则提示
  63. if(val1 == 0 && val2 == 0){
  64. this.showTip = true;
  65. return
  66. }
  67. const value = val1 + val2 + val3;
  68. this.$emit("comfirn",value,this.columIndx)
  69. this.columns[0].defaultIndex = this.columIndx[0] || 10;
  70. this.columns[1].defaultIndex = this.columIndx[1] || 11;
  71. this.columns[2].defaultIndex = this.columIndx[2] || 6;
  72. },
  73. onCancel() {
  74. this.$emit("cancel")
  75. this.showTip = false;
  76. },
  77. handlechange(picker, value, colum){//关掉提示
  78. this.showTip = false;
  79. if(colum == 2){
  80. this.columIndx[colum] = unit.lastIndexOf(value[colum]);
  81. }else{
  82. this.columIndx[colum] = num.lastIndexOf(value[colum]);
  83. }
  84. },
  85. resetIndex(){//清除时复原默认index
  86. this.columns[0].defaultIndex = 10;
  87. this.columns[1].defaultIndex = 11;
  88. this.columns[2].defaultIndex = 6;
  89. }
  90. }
  91. };
  92. </script>
  93. <style lang="less" scoped>
  94. @import '../less/base.less';
  95. .toast-wrap{
  96. .content{
  97. padding-top: 25px;
  98. width: 6.3rem;
  99. // height: 4.8rem;
  100. height: 227px;
  101. background: #fff;
  102. z-index: 999;
  103. position: fixed;
  104. top:50%;
  105. left: 50%;
  106. transform: translate(-50%,-50%);
  107. box-sizing: border-box;
  108. border-radius: .2rem;
  109. z-index: 1000;
  110. font-size: .3rem;
  111. overflow-y: hidden;
  112. .confBtn{
  113. height: 44px;
  114. line-height: 44px;
  115. text-align: center;
  116. border-top: 1px solid #ebedf0;
  117. margin-top: 25px;
  118. position: relative;
  119. .tip{
  120. position: absolute;
  121. top:-22px;
  122. font-size: .24rem;
  123. color: red;
  124. line-height: 20px;
  125. width:100%;
  126. text-align: center;
  127. }
  128. div{
  129. color: #colors[theme];
  130. }
  131. }
  132. }
  133. .mask{
  134. .mask;
  135. z-index: 999;
  136. }
  137. }
  138. </style>